Skip to content

Commit 562f242

Browse files
committed
Inlined newSV_type(SVt_NULL) leaner than non-inlined newSV(0)
When a function outside of sv.c creates a SV via newSV(0): * There is a call to Perl_newSV * A SV head is uprooted and its flags set * A runtime check is made to effectively see if 0 > 0 * The new SV* is returned Replacing newSV(0) with newSV_type(SVt_NULL) should be more efficient, because (assuming there are SV heads to uproot), the only step is: * A SV head is uprooted and its flags set
1 parent e168a4f commit 562f242

File tree

14 files changed

+43
-43
lines changed

14 files changed

+43
-43
lines changed

av.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ Perl_av_fetch(pTHX_ AV *av, SSize_t key, I32 lval)
292292

293293
if (!AvARRAY(av)[key]) {
294294
emptyness:
295-
return lval ? av_store(av,key,newSV(0)) : NULL;
295+
return lval ? av_store(av,key,newSV_type(SVt_NULL)) : NULL;
296296
}
297297

298298
return &AvARRAY(av)[key];
@@ -473,7 +473,7 @@ Perl_av_make(pTHX_ SSize_t size, SV **strp)
473473

474474
SvGETMAGIC(*strp); /* before newSV, in case it dies */
475475
AvFILLp(av)++;
476-
ary[i] = newSV(0);
476+
ary[i] = newSV_type(SVt_NULL);
477477
sv_setsv_flags(ary[i], *strp,
478478
SV_DO_COW_SVSETSV|SV_NOSTEAL);
479479
strp++;
@@ -1124,7 +1124,7 @@ Perl_av_iter_p(pTHX_ AV *av) {
11241124

11251125
SV *
11261126
Perl_av_nonelem(pTHX_ AV *av, SSize_t ix) {
1127-
SV * const sv = newSV(0);
1127+
SV * const sv = newSV_type(SVt_NULL);
11281128
PERL_ARGS_ASSERT_AV_NONELEM;
11291129
if (!av_store(av,ix,sv))
11301130
return sv_2mortal(sv); /* has tie magic */

dump.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2674,7 +2674,7 @@ S_append_gv_name(pTHX_ GV *gv, SV *out)
26742674
sv_catpvs_nomg(out, "<NULLGV>");
26752675
return;
26762676
}
2677-
sv = newSV(0);
2677+
sv = newSV_type(SVt_NULL);
26782678
gv_fullname4(sv, gv, NULL, FALSE);
26792679
Perl_sv_catpvf(aTHX_ out, "$%" SVf, SVfARG(sv));
26802680
SvREFCNT_dec_NN(sv);

gv.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ Perl_newGP(pTHX_ GV *const gv)
202202
Newxz(gp, 1, GP);
203203
gp->gp_egv = gv; /* allow compiler to reuse gv after this */
204204
#ifndef PERL_DONT_CREATE_GVSV
205-
gp->gp_sv = newSV(0);
205+
gp->gp_sv = newSV_type(SVt_NULL);
206206
#endif
207207

208208
/* PL_curcop may be null here. E.g.,
@@ -294,7 +294,7 @@ Perl_cvgv_from_hek(pTHX_ CV *cv)
294294
if (!CvSTASH(cv)) return NULL;
295295
ASSUME(CvNAME_HEK(cv));
296296
svp = hv_fetchhek(CvSTASH(cv), CvNAME_HEK(cv), 0);
297-
gv = MUTABLE_GV(svp && *svp ? *svp : newSV(0));
297+
gv = MUTABLE_GV(svp && *svp ? *svp : newSV_type(SVt_NULL));
298298
if (!isGV(gv))
299299
gv_init_pvn(gv, CvSTASH(cv), HEK_KEY(CvNAME_HEK(cv)),
300300
HEK_LEN(CvNAME_HEK(cv)),
@@ -580,7 +580,7 @@ S_maybe_add_coresub(pTHX_ HV * const stash, GV *gv,
580580
ampable = FALSE;
581581
}
582582
if (!gv) {
583-
gv = (GV *)newSV(0);
583+
gv = (GV *)newSV_type(SVt_NULL);
584584
gv_init(gv, stash, name, len, TRUE);
585585
}
586586
GvMULTI_on(gv);
@@ -1359,7 +1359,7 @@ Perl_gv_autoload_pvn(pTHX_ HV *stash, const char *name, STRLEN len, U32 flags)
13591359
if (!isGV(vargv)) {
13601360
gv_init_pvn(vargv, varstash, S_autoload, S_autolen, 0);
13611361
#ifdef PERL_DONT_CREATE_GVSV
1362-
GvSV(vargv) = newSV(0);
1362+
GvSV(vargv) = newSV_type(SVt_NULL);
13631363
#endif
13641364
}
13651365
LEAVE;
@@ -2516,7 +2516,7 @@ Perl_gv_fetchpvn_flags(pTHX_ const char *nambeg, STRLEN full_len, I32 flags,
25162516
/* By this point we should have a stash and a name */
25172517
gvp = (GV**)hv_fetch(stash,name,is_utf8 ? -(I32)len : (I32)len,add);
25182518
if (!gvp || *gvp == (const GV *)&PL_sv_undef) {
2519-
if (addmg) gv = (GV *)newSV(0); /* tentatively */
2519+
if (addmg) gv = (GV *)newSV_type(SVt_NULL); /* tentatively */
25202520
else return NULL;
25212521
}
25222522
else gv = *gvp, addmg = 0;

hv.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ Perl_hv_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
480480
HV_FETCH_ISSTORE
481481
| HV_DISABLE_UVAR_XKEY
482482
| return_svp,
483-
newSV(0), hash);
483+
newSV_type(SVt_NULL), hash);
484484
} else {
485485
if (flags & HVhek_FREEKEY)
486486
Safefree(key);
@@ -739,7 +739,7 @@ Perl_hv_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
739739
break;
740740
}
741741
/* LVAL fetch which actually needs a store. */
742-
val = newSV(0);
742+
val = newSV_type(SVt_NULL);
743743
HvPLACEHOLDERS(hv)--;
744744
} else {
745745
/* store */
@@ -793,7 +793,7 @@ Perl_hv_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
793793
return NULL;
794794
}
795795
if (action & HV_FETCH_LVALUE) {
796-
val = action & HV_FETCH_EMPTY_HE ? NULL : newSV(0);
796+
val = action & HV_FETCH_EMPTY_HE ? NULL : newSV_type(SVt_NULL);
797797
if (SvMAGICAL(hv)) {
798798
/* At this point the old hv_fetch code would call to hv_store,
799799
which in turn might do some tied magic. So we need to make that
@@ -3247,7 +3247,7 @@ S_refcounted_he_value(pTHX_ const struct refcounted_he *he)
32473247

32483248
switch(he->refcounted_he_data[0] & HVrhek_typemask) {
32493249
case HVrhek_undef:
3250-
value = newSV(0);
3250+
value = newSV_type(SVt_NULL);
32513251
break;
32523252
case HVrhek_delete:
32533253
value = &PL_sv_placeholder;

inline.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ Perl_av_fetch_simple(pTHX_ AV *av, SSize_t key, I32 lval)
134134
assert(key > -1);
135135

136136
if ( (key > AvFILLp(av)) || !AvARRAY(av)[key]) {
137-
return lval ? av_store_simple(av,key,newSV(0)) : NULL;
137+
return lval ? av_store_simple(av,key,newSV_type(SVt_NULL)) : NULL;
138138
} else {
139139
return &AvARRAY(av)[key];
140140
}

op.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10964,7 +10964,7 @@ S_op_const_sv(pTHX_ const OP *o, CV *cv, bool allow_lex)
1096410964
if (type == OP_CONST && cSVOPo->op_sv)
1096510965
sv = cSVOPo->op_sv;
1096610966
else if (type == OP_UNDEF && !o->op_private) {
10967-
sv = newSV(0);
10967+
sv = newSV_type(SVt_NULL);
1096810968
SAVEFREESV(sv);
1096910969
}
1097010970
else if (allow_lex && type == OP_PADSV) {
@@ -13623,7 +13623,7 @@ Perl_ck_glob(pTHX_ OP *o)
1362313623
LEAVE;
1362413624
}
1362513625
#endif /* !PERL_EXTERNAL_GLOB */
13626-
gv = (GV *)newSV(0);
13626+
gv = (GV *)newSV_type(SVt_NULL);
1362713627
gv_init(gv, 0, "", 0, 0);
1362813628
gv_IOadd(gv);
1362913629
op_append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv));
@@ -13689,7 +13689,7 @@ Perl_ck_index(pTHX_ OP *o)
1368913689
if ( (!SvPOK(sv) || SvNIOKp(sv) || isREGEXP(sv))
1369013690
&& SvOK(sv) && !SvROK(sv))
1369113691
{
13692-
sv = newSV(0);
13692+
sv = newSV_type(SVt_NULL);
1369313693
sv_copypv(sv, kSVOP->op_sv);
1369413694
SvREFCNT_dec_NN(kSVOP->op_sv);
1369513695
kSVOP->op_sv = sv;

pad.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ Perl_pad_alloc(pTHX_ I32 optype, U32 tmptype)
719719
pad_reset();
720720
if (tmptype == SVs_PADMY) { /* Not & because this ‘flag’ is 0. */
721721
/* For a my, simply push a null SV onto the end of PL_comppad. */
722-
sv = *av_store_simple(PL_comppad, AvFILLp(PL_comppad) + 1, newSV(0));
722+
sv = *av_store_simple(PL_comppad, AvFILLp(PL_comppad) + 1, newSV_type(SVt_NULL));
723723
retval = (PADOFFSET)AvFILLp(PL_comppad);
724724
}
725725
else {
@@ -1565,7 +1565,7 @@ Perl_pad_swipe(pTHX_ PADOFFSET po, bool refadjust)
15651565
/* if pad tmps aren't shared between ops, then there's no need to
15661566
* create a new tmp when an existing op is freed */
15671567
#ifdef USE_PAD_RESET
1568-
PL_curpad[po] = newSV(0);
1568+
PL_curpad[po] = newSV_type(SVt_NULL);
15691569
SvPADTMP_on(PL_curpad[po]);
15701570
#else
15711571
PL_curpad[po] = NULL;
@@ -2030,7 +2030,7 @@ S_cv_clone_pad(pTHX_ CV *proto, CV *cv, CV *outside, HV *cloned,
20302030
else if (sigil == '%')
20312031
sv = MUTABLE_SV(newHV());
20322032
else
2033-
sv = newSV(0);
2033+
sv = newSV_type(SVt_NULL);
20342034
/* reset the 'assign only once' flag on each state var */
20352035
if (sigil != '&' && SvPAD_STATE(namesv))
20362036
SvPADSTALE_on(sv);
@@ -2041,7 +2041,7 @@ S_cv_clone_pad(pTHX_ CV *proto, CV *cv, CV *outside, HV *cloned,
20412041
sv = SvREFCNT_inc_NN(ppad[ix]);
20422042
}
20432043
else {
2044-
sv = newSV(0);
2044+
sv = newSV_type(SVt_NULL);
20452045
SvPADTMP_on(sv);
20462046
}
20472047
PL_curpad[ix] = sv;
@@ -2435,15 +2435,15 @@ Perl_pad_push(pTHX_ PADLIST *padlist, int depth)
24352435
else if (sigil == '%')
24362436
sv = MUTABLE_SV(newHV());
24372437
else
2438-
sv = newSV(0);
2438+
sv = newSV_type(SVt_NULL);
24392439
}
24402440
}
24412441
else if (PadnamePV(names[ix])) {
24422442
sv = SvREFCNT_inc_NN(oldpad[ix]);
24432443
}
24442444
else {
24452445
/* save temporaries on recursion? */
2446-
sv = newSV(0);
2446+
sv = newSV_type(SVt_NULL);
24472447
SvPADTMP_on(sv);
24482448
}
24492449
AvARRAY(newpad)[ix] = sv;
@@ -2543,7 +2543,7 @@ Perl_padlist_dup(pTHX_ PADLIST *srcpad, CLONE_PARAMS *param)
25432543
else if (sigil == '%')
25442544
sv = MUTABLE_SV(newHV());
25452545
else
2546-
sv = newSV(0);
2546+
sv = newSV_type(SVt_NULL);
25472547
pad1a[ix] = sv;
25482548
}
25492549
}
@@ -2554,7 +2554,7 @@ Perl_padlist_dup(pTHX_ PADLIST *srcpad, CLONE_PARAMS *param)
25542554
}
25552555
else {
25562556
/* save temporaries on recursion? */
2557-
SV * const sv = newSV(0);
2557+
SV * const sv = newSV_type(SVt_NULL);
25582558
pad1a[ix] = sv;
25592559

25602560
/* SvREFCNT(oldpad[ix]) != 1 for some code in threads.xs

pp.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ S_rv2gv(pTHX_ SV *sv, const bool vivify_sv, const bool strict,
130130
HV *stash;
131131
if (SvREADONLY(sv))
132132
Perl_croak_no_modify();
133-
gv = MUTABLE_GV(newSV(0));
133+
gv = MUTABLE_GV(newSV_type(SVt_NULL));
134134
stash = CopSTASH(PL_curcop);
135135
if (SvTYPE(stash) != SVt_PVHV) stash = NULL;
136136
if (cUNOP->op_targ) {
@@ -938,7 +938,7 @@ PP(pp_undef)
938938
Newxz(gp, 1, GP);
939939
GvGP_set(sv, gp_ref(gp));
940940
#ifndef PERL_DONT_CREATE_GVSV
941-
GvSV(sv) = newSV(0);
941+
GvSV(sv) = newSV_type(SVt_NULL);
942942
#endif
943943
GvLINE(sv) = CopLINE(PL_curcop);
944944
GvEGV(sv) = MUTABLE_GV(sv);
@@ -3499,7 +3499,7 @@ PP(pp_index)
34993499

35003500
/* At this point, pv is a malloc()ed string. So donate it to temp
35013501
to ensure it will get free()d */
3502-
little = temp = newSV(0);
3502+
little = temp = newSV_type(SVt_NULL);
35033503
sv_usepvn(temp, pv, llen);
35043504
little_p = SvPVX(little);
35053505
} else {
@@ -5534,13 +5534,13 @@ PP(pp_anonhash)
55345534
{
55355535
MARK++;
55365536
SvGETMAGIC(*MARK);
5537-
val = newSV(0);
5537+
val = newSV_type(SVt_NULL);
55385538
sv_setsv_nomg(val, *MARK);
55395539
}
55405540
else
55415541
{
55425542
Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Odd number of elements in anonymous hash");
5543-
val = newSV(0);
5543+
val = newSV_type(SVt_NULL);
55445544
}
55455545
(void)hv_store_ent(hv,key,val,0);
55465546
}
@@ -5791,7 +5791,7 @@ PP(pp_push)
57915791
for (++MARK; MARK <= SP; MARK++) {
57925792
SV *sv;
57935793
if (*MARK) SvGETMAGIC(*MARK);
5794-
sv = newSV(0);
5794+
sv = newSV_type(SVt_NULL);
57955795
if (*MARK)
57965796
sv_setsv_nomg(sv, *MARK);
57975797
av_store(ary, AvFILLp(ary)+1, sv);
@@ -7026,7 +7026,7 @@ PP(pp_argelem)
70267026
SV *tmpsv;
70277027
SV **svp = av_fetch(defav, ix + i, FALSE);
70287028
SV *val = svp ? *svp : &PL_sv_undef;
7029-
tmpsv = newSV(0);
7029+
tmpsv = newSV_type(SVt_NULL);
70307030
sv_setsv(tmpsv, val);
70317031
av_store((AV*)targ, i++, tmpsv);
70327032
TAINT_NOT;
@@ -7042,7 +7042,7 @@ PP(pp_argelem)
70427042
/* see "target should usually be empty" comment above */
70437043
for (i = 0; i < argc; i++) {
70447044
SV **svp = av_fetch(defav, ix + i, FALSE);
7045-
SV *newsv = newSV(0);
7045+
SV *newsv = newSV_type(SVt_NULL);
70467046
sv_setsv_flags(newsv,
70477047
svp ? *svp : &PL_sv_undef,
70487048
(SV_DO_COW_SVSETSV|SV_NOSTEAL));
@@ -7071,7 +7071,7 @@ PP(pp_argelem)
70717071
argc -= 2;
70727072
if (UNLIKELY(SvGMAGICAL(key)))
70737073
key = sv_mortalcopy(key);
7074-
tmpsv = newSV(0);
7074+
tmpsv = newSV_type(SVt_NULL);
70757075
sv_setsv(tmpsv, val);
70767076
hv_store_ent((HV*)targ, key, tmpsv, 0);
70777077
TAINT_NOT;

pp_ctl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4386,7 +4386,7 @@ S_require_file(pTHX_ SV *sv)
43864386
than hanging another SV from it. In turn, filter_add() optionally
43874387
takes the SV to use as the filter (or creates a new SV if passed
43884388
NULL), so simply pass in whatever value filter_cache has. */
4389-
SV * const fc = filter_cache ? newSV(0) : NULL;
4389+
SV * const fc = filter_cache ? newSV_type(SVt_NULL) : NULL;
43904390
SV *datasv;
43914391
if (fc) sv_copypv(fc, filter_cache);
43924392
datasv = filter_add(S_run_user_filter, fc);

pp_hot.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4895,7 +4895,7 @@ Perl_leave_adjust_stacks(pTHX_ SV **from_sp, SV **to_sp, U8 gimme, int pass)
48954895
* ++PL_tmps_ix, moving the previous occupant there
48964896
* instead.
48974897
*/
4898-
SV *newsv = newSV(0);
4898+
SV *newsv = newSV_type(SVt_NULL);
48994899

49004900
PL_tmps_stack[++PL_tmps_ix] = *tmps_basep;
49014901
/* put it on the tmps stack early so it gets freed if we die */
@@ -5510,7 +5510,7 @@ Perl_vivify_ref(pTHX_ SV *sv, U32 to_what)
55105510
prepare_SV_for_RV(sv);
55115511
switch (to_what) {
55125512
case OPpDEREF_SV:
5513-
SvRV_set(sv, newSV(0));
5513+
SvRV_set(sv, newSV_type(SVt_NULL));
55145514
break;
55155515
case OPpDEREF_AV:
55165516
SvRV_set(sv, MUTABLE_SV(newAV()));

regexec.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3712,7 +3712,7 @@ Perl_regexec_flags(pTHX_ REGEXP * const rx, char *stringarg, char *strend,
37123712
magic belonging to this SV.
37133713
Not newSVsv, either, as it does not COW.
37143714
*/
3715-
reginfo->sv = newSV(0);
3715+
reginfo->sv = newSV_type(SVt_NULL);
37163716
SvSetSV_nosteal(reginfo->sv, sv);
37173717
SAVEFREESV(reginfo->sv);
37183718
}

scope.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ S_save_scalar_at(pTHX_ SV **sptr, const U32 flags)
232232
if (flags & SAVEf_KEEPOLDELEM)
233233
sv = osv;
234234
else {
235-
sv = (*sptr = newSV(0));
235+
sv = (*sptr = newSV_type(SVt_NULL));
236236
if (SvTYPE(osv) >= SVt_PVMG && SvMAGIC(osv))
237237
mg_localize(osv, sv, cBOOL(flags & SAVEf_SETMAGIC));
238238
}
@@ -1264,7 +1264,7 @@ Perl_leave_scope(pTHX_ I32 base)
12641264
CvLEXICAL_on(*svp);
12651265
break;
12661266
}
1267-
default: *svp = newSV(0); break;
1267+
default: *svp = newSV_type(SVt_NULL); break;
12681268
}
12691269
SvREFCNT_dec_NN(sv); /* Cast current value to the winds. */
12701270
/* preserve pad nature, but also mark as not live

sv.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8361,7 +8361,7 @@ Perl_sv_collxfrm_flags(pTHX_ SV *const sv, STRLEN *const nxp, const I32 flags)
83618361
static char *
83628362
S_sv_gets_append_to_utf8(pTHX_ SV *const sv, PerlIO *const fp, I32 append)
83638363
{
8364-
SV * const tsv = newSV(0);
8364+
SV * const tsv = newSV_type(SVt_NULL);
83658365
ENTER;
83668366
SAVEFREESV(tsv);
83678367
sv_gets(tsv, fp, 0);
@@ -16308,7 +16308,7 @@ Perl_varname(pTHX_ const GV *const gv, const char gvtype, PADOFFSET targ,
1630816308
}
1630916309

1631016310
if (subscript_type == FUV_SUBSCRIPT_HASH) {
16311-
SV * const sv = newSV(0);
16311+
SV * const sv = newSV_type(SVt_NULL);
1631216312
STRLEN len;
1631316313
const char * const pv = SvPV_nomg_const((SV*)keyname, len);
1631416314

toke.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2382,7 +2382,7 @@ S_force_strict_version(pTHX_ char *s)
23822382
s++;
23832383

23842384
if (is_STRICT_VERSION(s,&errstr)) {
2385-
SV *ver = newSV(0);
2385+
SV *ver = newSV_type(SVt_NULL);
23862386
s = (char *)scan_version(s, ver, 0);
23872387
version = newSVOP(OP_CONST, 0, ver);
23882388
}

0 commit comments

Comments
 (0)