This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
scope.c: Don’t stringify globs on scope exit
authorFather Chrysostomos <sprout@cpan.org>
Mon, 30 Jul 2012 21:27:12 +0000 (14:27 -0700)
committerFather Chrysostomos <sprout@cpan.org>
Tue, 31 Jul 2012 06:24:23 +0000 (23:24 -0700)
This is a waste:

    /* Can clear pad variable in place? */
    if (SvREFCNT(sv) <= 1 && !SvOBJECT(sv)) {
/*
 * if a my variable that was made readonly is going out of
 * scope, we want to remove the readonlyness so that it can
 * go out of scope quietly
 */
if (SvPADMY(sv) && !SvFAKE(sv))
    SvREADONLY_off(sv);

if (SvTHINKFIRST(sv))
    sv_force_normal_flags(sv, SV_IMMEDIATE_UNREF);

We can simply drop the globness in sv_force_normal instead of flatten-
ing globs to strings.  The same applies to COWs.  The SV_COW_DROP_PV
flag accomplishes both.

Before and after:

$ time ./miniperl -e 'for (1..1000000) { my $x = *foo }'

real 0m2.324s
user 0m2.316s
sys 0m0.006s
$ time ./miniperl -e 'for (1..1000000) { my $x = *foo }'

real 0m0.848s
user 0m0.840s
sys 0m0.005s

scope.c

diff --git a/scope.c b/scope.c
index 2a9b3d5..d42aa80 100644 (file)
--- a/scope.c
+++ b/scope.c
@@ -911,7 +911,8 @@ Perl_leave_scope(pTHX_ I32 base)
                    SvREADONLY_off(sv);
 
                if (SvTHINKFIRST(sv))
-                   sv_force_normal_flags(sv, SV_IMMEDIATE_UNREF);
+                   sv_force_normal_flags(sv, SV_IMMEDIATE_UNREF
+                                            |SV_COW_DROP_PV);
                if (SvTYPE(sv) == SVt_PVHV)
                    Perl_hv_kill_backrefs(aTHX_ MUTABLE_HV(sv));
                if (SvMAGICAL(sv))