Skip to content

Commit

Permalink
Replace sv_2mortal(newSVsv(sv)) with sv_mortalcopy_flags(sv)
Browse files Browse the repository at this point in the history
`sv_2mortal(newSVsv(sv))` calls two _sv.c_ functions in order to create
a mortal copy of `sv`:

* `sv_2mortal(sv)` is a function call to make `sv` mortal.
* `newSVsv(sv)` expands to: `Perl_newSVsv_flags(aTHX_ (sv), SV_GMAGIC|SV_NOSTEAL)

The same outcome can be achieved in a single function call:
* `sv_mortalcopy_flags(sv, SV_GMAGIC|SV_NOSTEAL)`
  • Loading branch information
richardleach committed Feb 3, 2025
1 parent 6f02939 commit 54858f1
Show file tree
Hide file tree
Showing 9 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion hv.c
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ Perl_hv_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
}
if (TAINTING_get)
TAINT_set(SvTAINTED(keysv));
keysv = sv_2mortal(newSVsv(keysv));
keysv = sv_mortalcopy_flags(keysv, SV_GMAGIC|SV_NOSTEAL);
mg_copy(MUTABLE_SV(hv), val, (char*)keysv, HEf_SVKEY);
} else {
mg_copy(MUTABLE_SV(hv), val, key, klen);
Expand Down
4 changes: 2 additions & 2 deletions mro_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -1192,7 +1192,7 @@ S_mro_gather_and_rename(pTHX_ HV * const stashes, HV * const seen_stashes,
}
}
else {
subname = sv_2mortal(newSVsv(namesv));
subname = sv_mortalcopy_flags(namesv, SV_GMAGIC|SV_NOSTEAL);
if (len == 1) sv_catpvs(subname, ":");
else {
sv_catpvs(subname, "::");
Expand Down Expand Up @@ -1275,7 +1275,7 @@ S_mro_gather_and_rename(pTHX_ HV * const stashes, HV * const seen_stashes,
}
}
else {
subname = sv_2mortal(newSVsv(namesv));
subname = sv_mortalcopy_flags(namesv, SV_GMAGIC|SV_NOSTEAL);
if (len == 1) sv_catpvs(subname, ":");
else {
sv_catpvs(subname, "::");
Expand Down
2 changes: 1 addition & 1 deletion op.c
Original file line number Diff line number Diff line change
Expand Up @@ -4072,7 +4072,7 @@ S_move_proto_attr(pTHX_ OP **proto, OP **attrs, const GV * name,

if (curstash && svname == (SV *)name
&& !memchr(SvPVX(svname), ':', SvCUR(svname))) {
svname = sv_2mortal(newSVsv(PL_curstname));
svname = sv_mortalcopy_flags(PL_curstname, SV_GMAGIC|SV_NOSTEAL);
sv_catpvs(svname, "::");
sv_catsv(svname, (SV *)name);
}
Expand Down
2 changes: 1 addition & 1 deletion pp.c
Original file line number Diff line number Diff line change
Expand Up @@ -3398,7 +3398,7 @@ PP(pp_oct)
if (DO_UTF8(sv)) {
/* If Unicode, try to downgrade
* If not possible, croak. */
SV* const tsv = sv_2mortal(newSVsv(sv));
SV* const tsv = sv_mortalcopy_flags(sv, SV_GMAGIC|SV_NOSTEAL);

SvUTF8_on(tsv);
(void)sv_utf8_downgrade(tsv, FALSE);
Expand Down
4 changes: 2 additions & 2 deletions pp_ctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1517,7 +1517,7 @@ PP_wrapped(pp_flop, (GIMME_V == G_LIST) ? 2 : 1, 0)
XPUSHs(sv);
if (strEQ(SvPVX_const(sv),tmps))
break;
sv = sv_2mortal(newSVsv(sv));
sv = sv_mortalcopy_flags(sv, SV_GMAGIC|SV_NOSTEAL);
sv_inc(sv);
}
}
Expand Down Expand Up @@ -2315,7 +2315,7 @@ PP_wrapped(pp_caller, MAXARG, 0)
}
else {
/* I think this is will always be "", but be sure */
PUSHs(sv_2mortal(newSVsv(cur_text)));
PUSHs(sv_mortalcopy_flags(cur_text, SV_GMAGIC|SV_NOSTEAL));
}

PUSHs(&PL_sv_no);
Expand Down
2 changes: 1 addition & 1 deletion pp_hot.c
Original file line number Diff line number Diff line change
Expand Up @@ -5505,7 +5505,7 @@ PP(pp_subst)
However, I suspect it isn't worth the complexity of
unravelling the C<goto force_it> for the small number of
cases where it would be viable to drop into the copy code. */
TARG = sv_2mortal(newSVsv(TARG));
TARG = sv_mortalcopy_flags(TARG, SV_GMAGIC|SV_NOSTEAL);
}
orig = SvPV_force_nomg(TARG, len);
goto force_it;
Expand Down
4 changes: 2 additions & 2 deletions regcomp.c
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ S_concat_pat(pTHX_ RExC_state_t * const pRExC_state,
} else {
/* a string with no trailing null, we need to copy it
* so it has a trailing null */
pat = sv_2mortal(newSVsv(msv));
pat = sv_mortalcopy_flags(msv, SV_GMAGIC|SV_NOSTEAL);
}
}

Expand Down Expand Up @@ -16065,7 +16065,7 @@ S_handle_names_wildcard(pTHX_ const char * wname, /* wildcard name to match */
must_sv = re_intuit_string(subpattern_re);
if (must_sv) {
/* regexec.c can free the re_intuit_string() return. GH #17734 */
must_sv = sv_2mortal(newSVsv(must_sv));
must_sv = sv_mortalcopy_flags(must_sv, SV_GMAGIC|SV_NOSTEAL);
must = SvPV(must_sv, must_len);
}
else {
Expand Down
2 changes: 1 addition & 1 deletion toke.c
Original file line number Diff line number Diff line change
Expand Up @@ -1842,7 +1842,7 @@ Perl_validate_proto(pTHX_ SV *name, SV *proto, bool warn, bool curstash)
: pv_pretty(tmpsv, p, origlen, 60, NULL, NULL, PERL_PV_ESCAPE_NONASCII);

if (curstash && !memchr(SvPVX(name), ':', SvCUR(name))) {
SV *name2 = sv_2mortal(newSVsv(PL_curstname));
SV *name2 = sv_mortalcopy_flags(PL_curstname, SV_GMAGIC|SV_NOSTEAL);
sv_catpvs(name2, "::");
sv_catsv(name2, (SV *)name);
name = name2;
Expand Down
2 changes: 1 addition & 1 deletion universal.c
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,7 @@ XS(XS_re_regexp_pattern)
} else {
/* Scalar, so use the string that Perl would return */
/* return the pattern in (?msixn:..) format */
pattern = sv_2mortal(newSVsv(MUTABLE_SV(re)));
pattern = sv_mortalcopy_flags(MUTABLE_SV(re), SV_GMAGIC|SV_NOSTEAL);
PUSHs(pattern);
XSRETURN(1);
}
Expand Down

0 comments on commit 54858f1

Please sign in to comment.