This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
pp_iter: optimise integer setting in for (1..10) {}
authorDavid Mitchell <davem@iabyn.com>
Fri, 18 Dec 2015 16:36:54 +0000 (16:36 +0000)
committerDavid Mitchell <davem@iabyn.com>
Wed, 3 Feb 2016 09:18:35 +0000 (09:18 +0000)
If the target SV is a simple SVt_IV, which it is likely to be,
just update it directly rather than calling sv_setiv()

pp_hot.c

index 28360e7..8b8a0ad 100644 (file)
--- a/pp_hot.c
+++ b/pp_hot.c
@@ -2680,7 +2680,22 @@ PP(pp_iter)
        /* see NB comment above */
        if (oldsv && LIKELY(SvREFCNT(oldsv) == 1 && !SvMAGICAL(oldsv))) {
            /* safe to reuse old SV */
        /* see NB comment above */
        if (oldsv && LIKELY(SvREFCNT(oldsv) == 1 && !SvMAGICAL(oldsv))) {
            /* safe to reuse old SV */
-           sv_setiv(oldsv, cur);
+
+            if (    (SvFLAGS(oldsv) & (SVTYPEMASK|SVf_THINKFIRST|SVf_IVisUV))
+                 == SVt_IV)
+            {
+                /* Cheap SvIOK_only().
+                 * Assert that flags which SvIOK_only() would test or
+                 * clear can't be set, because we're SVt_IV */
+                assert(!(SvFLAGS(oldsv) &
+                    (SVf_OOK|SVf_UTF8|(SVf_OK & ~(SVf_IOK|SVp_IOK)))));
+                SvFLAGS(oldsv) |= (SVf_IOK|SVp_IOK);
+                /* SvIV_set() where sv_any points to head */
+                oldsv->sv_u.svu_iv = cur;
+
+            }
+            else
+                sv_setiv(oldsv, cur);
        }
        else
        {
        }
        else
        {