This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
locale.c: Avoid unnecessary malloc
authorKarl Williamson <khw@cpan.org>
Mon, 18 Dec 2023 14:19:45 +0000 (07:19 -0700)
committerKarl Williamson <khw@cpan.org>
Sat, 30 Dec 2023 16:52:08 +0000 (09:52 -0700)
Here an SV is being created with a size, but immediately
sv_usepvn_flags() is called to place an already allocated and populated
PV into it, so that initial size is wasted.

locale.c

index 9efcd41..1a820fd 100644 (file)
--- a/locale.c
+++ b/locale.c
@@ -5969,9 +5969,8 @@ Perl_sv_strftime_tm(pTHX_ SV * fmt, const struct tm * mytm)
                              );
     SV * sv = NULL;
     if (retval) {
-        STRLEN len = strlen(retval);
-        sv = newSV(len);
-        sv_usepvn_flags(sv, retval, len, SV_HAS_TRAILING_NUL);
+        sv = newSV_type(SVt_PV);
+        sv_usepvn_flags(sv, retval, strlen(retval), SV_HAS_TRAILING_NUL);
 
         if (result_utf8ness == UTF8NESS_YES) {
             SvUTF8_on(sv);