Skip to content

Commit 28f0091

Browse files
committed
Perl_leave_scope - sv_backoff shouldn't do an unnecessay string copy
When a `my` SV goes out of scope, any OOK hack on its string buffer is undone by `Perl_sv_backoff`. If the SV is `SvOK`, a copy of the buffer contents will occur, but since the contents are defunct at this point, the copy is unnecessary. See #23967 as an example of where this unnecessary copy had a noticeable effect on performance. This commit adds some flag meddling either side of the `sv_backoff` call to avoid the copy but otherwise preserve the SV's flags.
1 parent 789b63c commit 28f0091

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

scope.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1440,9 +1440,20 @@ Perl_leave_scope(pTHX_ I32 base)
14401440

14411441
if (SvTYPE(sv) == SVt_PVHV && HvHasAUX(sv))
14421442
Perl_hv_kill_backrefs(aTHX_ MUTABLE_HV(sv));
1443-
else if(SvOOK(sv))
1443+
else if(SvOOK(sv)) {
1444+
/* sv_backoff will copy the buffer contents when SvOK(sv),
1445+
even though this particular buffer is defunct and copying
1446+
its contents is a waste of time. It might not (?) be
1447+
desirable to remove any SvOK flags just yet, so we fake it.*/
1448+
U32 savedflags = SvFLAGS(sv);
1449+
savedflags &=~ SVf_OOK;
1450+
SvFLAGS(sv) &=~ (SVf_OK);
1451+
14441452
sv_backoff(sv);
14451453

1454+
SvFLAGS(sv) = savedflags;
1455+
}
1456+
14461457
if (SvMAGICAL(sv)) {
14471458
/* note that backrefs (either in HvAUX or magic)
14481459
* must be removed before other magic */

0 commit comments

Comments
 (0)