Skip to content

Commit 2ad59ed

Browse files
committed
mg.c pp.c pp_sys.c inline.h add more SV_CONST() SV* HEK* hash optimizations
-faster method lookups, faster new SVPV creation (COWs), some of these locations were missed by the original branch/PRs/commits that added SV_CONST() macro/api. -I belive all "" C string literals that match a SV_CONST_UPPERCASE SV* HEK* cached constant have been replaced with their SV* POK HEK* COW buffer equivalents inside libperl with this commit, excluding some instances of "__ANON__" strings. Only PERL_CORE files qualify for the SV_CONST() optimization, because of design choices made previously about the SV_CONST() API. Changing the PERL_CORE-only design choice is out of scope of this patch. -in pp_dbmopen() add SV_CONST(TIEHASH) macros for faster lookup/U32 hash pre-calc, and change newSVpvs_flags("AnyDBM_File", SVs_TEMP) to newSVpvs_share("AnyDBM_File"), because this sv is used multiple times in this pp_*() function, and it is a package name, and it is guaranteed to get passed into hv_common() somewhere eventually in some child function call we are making. -some "__ANON__" locations were not changed from sv_*newSV*pvs("__ANON__"); to sv_*newSV*hek(SV_CONST(__ANON__)); because right after, there is a sv_catpvs(""); that will make the SVPV HEK* COW instantly de-COW which saved no CPU or memory resources in the end, and only wasted them. Or it didn't look "safe" for a SV* COW buffer to be on that line. -pp_tie() call_method() is an thin inefficient wrapper that makes a mortal SVPV around a C string, since the real backend API is call_sv(), so switch the call_method() in pp_tie() to the read backend function call_sv() and avoid making that mortal SVPV
1 parent aa5e4ce commit 2ad59ed

File tree

6 files changed

+44
-27
lines changed

6 files changed

+44
-27
lines changed

inline.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,6 +1176,11 @@ Perl_rpp_invoke_xs(pTHX_ CV *cv)
11761176
CvXSUB(cv)(aTHX_ cv);
11771177
}
11781178

