Skip to content
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

sv_setsv_cow: only succeed if sv_setsv() would also COW #22120

Draft
wants to merge 4 commits into
base: blead
Choose a base branch
from

Conversation

tonycoz
Copy link
Contributor

@tonycoz tonycoz commented Apr 4, 2024

This improved performance of the test case I was using from:

real    1m7.690s
user    0m2.593s
sys     1m4.984s

to

real    0m0.576s
user    0m0.453s
sys     0m0.093s

on cygwin.

The problem that is fixed here is that sv_setsv_cow() would COW SVs even though they had a very short string in a very large buffer, which had some sort of unclear to me interaction with the Win32 virtual memory system that had a painful performance impact when the large SV was made COW.

It may also improve memory handling on non-Win32 systems, since the SV was made COW, other SV copying functions wouldn't check if it was suitable for COW, so further copies would retain a COW reference for the large buffer. In the example test case from #21877 if we pushed the SV to an array after a successful match memory use would balloon out, running even a system with a generous amount of memory, out of memory.

Fixes part of #21877

This will actually fail in some cases in the next commit.
Previously if you had a successful match against an SV with a
SvLEN() large relative to the SvCUR() the regexp engine would
use sv_setsv_cow() to make a COW copy of the matched SV,
extending the life of the large allocation buffer.

A normal sv_setsv() normally didn't do such a COW copy, but the above
also marked the source SV as COW, so further copies of the SV
could even further extend the lifetime of the buffer, eg:

  while (<>) { # readline tends to make large SvLEN()
    /something/; # some sort of match
    push @save, $_; # with a successful match, the large $_ buffer
                    # survives until @save is released
  }

Fixes part of Perl#21877
@tonycoz tonycoz requested a review from demerphq April 4, 2024 04:57
@tonycoz tonycoz marked this pull request as draft April 7, 2024 23:01
Copy link
Contributor

@iabyn iabyn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks plausible. It would be nice for the sv_setsv_cow() fn to have a few lines of code comments at the top explaining what it's for/does.

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.

None yet

2 participants