Skip to content

Commit ce87404

Browse files
committed
S_fold_constants: remove early SvREADONLY(sv) to allow SvIsCOW(sv)
Standard CONST PVs have the IsCOW flag set, meaning that COW can be used when assigning the CONST to a variable, rather than making a copy of the buffer. CONST PVs arising from constant folding have been lacking this flag, leading to unnecessary copying of PV buffers. This seems to have occurred because a common branch in S_fold_constants marks SVs as READONLY before the new CONST OP is created. When the OP is created, the Perl_ck_svconst() check function is called - this is the same as when a standard CONST OP is created. If the SV is not already marked as READONLY, the check function will try to set IsCOW if it is safe to do so, then in either case will make sure that the READONLY flag is set. This commit therefore removes the SvREADONLY(sv) statement from S_fold_constants(), allowing Perl_ck_svconst() to set the IsCOW and READONLY flags itself. Minor test updates are also included.
1 parent ef4a769 commit ce87404

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

ext/Devel-Peek/t/Peek.t

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ do_test('string with Unicode',
551551
. '"\\\0 \[UTF8 "\\\x\{100\}\\\x\{0\}\\\x\{200\}"\]
552552
CUR = 5
553553
LEN = \\d+
554-
COW_REFCNT = 1 # $] < 5.019007
554+
COW_REFCNT = 1 # $] < 5.019007 || $] >=5.039000
555555
');
556556

557557
do_test('reference to hash containing Unicode',
@@ -575,7 +575,7 @@ do_test('reference to hash containing Unicode',
575575
PV = $ADDR "' . $cp200_bytes . '"\\\0 \[UTF8 "\\\x\{200\}"\]
576576
CUR = 2
577577
LEN = \\d+
578-
COW_REFCNT = 1 # $] < 5.019007
578+
COW_REFCNT = 1 # $] < 5.019007 || $] >=5.039000
579579
', '',
580580
$] >= 5.015
581581
? undef

op.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4980,7 +4980,9 @@ S_fold_constants(pTHX_ OP *const o)
49804980
SvPADTMP_off(sv);
49814981
else if (!SvIMMORTAL(sv)) {
49824982
SvPADTMP_on(sv);
4983-
SvREADONLY_on(sv);
4983+
/* Do not set SvREADONLY(sv) here. newSVOP will call
4984+
* Perl_ck_svconst, which will do it. Setting it early
4985+
* here prevents Perl_ck_svconst from setting SvIsCOW(sv).*/
49844986
}
49854987
newop = newSVOP(OP_CONST, 0, MUTABLE_SV(sv));
49864988
if (!is_stringify) newop->op_folded = 1;

t/op/undef.t

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,9 @@ SKIP: {
165165
my $out = runperl(stderr => 1,
166166
progs => [ split /\n/, <<'EOS' ]);
167167
require Devel::Peek;
168-
my $f = q(x) x 40; $f = undef;
168+
my $f = q(x) x 40;
169+
chop $f; # Make sure that the PV buffer is not COWed
170+
$f = undef;
169171
Devel::Peek::Dump($f);
170172
undef $f;
171173
Devel::Peek::Dump($f);

0 commit comments

Comments
 (0)