Front page | perl.perl5.porters |
Postings from May 2003
Re: slice autoextending ? still another revised doc patch; COW
Thread Previous
|
Thread Next
From:
Nicholas Clark
Date:
May 15, 2003 15:24
Subject:
Re: slice autoextending ? still another revised doc patch; COW
Message ID:
20030515232416.H1149@plum.flirble.org
On Wed, May 14, 2003 at 05:04:30PM -0500, david nicol wrote:
> In a context of talking about slices, I think I was imagining using COW
> to optimize away the copying of the data within the array by hiding
> aliases to the first array.
>
> @second = splice @first, $offset, $length
>
> does, I believe with a less than 100% confidence, do an elementwise
> copy of the desired items -- a fetch on each, and a store. If @second
> was some kind of magic array that hides the fact that it stores
> references instead of values until fetching time, the FETCHes could
> be deferred.
Sorry that the 1 liner is rather long. The map is a hack to make a copy
of @ARGV that is PVIV (and hence COW-able)
$^D = 2097152; turns on -DC for the splice only:
$ ./perl -wle '@first = map {$a=1;$a=$_} @ARGV; $^D = 2097152; @second = splice @first, 1, 2; $^D = 0; print foreach @second' a b c d
Copy on write: sstr --> dstr
SV = PVIV(0x816fee0) at 0x817c144
REFCNT = 1
FLAGS = (TEMP,POK,pPOK)
IV = 135723056
PV = 0x8174588 "b"\0
CUR = 1
LEN = 2
SV = PVIV(0x816fe50) at 0x816f740
REFCNT = 1
FLAGS = ()
IV = 0
PV = 0
Copy on write: sstr --> dstr
SV = PVIV(0x816fef0) at 0x817c150
REFCNT = 1
FLAGS = (TEMP,POK,pPOK)
IV = 135723116
PV = 0x817cc20 "c"\0
CUR = 1
LEN = 2
SV = PVIV(0x816fe60) at 0x816f830
REFCNT = 1
FLAGS = ()
IV = 0
PV = 0
Copy on write: clear
SV = PVIV(0x816fef0) at 0x817c150
REFCNT = 0
FLAGS = (POK,FAKE,READONLY,pPOK)
UV = 135723056 (COW from 0x816f830)
PV = 0x817cc20 "c"\0
CUR = 1
LEN = 2
Copy on write: clear
SV = PVIV(0x816fee0) at 0x817c144
REFCNT = 0
FLAGS = (POK,FAKE,READONLY,pPOK)
UV = 135722816 (COW from 0x816f740)
PV = 0x8174588 "b"\0
CUR = 1
LEN = 2
b
c
So elements 1 and 2 (b and c) are getting COW-ed from @first to @second
as the first part of the splice, and then cleared in @first.
At least, that's what I infer is going on. I've not prodded at the internals;
all I know from prior poking is that SVs get copied in exactly one
function - sv_setsv (now Perl_sv_setsv_flags). So whatever COW that
can achieve, every copy will benefit.
Strictly it's "got", not "get", as I added Perl_sv_setsv_cow for the
regexp engine, that is a bit more aggressive (it upgrades PV to PVIV
if needed, sv_setsv doesn't).
I've nothing decent to benchmark with, so I don't know if it would be
better to add the aggressive upgrade to sv_setsv, or to drop the
aggressive upgrade, or to drop Perl_sv_setsv_cow and make the regexp
engine go back to call sv_setv.
Nicholas Clark
Thread Previous
|
Thread Next