Linux uses COW to keep memory usage low after a fork, but the way Perl 5 variables work in perl
seems to defeat this optimization. For instance, for the variable:
my $s = "1";
perl
is really storing:
SV = PV(0x100801068) at 0x1008272e8
REFCNT = 1
FLAGS = (POK,pPOK)
PV = 0x100201d50 "1"\0
CUR = 1
LEN = 16
When you use that string in a numeric context, it modifies the C struct
representing the data:
SV = PVIV(0x100821610) at 0x1008272e8
REFCNT = 1
FLAGS = (IOK,POK,pIOK,pPOK)
IV = 1
PV = 0x100201d50 "1"\0
CUR = 1
LEN = 16
The string pointer itself did not change (it is still 0x100201d50
), but now it is in a different C struct
(a PVIV
instead of a PV
). I did not modify the value at all, but suddenly I am paying a COW cost. Is there any way to lock the perl
representation of a Perl 5 variable so that this time saving (perl
doesn't have to convert "0"
to 0
a second time) hack doesn't hurt my memory usage?
Note, the representations above were generated from this code:
perl -MDevel::Peek -e '$s = "1"; Dump $s; $s + 0; Dump $s'