This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Skip trailing constants when searching pads
authorFather Chrysostomos <sprout@cpan.org>
Tue, 30 Jul 2013 03:40:24 +0000 (20:40 -0700)
committerFather Chrysostomos <sprout@cpan.org>
Tue, 30 Jul 2013 13:30:39 +0000 (06:30 -0700)
commit7db6405c07905ec5bc4178ea8184089842949690
treef45de0317bdf2e3aad3035a0c51fe26163cafd88
parent533d93cc1b438187c43582769dc47b2f8c1797a3
Skip trailing constants when searching pads

Under ithreads, constants and GVs are stored in the pad.

When names are looked up up in a pad, the search begins at the end and
works its way toward the beginning, so that an $x declared later masks
one declared earlier.

If there are many constants at the end of the pad, which can happen
for generated code such as lib/unicore/TestProp.pl (which has about
100,000 lines and over 500,000 pad entries for constants at the
end of the file scope’s pad), it can take a long time to search
through them all.

Before commit 325e1816, constants used &PL_sv_undef ‘names’.  Since
that is the default value for array elements (when viewed directly
through AvARRAY, rather than av_fetch), the pad allocation code did
not even bother storing the ‘name’ for these.  So the name pad (aka
padnamelist) was not extended, leaving just 10 entries or so in the
case of lib/unicore/TestProp.pl.

Commit 325e1816 make pad constants have &PL_sv_no names, so the
name pad would be implicitly extended as a result of storing
&PL_sv_no, causing a huge slowdown in t/re/uniprops.t (which runs
lib/unicore/TestProp.pl) under threaded builds.

Now, normally the name pad *does* get extended to match the pad,
in pad_tidy, but that is skipped for string eval (and required
file scope, of course).  Hence, wrapping the contents of
lib/unicore/TestProp.pl in a sub or adding ‘my $x’ to end of it will
cause the same slowdown before 325e1816.

lib/unicore/TestProp.pl just happened to be written (ok, generated) in
such a way that it ended up with a small name pad.

This commit fixes things to make them as fast as before by recording
the index of the last named variable in the pad.  Anything following
that is disregarded in pad lookup and search begins with the last
named variable.  (This actually does make things faster before for
subs with many trailing constants in the pad.)

This is not a complete fix.  Adding ‘my $x’ to the end of a large file
like lib/unicore/TestProp.pl will make it just as slow again.

Ultimately we need another algorithm, such as a binary search.
av.h
dump.c
mg.c
pad.c
pad.h
sv.c
sv.h