From d4d03940c58a0177edb93c8854929856e9975bf9 Mon Sep 17 00:00:00 2001 From: Father Chrysostomos Date: Mon, 4 Jun 2012 14:04:03 -0700 Subject: [perl #78742] Store CopSTASH in a pad under threads MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- embedvar.h | 3 +++ 1 file changed, 3 insertions(+) (limited to 'embedvar.h') diff --git a/embedvar.h b/embedvar.h index c4a0fa9042..52452617ac 100644 --- a/embedvar.h +++ b/embedvar.h @@ -316,6 +316,9 @@ #define PL_stack_sp (vTHX->Istack_sp) #define PL_start_env (vTHX->Istart_env) #define PL_stashcache (vTHX->Istashcache) +#define PL_stashpad (vTHX->Istashpad) +#define PL_stashpadix (vTHX->Istashpadix) +#define PL_stashpadmax (vTHX->Istashpadmax) #define PL_statbuf (vTHX->Istatbuf) #define PL_statcache (vTHX->Istatcache) #define PL_statgv (vTHX->Istatgv) -- cgit v1.2.1