develooper Front page | perl.perl5.porters | Postings from February 2016

[perl #121977] COWification seems expensive in PADMY variables

Thread Next
From:
l.mai@web.de via RT
Date:
February 26, 2016 18:34
Subject:
[perl #121977] COWification seems expensive in PADMY variables
Message ID:
rt-4.0.18-7917-1456511690-916.121977-15-0@perl.org
On Thu Jun 05 08:16:02 2014, davem wrote:
> On Thu, May 29, 2014 at 12:22:42PM +0200, Leon Timmermans wrote:
> > > This is perl 5, version 21, subversion 1 (v5.21.1 (v5.21.0-69-
> > > g0fadf2d))
> > > built for x86_64-linux
> > > $ ./perl -Ilib ../p5p/121977-leont-cow.pl 300000
> > >          Rate lvalue    ref simple
> > > lvalue 8460/s     --    -1%    -1%
> > > ref    8535/s     1%     --    -0%
> > > simple 8574/s     1%     0%     --
> > >          Rate simple lvalue    ref
> > > simple 4228/s     --   -49%   -49%
> > > lvalue 8299/s    96%     --    -1%
> > > ref    8366/s    98%     1%     --
> > >
> > >
> > Those are the results I was expecting.
> 
> i.e. that although COW has made some things faster in 5.20.0 compared
> with
> 5.18, the 'simple' case hasn't seen the speedup seen by the other
> cases.
> 
> The commit below fixes the proximate cause. However, there were three
> things interacting with each other that together caused the issue.
> My commit fixes one of the 3 issues, and is enough to boost
> performance
> for this use case. Two other issues that I have not yet addressed are:
> 
> ('x' x 1_000_000) is constant folded at compile time, and the COW code
> in sv_setsv_flags() is failing to do COW on something like
> 
> $buf = 'x' x 1_000_000;
> 
> and is copying instead. I think FC did some work on making COW work
> with
> RO values, so perhaps this is something that should work. Perhaps
> string constants need to be marked as COW (with RC==0) before making
> them read-only at compile time???
> 
> The second issue is that, to work around the problem with readline
> allocating a large buffer, which then got COWed and 'donated' in
> something
> like
> 
> push @a, $_ while <>;
> 
> we added a heuristic along the lines of 'copy rather than COW' if
> SvCUR * A < SvLEN for some constant factor A. The trouble is, this is
> clashing with sv_grow()'s
> 
> SvLEN = SvCUR * B;
> 
> for some fudge factor B (i.e. over-allocate when growing the buffer).
> 
> If B > A, we end up creating strings that can't be COWed. So we
> probably
> need to harmonise the constants involved in A and B.
> 
> Anyway, here's my commit:
> 
> commit a7ab896004fe7cc32eeddadf760d0829e9fed13d
> Author:     David Mitchell <davem@iabyn.com>
> AuthorDate: Thu Jun 5 15:03:32 2014 +0100
> Commit:     David Mitchell <davem@iabyn.com>
> CommitDate: Thu Jun 5 15:03:32 2014 +0100
> 
> when unCOWing a string, set SvCUR to 0
> 
> When a COW string is unCOWed, as well as setting SvPVX to NULL and
> SvLEN
> to 0, set SvCUR to 0 too.
> 
> This is to avoid a later SvGROW on the same using the old SvCUR()
> value
> to calculate a roundup to the buffer size.
> 
> Consider the following code:
> 
> use Devel::Peek;
> for (1..3) {
>     my $t;
>     my $s = 'x' x 100;
>     $t = $s;
>     Dump $s;
> }
> 
> Looking at the LEN line of the Dump output, we got on 5.20.0:
> 
> LEN = 102
> LEN = 135
> LEN = 135
> 
> and after this commit,
> 
> LEN = 102
> LEN = 102
> LEN = 102
> 
> As well as wasting space, this extra LEN was then triggering the 'skip
> COW
> if LEN >> CUR' mechanism, causing extra copies. See:
> 
> [perl #121977] COWification seems expensive in PADMY variables

This ticket is listed in perl5201delta. Is there still work happening here?


---
via perlbug:  queue: perl5 status: open
https://rt.perl.org/Ticket/Display.html?id=121977

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About