in reply to Re^3: Access via substr refs 2000 times slower
in thread Access via substr refs 2000 times slower
It's not related to COW. For example, let's look at the following snippet from (the arbitrarily chosen) pp_concat function ("." operator):
else { /* TARG == left */ STRLEN llen; SvGETMAGIC(left); /* or mg_get(left) may happen here */ if (!SvOK(TARG)) { if (left == right && ckWARN(WARN_UNINITIALIZED)) report_uninit(right); sv_setpvn(left, "", 0); } (void)SvPV_nomg_const(left, llen); /* Needed to set UTF8 flag */ lbyte = !DO_UTF8(left); if (IN_BYTES) SvUTF8_off(TARG); }
SvGETMAGIC(left) is what calls the 'get' magic and stores the result in the SV if the LHS is magical. If it didn't work that way, the following sv_setpvn, SvPV_nomg_const and DO_UTF8 would all have to be handled by the magic. It doesn't make sense to have every type of magic and every tied variable handle every perlapi and internal Perl function that might affect it.
You could shorten the life of the copy to where it's needed, but Perl doesn't even do that for lexical variables. Their PV remains allocated when the variable is out of scope.
>perl -MDevel::Peek -e"sub f { my $s; Dump $s if $i++; $s='abc' } f;f" SV = PV(0x238e44) at 0x182ee40 REFCNT = 1 FLAGS = (PADBUSY,PADMY) PV = 0x18250bc "abc"\0 CUR = 3 LEN = 4
|
|---|