This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
optimize CLEAR_ERRSV
authorDaniel Dragan <bulk88@hotmail.com>
Fri, 26 Dec 2014 14:15:23 +0000 (09:15 -0500)
committerFather Chrysostomos <sprout@cpan.org>
Sat, 27 Dec 2014 01:33:00 +0000 (17:33 -0800)
-gv_add_by_type is special only for non-GVs, and add AV to GV, otherwise
it is just assigning to GP slot (in this case through GvSV), which
CLEAR_ERRSV already did in another branch, so inline the gv_add_by_type
-dont compute GvSV multiple times, GvSV contains 2 derefs, after this patch
it will contains just 1 deref ("*svp") without an offset (deref without
offset is smaller in x86 machine code than deref with offset)
-SvREFCNT_dec_NN for efficiency
-move SvPOK_only closer to SvMAGICAL, this ensures SvFLAGS is read and
written only once, not 2 reads and 1 write, this is specifically for
RISC-ish cpus
-the goto clresv_newemptypv is because VC optimizer in -O1 didn't combine
the branches

perl521.dll VC 2003 before .text section size in machine code bytes
0xc8a63, after 0xc88e3

perl.h

diff --git a/perl.h b/perl.h
index 9fe2fb5..7d93332 100644 (file)
--- a/perl.h
+++ b/perl.h
@@ -1269,19 +1269,22 @@ EXTERN_C char *crypt(const char *, const char *);
 
 #define ERRSV GvSVn(PL_errgv)
 
+/* contains inlined gv_add_by_type */
 #define CLEAR_ERRSV() STMT_START {                                     \
-    if (!GvSV(PL_errgv)) {                                             \
-       sv_setpvs(GvSV(gv_add_by_type(PL_errgv, SVt_PV)), "");          \
-    } else if (SvREADONLY(GvSV(PL_errgv))) {                           \
-       SvREFCNT_dec(GvSV(PL_errgv));                                   \
-       GvSV(PL_errgv) = newSVpvs("");                                  \
+    SV ** const svp = &GvSV(PL_errgv);                                 \
+    if (!*svp) {                                                       \
+       goto clresv_newemptypv;                                         \
+    } else if (SvREADONLY(*svp)) {                                     \
+       SvREFCNT_dec_NN(*svp);                                          \
+       clresv_newemptypv:                                              \
+       *svp = newSVpvs("");                                            \
     } else {                                                           \
-       SV *const errsv = GvSV(PL_errgv);                               \
+       SV *const errsv = *svp;                                         \
        sv_setpvs(errsv, "");                                           \
+       SvPOK_only(errsv);                                              \
        if (SvMAGICAL(errsv)) {                                         \
            mg_free(errsv);                                             \
        }                                                               \
-       SvPOK_only(errsv);                                              \
     }                                                                  \
     } STMT_END