Skip to content

Conversation

@richardleach
Copy link
Contributor

Previously, Perl_sv_backoff would always copy the contents of the offset
string buffer to the start of the buffer when undoing the OOK hack. When
the contents are defunct, this is unnecessary activity.

This PR makes two changes:

  • Perl_sv_backoff now only copies when SvOK(sv) is true.
  • Perl_leave_scope fakes !SvOK(sv) when a lexical SV goes out of scope and sv_backoff is called.

Closes GH #23967

Note:The main assumption made here is that SvOK(sv) is a good proxy for buffer content liveness. It seems reasonable for core, but who knows what people have done in their own XS code.

A lesser worry: Perhaps Perl_leave_scope doesn't need the SV flags preserving and it'd be okay to toggle the SvOK flags off and keep them off.


  • This set of changes requires a perldelta entry, and I'll add one shortly.

When undoing an OOK, `Perl_sv_backoff` will always copy the shifted buffer
contents down to the start of the buffer. That's required when the contents
are live, unfortunate when the contents are defunct but tiny, and can have a
noticeably bad effect on performance when the contents are defunct but large.

See Perl#23967 for an example of the latter.

With this commit the copy will only occur if `SvOK(sv)`. This test is expected
to indicate whether the buffer contents are active or defunct.
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 Perl#23967 as an example of where
this unnecessary copy had a noticeable effect on performance.

This commit essentially inlines the necessary parts of `sv_backoff` to
avoid the copy, without excessive messing around with `sv`'s flags at
the call site in `Perl_leave_scope`.
@richardleach richardleach merged commit c8c2426 into Perl:blead Dec 3, 2025
33 checks passed
@richardleach richardleach deleted the sv_backoff-nocopy branch December 3, 2025 00:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants