Skip to content

pp_reverse: don't COW buffer just to then un-COW it #22729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 12, 2024

Conversation

richardleach
Copy link
Contributor

When it has a single argument, scaler-mode pp_reverse copies the source PV buffer to the destination PV buffer via the sv_setsv() macro. This expands to:

    sv_setsv_flags(dsv, ssv, SV_GMAGIC|SV_DO_COW_SVSETSV)

The SV_DO_COW_SVSETSV flag means that Perl_sv_setsv_flags will try to achieve the "copy" via these mechanisms:

  • Swipe the source buffer (if possible)
  • COW the buffer (if possible)
  • Copy the buffer

However, using COW makes no sense because pp_reverse is going to then modify the buffer. Almost immediately after the sv_setsv call, any COWing will be reversed by the call to SvPV_force and a full copy of the buffer taken. So this commit expands the sv_setsv call and drops the COW flag.


  • This set of changes does not require a perldelta entry.

@richardleach
Copy link
Contributor Author

PS Doing a full copy also seems like not the best idea, since it read-writes the entire string (albeit between buffers), only for the function to then read-write the whole buffer at least once.

I have a branch in progress to separate out the "swipe" case from the "copy" case, where in the "copy" case the buffer isn't actually copied wholesale, instead the reverse operation reads from the source buffer and writes to the destination buffer.

Seems worth making this PR in case the WIP branch never lands.

@tonycoz
Copy link
Contributor

tonycoz commented Nov 12, 2024

LGTM except for the "scaler" in the commit message.

When it has a single argument, scalar-mode `pp_reverse` copies the
source PV buffer to the destination PV buffer via the `sv_setsv()`
macro. This expands to:

        sv_setsv_flags(dsv, ssv, SV_GMAGIC|SV_DO_COW_SVSETSV)

The `SV_DO_COW_SVSETSV` flag means that Perl_sv_setsv_flags will
try to achieve the "copy" via these mechanisms:
* Swipe the source buffer (if possible)
* COW the buffer (if possible)
* Copy the buffer

However, using COW makes no sense because pp_reverse is going to
then modify the buffer. Almost immediately after the `sv_setsv`
call, any COWing will be reversed by the call to `SvPV_force` and
a full copy of the buffer taken. So this commit expands the
`sv_setsv` call and drops the COW flag.
@richardleach richardleach force-pushed the hydahy/reverse_nocowuncow branch from 82def65 to 3c6c441 Compare November 12, 2024 09:21
@richardleach richardleach merged commit 08223ba into Perl:blead Nov 12, 2024
@richardleach richardleach deleted the hydahy/reverse_nocowuncow branch November 12, 2024 10:48
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