This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
av_exists: dont make a mortal never to use it
authorbulk88 (via RT) <perlbug-followup@perl.org>
Fri, 23 Nov 2012 00:33:34 +0000 (16:33 -0800)
committerSteffen Mueller <smueller@cpan.org>
Fri, 23 Nov 2012 07:44:44 +0000 (08:44 +0100)
Make av_exists slightly smaller and faster by reducing the liveness of a
mortal SV and using a NN SvTRUE_nomg.

There were 3 cases, where this mortal would be created, yet a return
happened and the mortal went unused and was wasted. So move the mortal
creation point closer to where it is first used. Also var sv will never be
null, so use a NN version of SvTRUE_nomg created in commit [perl #115870].
The retbool line isn't actually required for optimization reasons, but was
created just in case something in the future changes or some unknown
compiler does something inefficiently.

For me with 32 bit x86 VC 2003, before av_exists was 0x1C2, after 0x1B8.

Comment from committer: Includes SvTRUE_nomg_NN from [perl #115870].

av.c
sv.h

diff --git a/av.c b/av.c
index 6d2b949..cfa5399 100644 (file)
--- a/av.c
+++ b/av.c
@@ -934,7 +934,6 @@ Perl_av_exists(pTHX_ AV *av, I32 key)
         const MAGIC * const regdata_magic
             = mg_find((const SV *)av, PERL_MAGIC_regdata);
         if (tied_magic || regdata_magic) {
-           SV * const sv = sv_newmortal();
             MAGIC *mg;
             /* Handle negative array indices 20020222 MJD */
             if (key < 0) {
@@ -948,14 +947,18 @@ Perl_av_exists(pTHX_ AV *av, I32 key)
                 else
                     return FALSE;
             }
-
-            mg_copy(MUTABLE_SV(av), sv, 0, key);
-            mg = mg_find(sv, PERL_MAGIC_tiedelem);
-            if (mg) {
-                magic_existspack(sv, mg);
-                return cBOOL(SvTRUE_nomg(sv));
-            }
-
+           {
+               SV * const sv = sv_newmortal();
+               mg_copy(MUTABLE_SV(av), sv, 0, key);
+               mg = mg_find(sv, PERL_MAGIC_tiedelem);
+               if (mg) {
+                   magic_existspack(sv, mg);
+                   {
+                       I32 retbool = SvTRUE_nomg_NN(sv);
+                       return cBOOL(retbool);
+                   }
+               }
+           }
         }
     }
 
diff --git a/sv.h b/sv.h
index 35b360f..fe29a41 100644 (file)
--- a/sv.h
+++ b/sv.h
@@ -1709,6 +1709,7 @@ Like sv_utf8_upgrade, but doesn't do magic on C<sv>.
 #define SvTRUE(sv)        ((sv) && (SvGMAGICAL(sv) ? sv_2bool(sv) : SvTRUE_common(sv, sv_2bool_nomg(sv))))
 #define SvTRUE_nomg(sv)   ((sv) && (                                SvTRUE_common(sv, sv_2bool_nomg(sv))))
 #define SvTRUE_NN(sv)              (SvGMAGICAL(sv) ? sv_2bool(sv) : SvTRUE_common(sv, sv_2bool_nomg(sv)))
+#define SvTRUE_nomg_NN(sv) (                                        SvTRUE_common(sv, sv_2bool_nomg(sv)))
 #define SvTRUE_common(sv,fallback) (                   \
       !SvOK(sv)                                                \
        ? 0                                             \