Perl / perl5

🐪 The Perl programming language
https://dev.perl.org/perl5/
Other
1.85k stars 524 forks source link

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

Open tonycoz opened 4 weeks ago

tonycoz commented 4 weeks ago

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