1179+
/* SV_CONST() is limited to #ifdef PERL_CORE, so make a temporary macro. */
1180+
#define x_SV_CONST(name) PL_sv_consts[SV_CONST_##name] \
1181+
? PL_sv_consts[SV_CONST_##name] \
1182+
: (PL_sv_consts[SV_CONST_##name] = newSVpv_share(#name, 0))
1183+
11791184

11801185
/* for SvCANEXISTDELETE() macro in pp.h */
11811186
PERL_STATIC_INLINE bool
@@ -1188,10 +1193,11 @@ Perl_sv_can_existdelete(pTHX_ SV *sv)
11881193

11891194
HV *stash = SvSTASH(SvRV(SvTIED_obj(sv, mg)));
11901195
return stash &&
1191-
gv_fetchmethod_autoload(stash, "EXISTS", TRUE) &&
1192-
gv_fetchmethod_autoload(stash, "DELETE", TRUE);
1196+
gv_fetchmethod_sv_flags(stash, x_SV_CONST(EXISTS), GV_AUTOLOAD) &&
1197+
gv_fetchmethod_sv_flags(stash, x_SV_CONST(DELETE), GV_AUTOLOAD);
11931198
}
11941199

1200+
#undef x_SV_CONST
11951201

11961202
/* ----------------------------- regexp.h ----------------------------- */
11971203

mg.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2306,8 +2306,7 @@ Perl_magic_scalarpack(pTHX_ HV *hv, MAGIC *mg)
23062306
HV * const pkg = SvSTASH((const SV *)SvRV(tied));
23072307

23082308
PERL_ARGS_ASSERT_MAGIC_SCALARPACK;
2309-
2310-
if (!gv_fetchmethod_autoload(pkg, "SCALAR", FALSE)) {
2309+
if (!gv_fetchmethod_sv_flags(pkg, SV_CONST(SCALAR), 0)) {
23112310
SV *key;
23122311
if (HvEITER_get(hv))
23132312
/* we are in an iteration so the hash cannot be empty */

op.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11080,7 +11080,7 @@ Perl_newATTRSUB_x(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs,
1108011080
gv = gv_fetchsv(sv, gv_fetch_flags, SVt_PVCV);
1108111081
has_name = TRUE;
1108211082
} else if (PL_curstash) {
11083-
gv = gv_fetchpvs("__ANON__", gv_fetch_flags, SVt_PVCV);
11083+
gv = gv_fetchsv(SV_CONST(__ANON__), gv_fetch_flags, SVt_PVCV);
1108411084
has_name = FALSE;
1108511085
} else {
1108611086
gv = gv_fetchpvs("__ANON__::__ANON__", gv_fetch_flags, SVt_PVCV);

pp.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -681,8 +681,12 @@ PP(pp_gelem)
681681
case 'P':
682682
if (memEQs(elem, len, "PACKAGE")) {
683683
const HV * const stash = GvSTASH(gv);
684-
const HEK * const hek = stash ? HvNAME_HEK(stash) : NULL;
685-
sv = hek ? newSVhek(hek) : newSVpvs("__ANON__");
684+
const HEK * hek = stash ? HvNAME_HEK(stash) : NULL;
685+
if (!hek) {
686+
SV * sv_hek = SV_CONST(__ANON__);
687+
hek = SvSHARED_HEK_FROM_PV(SvPVX_const(sv_hek));
688+
}
689+
sv = newSVhek(hek);
686690
}
687691
break;
688692
case 'S':

pp_sys.c

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,7 +1078,7 @@ PP_wrapped(pp_tie, 0, 1)
10781078
GV *gv = NULL;
10791079
SV *sv;
10801080
const SSize_t markoff = MARK - PL_stack_base;
1081-
const char *methname;
1081+
SV* methname;
10821082
int how = PERL_MAGIC_tied;
10831083
SSize_t items;
10841084
SV *varsv = *++MARK;
@@ -1087,7 +1087,7 @@ PP_wrapped(pp_tie, 0, 1)
10871087
case SVt_PVHV:
10881088
{
10891089
HE *entry;
1090-
methname = "TIEHASH";
1090+
methname = SV_CONST(TIEHASH);
10911091
if (HvLAZYDEL(varsv) && (entry = HvEITER_get((HV *)varsv))) {
10921092
HvLAZYDEL_off(varsv);
10931093
hv_free_ent(NULL, entry);
@@ -1097,7 +1097,7 @@ PP_wrapped(pp_tie, 0, 1)
10971097
break;
10981098
}
10991099
case SVt_PVAV:
1100-
methname = "TIEARRAY";
1100+
methname = SV_CONST(TIEARRAY);
11011101
if (!AvREAL(varsv)) {
11021102
if (!AvREIFY(varsv))
11031103
croak("Cannot tie unreifiable array");
@@ -1109,7 +1109,7 @@ PP_wrapped(pp_tie, 0, 1)
11091109
case SVt_PVGV:
11101110
case SVt_PVLV:
11111111
if (isGV_with_GP(varsv) && !SvFAKE(varsv)) {
1112-
methname = "TIEHANDLE";
1112+
methname = SV_CONST(TIEHANDLE);
11131113
how = PERL_MAGIC_tiedscalar;
11141114
/* For tied filehandles, we apply tiedscalar magic to the IO
11151115
slot of the GP rather than the GV itself. AMS 20010812 */
@@ -1124,7 +1124,7 @@ PP_wrapped(pp_tie, 0, 1)
11241124
}
11251125
/* FALLTHROUGH */
11261126
default:
1127-
methname = "TIESCALAR";
1127+
methname = SV_CONST(TIESCALAR);
11281128
how = PERL_MAGIC_tiedscalar;
11291129
break;
11301130
}
@@ -1137,7 +1137,7 @@ PP_wrapped(pp_tie, 0, 1)
11371137
while (items--)
11381138
PUSHs(*MARK++);
11391139
PUTBACK;
1140-
call_method(methname, G_SCALAR);
1140+
call_sv(methname, G_SCALAR | G_METHOD);
11411141
}
11421142
else {
11431143
/* Can't use call_method here, else this: fileno FOO; tie @a, "FOO"
@@ -1148,37 +1148,37 @@ PP_wrapped(pp_tie, 0, 1)
11481148
stash = gv_stashsv(*MARK, 0);
11491149
if (!stash) {
11501150
if (SvROK(*MARK))
1151-
DIE(aTHX_ "Can't locate object method %" PVf_QUOTEDPREFIX
1151+
DIE(aTHX_ "Can't locate object method %" SVf_QUOTEDPREFIX
11521152
" via package %" SVf_QUOTEDPREFIX,
1153-
methname, SVfARG(*MARK));
1153+
SVfARG(methname), SVfARG(*MARK));
11541154
else if (isGV(*MARK)) {
11551155
/* If the glob doesn't name an existing package, using
11561156
* SVfARG(*MARK) would yield "*Foo::Bar" or *main::Foo. So
11571157
* generate the name for the error message explicitly. */
11581158
SV *stashname = sv_newmortal();
11591159
gv_fullname4(stashname, (GV *) *MARK, NULL, FALSE);
1160-
DIE(aTHX_ "Can't locate object method %" PVf_QUOTEDPREFIX
1160+
DIE(aTHX_ "Can't locate object method %" SVf_QUOTEDPREFIX
11611161
" via package %" SVf_QUOTEDPREFIX,
1162-
methname, SVfARG(stashname));
1162+
SVfARG(methname), SVfARG(stashname));
11631163
}
11641164
else {
11651165
SV *stashname = !SvPOK(*MARK) ? &PL_sv_no
11661166
: SvCUR(*MARK) ? *MARK
11671167
: newSVpvs_flags("main", SVs_TEMP);
1168-
DIE(aTHX_ "Can't locate object method %" PVf_QUOTEDPREFIX
1168+
DIE(aTHX_ "Can't locate object method %" SVf_QUOTEDPREFIX
11691169
" via package %" SVf_QUOTEDPREFIX
11701170
" (perhaps you forgot to load %" SVf_QUOTEDPREFIX "?)",
1171-
methname, SVfARG(stashname), SVfARG(stashname));
1171+
SVfARG(methname), SVfARG(stashname), SVfARG(stashname));
11721172
}
11731173
}
1174-
else if (!(gv = gv_fetchmethod(stash, methname))) {
1174+
else if (!(gv = gv_fetchmethod_sv_flags(stash, methname, GV_AUTOLOAD))) {
11751175
/* The effective name can only be NULL for stashes that have
11761176
* been deleted from the symbol table, which this one can't
11771177
* be, since we just looked it up by name.
11781178
*/
1179-
DIE(aTHX_ "Can't locate object method %" PVf_QUOTEDPREFIX
1179+
DIE(aTHX_ "Can't locate object method %" SVf_QUOTEDPREFIX
11801180
" via package %" HEKf_QUOTEDPREFIX ,
1181-
methname, HvENAME_HEK_NN(stash));
1181+
SVfARG(methname), HvENAME_HEK_NN(stash));
11821182
}
11831183
ENTER_with_name("call_TIE");
11841184
PUSHSTACKi(PERLSI_MAGIC);
@@ -1229,7 +1229,7 @@ PP_wrapped(pp_untie, 1, 0)
12291229
if ((mg = SvTIED_mg(sv, how))) {
12301230
SV * const obj = SvRV(SvTIED_obj(sv, mg));
12311231
if (obj && SvSTASH(obj)) {
1232-
GV * const gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE);
1232+
GV * const gv = gv_fetchmethod_sv_flags(SvSTASH(obj), SV_CONST(UNTIE), 0);
12331233
CV *cv;
12341234
if (gv && isGV(gv) && (cv = GvCV(gv))) {
12351235
PUSHMARK(SP);
@@ -1295,13 +1295,13 @@ PP_wrapped(pp_dbmopen, 3, 0)
12951295
GV *gv = NULL;
12961296

12971297
HV * const hv = MUTABLE_HV(POPs);
1298-
SV * const sv = newSVpvs_flags("AnyDBM_File", SVs_TEMP);
1298+
SV * const sv = sv_2mortal(newSVpvs_share("AnyDBM_File"));
12991299
stash = gv_stashsv(sv, 0);
1300-
if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
1300+
if (!stash || !(gv = gv_fetchmethod_sv_flags(stash, SV_CONST(TIEHASH), GV_AUTOLOAD))) {
13011301
PUTBACK;
13021302
require_pv("AnyDBM_File.pm");
13031303
SPAGAIN;
1304-
if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH")))
1304+
if (!stash || !(gv = gv_fetchmethod_sv_flags(stash, SV_CONST(TIEHASH), GV_AUTOLOAD)))
13051305
DIE(aTHX_ "No dbm on this machine");
13061306
}
13071307

sv.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2664,8 +2664,12 @@ Create a new IO, setting the reference count to 1.
26642664
# define SV_CONST_FETCHSIZE 5
26652665
# define SV_CONST_STORE 6
26662666
# define SV_CONST_STORESIZE 7
2667-
# define SV_CONST_EXISTS 8
2667+
#endif
2668+
2669+
/* required by Perl_sv_can_existdelete() */
2670+
#define SV_CONST_EXISTS 8
26682671

2672+
#if defined(PERL_CORE) || defined(PERL_EXT)
26692673
# define SV_CONST_PUSH 9
26702674
# define SV_CONST_POP 10
26712675
# define SV_CONST_SHIFT 11
@@ -2690,8 +2694,12 @@ Create a new IO, setting the reference count to 1.
26902694
# define SV_CONST_BINMODE 28
26912695
# define SV_CONST_FILENO 29
26922696
# define SV_CONST_CLOSE 30
2697+
#endif
26932698

2699+
/* required by Perl_sv_can_existdelete() */
26942700
# define SV_CONST_DELETE 31
2701+
2702+
#if defined(PERL_CORE) || defined(PERL_EXT)
26952703
# define SV_CONST_CLEAR 32
26962704
# define SV_CONST_UNTIE 33
26972705
# define SV_CONST_DESTROY 34

0 commit comments

Comments
 (0)