|
| 1 | +#!perl |
| 2 | +use strict; |
| 3 | +use warnings; |
| 4 | +use XS::APItest; |
| 5 | +use B; |
| 6 | + |
| 7 | +use Test::More tests => 11; |
| 8 | + |
| 9 | +{ |
| 10 | + # github #21877 |
| 11 | + # the regexp engine would COW an SV that had a large |
| 12 | + # SvLEN() in cases where sv_setsv() wouldn't. |
| 13 | + # This led to some surprises. |
| 14 | + # - On cywgin this produced some strange performance problems |
| 15 | + # - In general it meant the (large) buffer of the SV remained |
| 16 | + # allocated for longer than it otherwise would. |
| 17 | + # Also, since the SV became CoW, further copies would also |
| 18 | + # be CoW, for example, code like: |
| 19 | + # |
| 20 | + # while (<>) { # sv_getsv() currently allocates a large-ish buffer |
| 21 | + # /regex that (captures)/; # CoW large buffer |
| 22 | + # push @save, $_; # copy in @save still has that large buffer |
| 23 | + # } |
| 24 | + my $x = "Something\n" x 1000; |
| 25 | + cmp_ok(length $x, '>=', 1250, |
| 26 | + "need to be at least 1250 to be COWed"); |
| 27 | + sv_grow($x, 1_000_000); |
| 28 | + my $ref = B::svref_2object(\$x); |
| 29 | + cmp_ok($ref->LEN, '>=', 1_000_000, |
| 30 | + "check we got it longer"); |
| 31 | + ok(!SvIsCOW($x), "not cow before"); |
| 32 | + is($ref->REFCNT, 1, "expected reference count"); |
| 33 | + ok($x =~ /me(.)hing/, "match"); |
| 34 | + ok(!SvIsCOW($x), "not cow after"); |
| 35 | + |
| 36 | + # make sure reasonable SVs are COWed |
| 37 | + my $y = "Something\n" x 1000; |
| 38 | + sv_force_normal($y); |
| 39 | + cmp_ok(length $y, '>=', 1250, |
| 40 | + "need to be at least 1250 to be COWed"); |
| 41 | + my $ref2 = B::svref_2object(\$y); |
| 42 | + ok(!SvIsCOW($y), "not cow before"); |
| 43 | + is($ref2->REFCNT, 1, "expected reference count"); |
| 44 | + ok($y =~ /me(.)hing/, "match"); |
| 45 | + ok(SvIsCOW($y), "is cow after"); |
| 46 | +} |
0 commit comments