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

Re: Why isn't COW used here?

Thread Previous
From:
Dave Mitchell
Date:
September 16, 2016 22:37
Subject:
Re: Why isn't COW used here?
Message ID:
20160916223741.GB3294@iabyn.com
On Thu, Sep 15, 2016 at 11:16:08AM -0400, Eric Brine wrote:
> Is there a reason COW isn't used here?
> 
> $ perl -MDevel::Peek -e'
>    my $x; $x .= "x" for 1..100;
>    substr($x, 50, length($x), "") if $ARGV[0];
> 
>    Dump($x);
>    my $y = $x;
>    Dump($x);
>    Dump($y);
> ' 1
> SV = PV(0x1886450) at 0x18a30e8
>   REFCNT = 1
>   FLAGS = (POK,pPOK)
>   PV = 0x18a57f0 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"\0
>   CUR = 50
>   LEN = 120
> SV = PV(0x1886450) at 0x18a30e8
>   REFCNT = 1
>   FLAGS = (POK,pPOK)
>   PV = 0x18a57f0 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"\0
>   CUR = 50
>   LEN = 120
> SV = PV(0x18864f0) at 0x18ae668
>   REFCNT = 1
>   FLAGS = (POK,pPOK)
>   PV = 0x18b5040 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"\0
>   CUR = 50
>   LEN = 52
> 
> COW is used if you remove the substr (by changing the script's argument
> from 1 to 0).

From comments in sv.c:

	 * If the rhs is not a copy-on-write string yet, then we also
	 * consider whether the buffer is too large relative to the string
	 * it holds.  Some operations such as readline allocate a large
	 * buffer in the expectation of reusing it.  But turning such into
	 * a COW buffer is counter-productive because it increases memory
	 * usage by making readline allocate a new large buffer the sec-
	 * ond time round.  So, if the buffer is too large, again, we use
	 * method 3 (copy).

-- 
Dave's first rule of Opera:
If something needs saying, say it: don't warble it.

Thread Previous


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