This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #78742] Store CopSTASH in a pad under threads
authorFather Chrysostomos <sprout@cpan.org>
Mon, 4 Jun 2012 21:04:03 +0000 (14:04 -0700)
committerFather Chrysostomos <sprout@cpan.org>
Tue, 5 Jun 2012 01:14:53 +0000 (18:14 -0700)
commitd4d03940c58a0177edb93c8854929856e9975bf9
tree3852b1f0c210b55ba9172c1edbef3040a6fbae36
parentf3ac9fb2631eb3706dcdd2fe0274a953da37486f
[perl #78742] Store CopSTASH in a pad under threads

Before this commit, a pointer to the cop’s stash was stored in
cop->cop_stash under non-threaded perls, and the name and name length
were stored in cop->cop_stashpv and cop->cop_stashlen under ithreads.

Consequently, eval "__PACKAGE__" would end up returning the
wrong package name under threads if the current package had been
assigned over.

This commit changes the way cops store their stash under threads.  Now
it is an offset (cop->cop_stashoff) into the new PL_stashpad array
(just a mallocked block), which holds pointers to all stashes that
have code compiled in them.

I didn’t use the lexical pads, because CopSTASH(cop) won’t work unless
PL_curpad is holding the right pad.  And things start to get very
hairy in pp_caller, since the correct pad isn’t anywhere easily
accessible on the context stack (oldcomppad actually referring to the
current comppad).  The approach I’ve followed uses far less code, too.

In addition to fixing the bug, this also saves memory.  Instead of
allocating a separate PV for every single statement (to hold the stash
name), now all lines of code in a package can share the same stashpad
slot.  So, on a 32-bit OS X, that’s 16 bytes less memory per COP for
short package names.  Since stashoff is the same size as stashpv,
there is no difference there.  Each package now needs just 4 bytes in
the stashpad for storing a pointer.

For speed’s sake PL_stashpadix stores the index of the last-used
stashpad offset.  So only when switching packages is there a linear
search through the stashpad.
14 files changed:
cop.h
embed.fnc
embed.h
embedvar.h
ext/B/B.xs
gv.c
intrpvar.h
makedef.pl
op.c
perl.c
proto.h
scope.h
sv.c
util.c