This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
In Perl_scalarvoid(), avoid creating a temporary SV for simple messages.
authorNicholas Clark <nick@ccl4.org>
Wed, 11 Jul 2012 18:24:14 +0000 (20:24 +0200)
committerNicholas Clark <nick@ccl4.org>
Tue, 31 Jul 2012 11:04:42 +0000 (13:04 +0200)
If using an SV to generate a potentially UTF-8 error message, pass that SV
onward to the code that generates warnings, and use a SV-specific format.
Otherwise use a %s format and pass the char * pointer directly to
Perl_ck_warner(). This avoids creating a temporary SV just to hold a fixed
ASCII string, but retains the ability to generate clean UTF-8 error messages.

op.c

diff --git a/op.c b/op.c
index 8fc0312..6cfff4f 100644 (file)
--- a/op.c
+++ b/op.c
@@ -1165,8 +1165,8 @@ Perl_scalarvoid(pTHX_ OP *o)
 {
     dVAR;
     OP *kid;
+    SV *useless_sv = NULL;
     const char* useless = NULL;
-    U32 useless_is_utf8 = 0;
     SV* sv;
     U8 want;
 
@@ -1367,19 +1367,19 @@ Perl_scalarvoid(pTHX_ OP *o)
                            useless = NULL;
                    else {
                        SV * const dsv = newSVpvs("");
-                       SV* msv = sv_2mortal(Perl_newSVpvf(aTHX_
-                                   "a constant (%s)",
-                                   pv_pretty(dsv, maybe_macro, SvCUR(sv), 32, NULL, NULL,
-                                           PERL_PV_PRETTY_DUMP | PERL_PV_ESCAPE_NOCLEAR | PERL_PV_ESCAPE_UNI_DETECT )));
+                       useless_sv
+                            = Perl_newSVpvf(aTHX_
+                                            "a constant (%s)",
+                                            pv_pretty(dsv, maybe_macro,
+                                                      SvCUR(sv), 32, NULL, NULL,
+                                                      PERL_PV_PRETTY_DUMP
+                                                      | PERL_PV_ESCAPE_NOCLEAR
+                                                      | PERL_PV_ESCAPE_UNI_DETECT));
                        SvREFCNT_dec(dsv);
-                       useless = SvPV_nolen(msv);
-                       useless_is_utf8 = SvUTF8(msv);
                    }
                }
                else if (SvOK(sv)) {
-                   SV* msv = sv_2mortal(Perl_newSVpvf(aTHX_
-                               "a constant (%"SVf")", sv));
-                   useless = SvPV_nolen(msv);
+                   useless_sv = Perl_newSVpvf(aTHX_ "a constant (%"SVf")", sv);
                }
                else
                    useless = "a constant (undef)";
@@ -1506,10 +1506,18 @@ Perl_scalarvoid(pTHX_ OP *o)
     case OP_SCALAR:
        return scalar(o);
     }
-    if (useless)
-       Perl_ck_warner(aTHX_ packWARN(WARN_VOID), "Useless use of %"SVf" in void context",
-                       newSVpvn_flags(useless, strlen(useless),
-                            SVs_TEMP | ( useless_is_utf8 ? SVf_UTF8 : 0 )));
+
+    if (useless_sv) {
+        /* mortalise it, in case warnings are fatal.  */
+        Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
+                       "Useless use of %"SVf" in void context",
+                       sv_2mortal(useless_sv));
+    }
+    else if (useless) {
+       Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
+                      "Useless use of %s in void context",
+                      useless);
+    }
     return o;
 }