develooper Front page | perl.perl5.porters | Postings from April 2003

Re: COW in CORE and SvFAKE in older Perl

Thread Previous | Thread Next
Nicholas Clark
April 30, 2003 15:58
Re: COW in CORE and SvFAKE in older Perl
Message ID:
On Tue, Apr 29, 2003 at 06:29:32PM -0400, Aaron Sherman wrote:
> On Tue, 2003-04-29 at 16:12, Nicholas Clark wrote:

> some interesting efficiency tricks which are very useful, but result in
> massive memory duplication (which is why a 250Kb message is by default
> the largest that it will process, and why SA is a useless tool for most
> virus-checking).

Ah. I'm not sure which situations copy on write will give you a win over
simple passing a scalar reference and dereferencing it as needed. There
doesn't seem to be any copying for that for the simple test that I did:

./perl -DC -le 'chop ($a = 0); $a = "Pie"; $b = \$a; print "Hit" if $$b =~ /i/'

> I'll check out the rest of your suggestions as well when I have a
> chance. Thanks again for the wonderfully detailed reply!

Well, I'd like someone to find something useful that justifies it.

There's a -DC debugging flag to show what the copy on write code is doing.
It actually copies all strings, however short. There is probably scope for
tuning this to only copy long things. Also, currently the basic copy code
doesn't upgrade anything aggressively. It can only copy on write if the
source is at least PVIV already (with no IV or NV slot in use)

./perl -DC -le '$a = "Pie"; $b = $a'
[snip stuff during compilation]


whereas if I force $a to be a PVIV:

./perl -DC -le 'chop ($a = 0); $a = "Pie"; $b = $a'


Copy on write: sstr --> dstr
SV = PVIV(0x192ec0) at 0x1b3404
  REFCNT = 1
  IV = 0
  PV = 0x1965a0 "Pie"\0
  CUR = 3
  LEN = 4
SV = PVIV(0x192ed0) at 0x1b3440
  REFCNT = 1
  FLAGS = ()
  IV = 0
  PV = 0

(and setting PERL_DESTRUCT_LEVEL=2 adds this to the end:

Copy on write: clear
SV = PVIV(0x192ec0) at 0x1b3288
  REFCNT = 0
  UV = 1782468  (COW from 0x1b32c4)
  PV = 0x1965a0 "Pie"\0
  CUR = 3
  LEN = 4

The COW code for $& (and $1 etc) is a bit more aggressive, in that it will
promote a plain PV to PVIV if needs be. Possibly the standard code should
do to, but maybe only if the source is long. (For some value of long)

I don't know. There are no good benchmarks to test this kind of thing.

Nicholas Clark

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About