Skip to content

Commit b9eeeef

Browse files
committed
op.c: re-enable coderef-in-stash optimization
When seeing 'sub foo { ... }', perl does not need to allocate a full typeglob (with the subroutine being stored in the CODE slot). Instead, it can just store a coderef directly in the stash. This optimization was first announced in perl5220delta: > - Subroutines in packages no longer need to be stored in typeglobs: > declaring a subroutine will now put a simple sub reference directly > in the stash if possible, saving memory. The typeglob still > notionally exists, so accessing it will cause the stash entry to be > upgraded to a typeglob (i.e. this is just an internal implementation > detail). This optimization does not currently apply to XSUBs or > exported subroutines, and method calls will undo it, since they > cache things in typeglobs. [GH #13392] However, due to a bug this optimization didn't actually work except for package main (GH #15671). The issue was fixed in v5.27.5, but the fix was backed out again in v5.27.9 because of CPAN breakage. This patch re-enables the optimization because I want to see what the current state of CPAN breakage is.
1 parent 7fdc8f3 commit b9eeeef

File tree

2 files changed

+1
-7
lines changed

2 files changed

+1
-7
lines changed

op.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10819,12 +10819,9 @@ Perl_newATTRSUB_x(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs,
1081910819
Also, we may be called from load_module at run time, so
1082010820
PL_curstash (which sets CvSTASH) may not point to the stash the
1082110821
sub is stored in. */
10822-
/* XXX This optimization is currently disabled for packages other
10823-
than main, since there was too much CPAN breakage. */
1082410822
const I32 flags =
1082510823
ec ? GV_NOADD_NOINIT
1082610824
: (IN_PERL_RUNTIME && PL_curstash != CopSTASH(PL_curcop))
10827-
|| PL_curstash != PL_defstash
1082810825
|| memchr(name, ':', namlen) || memchr(name, '\'', namlen)
1082910826
? gv_fetch_flags
1083010827
: GV_ADDMULTI | GV_NOINIT | GV_NOTQUAL;

t/op/sub.t

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -398,10 +398,7 @@ is ref($main::{rt_129916}), 'CODE', 'simple sub stored as CV in stash (main::)';
398398
package RT129916;
399399
sub foo { 42 }
400400
}
401-
{
402-
local $::TODO = "disabled for now";
403-
is ref($RT129916::{foo}), 'CODE', 'simple sub stored as CV in stash (non-main::)';
404-
}
401+
is ref($RT129916::{foo}), 'CODE', 'simple sub stored as CV in stash (non-main::)';
405402

406403
# Calling xsub via ampersand syntax when @_ has holes
407404
SKIP: {

0 commit comments

Comments
 (0)