Subject: | Potential Speed up by utilizing CoW on >5.20 |
Perl >=5.20 now has copy-on-write strings, so on such a perl, if possible, it may be advantageous to utilize CoW strings where possible, both saving substantial amounts of memory and runtime performance penalties.
As you can see here, a Pure-Perl implementation of Clone gets this benefit automatically:
perl -MClone::PP=clone -MDevel::Peek -e 'my $x = q[ a ]; my $y = $x; print Dump $x; print Dump $y; my $z = { $y => $y }; print Dump clone $z; print Dump $y'
SV = PV(0x11d0d80) at 0x11f0d90
REFCNT = 1
FLAGS = (PADMY,POK,IsCOW,pPOK)
PV = 0x11d55d0 " a "\0
CUR = 3
LEN = 10
COW_REFCNT = 2
SV = PV(0x11d0e40) at 0x11f0cd0
REFCNT = 1
FLAGS = (PADMY,POK,IsCOW,pPOK)
PV = 0x11d55d0 " a "\0
CUR = 3
LEN = 10
COW_REFCNT = 2
SV = IV(0x11e5d50) at 0x11e5d60
REFCNT = 1
FLAGS = (TEMP,ROK)
RV = 0x11e5d48
SV = PVHV(0x11d7130) at 0x11e5d48
REFCNT = 1
FLAGS = (SHAREKEYS)
ARRAY = 0x11fc090 (0:7, 1:1)
hash quality = 100.0%
KEYS = 1
FILL = 1
MAX = 7
Elt " a " HASH = 0x5e7fc22b
SV = PV(0x121bd70) at 0x11f0dd8
REFCNT = 1
FLAGS = (POK,IsCOW,pPOK)
PV = 0x11d55d0 " a "\0
CUR = 3
LEN = 10
COW_REFCNT = 4
SV = PV(0x11d0e40) at 0x11f0cd0
REFCNT = 1
FLAGS = (PADMY,POK,IsCOW,pPOK)
PV = 0x11d55d0 " a "\0
CUR = 3
LEN = 10
COW_REFCNT = 3
However, the XS implementation seems to lose the COW benefits:
perl -MClone=clone -MDevel::Peek -e 'my $x = q[ a ]; my $y = $x; print Dump $x; print Dump $y; my $z = { $y => $y }; print Dump clone $z; print Dump $y'
SV = PV(0x1dedd80) at 0x1e0dd80
REFCNT = 1
FLAGS = (PADMY,POK,IsCOW,pPOK)
PV = 0x1df25d0 " a "\0
CUR = 3
LEN = 10
COW_REFCNT = 2
SV = PV(0x1dede40) at 0x1e0dcc0
REFCNT = 1
FLAGS = (PADMY,POK,IsCOW,pPOK)
PV = 0x1df25d0 " a "\0
CUR = 3
LEN = 10
COW_REFCNT = 2
SV = IV(0x1e02c68) at 0x1e02c78
REFCNT = 1
FLAGS = (TEMP,ROK)
RV = 0x1e02ca8
SV = PVHV(0x1df4230) at 0x1e02ca8
REFCNT = 1
FLAGS = (SHAREKEYS)
ARRAY = 0x1eb7330 (0:7, 1:1)
hash quality = 100.0%
KEYS = 1
FILL = 1
MAX = 7
Elt " a " HASH = 0x729f1c06
SV = PV(0x1deded0) at 0x1e02d38
REFCNT = 1
FLAGS = (POK,pPOK)
PV = 0x1eb7380 " a "\0
CUR = 3
LEN = 10
SV = PV(0x1dede40) at 0x1e0dcc0
REFCNT = 1
FLAGS = (PADMY,POK,IsCOW,pPOK)
PV = 0x1df25d0 " a "\0
CUR = 3
LEN = 10
COW_REFCNT = 3
As you can see here, a Pure-Perl implementation of Clone gets this benefit automatically:
perl -MClone::PP=clone -MDevel::Peek -e 'my $x = q[ a ]; my $y = $x; print Dump $x; print Dump $y; my $z = { $y => $y }; print Dump clone $z; print Dump $y'
SV = PV(0x11d0d80) at 0x11f0d90
REFCNT = 1
FLAGS = (PADMY,POK,IsCOW,pPOK)
PV = 0x11d55d0 " a "\0
CUR = 3
LEN = 10
COW_REFCNT = 2
SV = PV(0x11d0e40) at 0x11f0cd0
REFCNT = 1
FLAGS = (PADMY,POK,IsCOW,pPOK)
PV = 0x11d55d0 " a "\0
CUR = 3
LEN = 10
COW_REFCNT = 2
SV = IV(0x11e5d50) at 0x11e5d60
REFCNT = 1
FLAGS = (TEMP,ROK)
RV = 0x11e5d48
SV = PVHV(0x11d7130) at 0x11e5d48
REFCNT = 1
FLAGS = (SHAREKEYS)
ARRAY = 0x11fc090 (0:7, 1:1)
hash quality = 100.0%
KEYS = 1
FILL = 1
MAX = 7
Elt " a " HASH = 0x5e7fc22b
SV = PV(0x121bd70) at 0x11f0dd8
REFCNT = 1
FLAGS = (POK,IsCOW,pPOK)
PV = 0x11d55d0 " a "\0
CUR = 3
LEN = 10
COW_REFCNT = 4
SV = PV(0x11d0e40) at 0x11f0cd0
REFCNT = 1
FLAGS = (PADMY,POK,IsCOW,pPOK)
PV = 0x11d55d0 " a "\0
CUR = 3
LEN = 10
COW_REFCNT = 3
However, the XS implementation seems to lose the COW benefits:
perl -MClone=clone -MDevel::Peek -e 'my $x = q[ a ]; my $y = $x; print Dump $x; print Dump $y; my $z = { $y => $y }; print Dump clone $z; print Dump $y'
SV = PV(0x1dedd80) at 0x1e0dd80
REFCNT = 1
FLAGS = (PADMY,POK,IsCOW,pPOK)
PV = 0x1df25d0 " a "\0
CUR = 3
LEN = 10
COW_REFCNT = 2
SV = PV(0x1dede40) at 0x1e0dcc0
REFCNT = 1
FLAGS = (PADMY,POK,IsCOW,pPOK)
PV = 0x1df25d0 " a "\0
CUR = 3
LEN = 10
COW_REFCNT = 2
SV = IV(0x1e02c68) at 0x1e02c78
REFCNT = 1
FLAGS = (TEMP,ROK)
RV = 0x1e02ca8
SV = PVHV(0x1df4230) at 0x1e02ca8
REFCNT = 1
FLAGS = (SHAREKEYS)
ARRAY = 0x1eb7330 (0:7, 1:1)
hash quality = 100.0%
KEYS = 1
FILL = 1
MAX = 7
Elt " a " HASH = 0x729f1c06
SV = PV(0x1deded0) at 0x1e02d38
REFCNT = 1
FLAGS = (POK,pPOK)
PV = 0x1eb7380 " a "\0
CUR = 3
LEN = 10
SV = PV(0x1dede40) at 0x1e0dcc0
REFCNT = 1
FLAGS = (PADMY,POK,IsCOW,pPOK)
PV = 0x1df25d0 " a "\0
CUR = 3
LEN = 10
COW_REFCNT = 3