develooper Front page | perl.perl5.porters | Postings from May 2006

Re: [perl #3306] DESTROY doesn't get called.

Thread Previous | Thread Next
From:
Dave Mitchell
Date:
May 6, 2006 15:55
Subject:
Re: [perl #3306] DESTROY doesn't get called.
Message ID:
20060506225529.GB3317@iabyn.com
On Mon, May 01, 2006 at 07:55:21PM +0100, Dave Mitchell wrote:
> I should imagine it's copying the scratchpad that takes most of the time.
> Hmm, perhaps we could just share the scratchpads for non-closure anon
> subs?

I've knocked up a proof-of-concept patch (it fails loads of tests, but
works for basic code).

The timings I get are as follows:

orig perl (share CV and scratchpad):

    0m2.292s	sub {} for 1..10_000_000

hacked perl (copy CV, but share scratchpad):

    0m6.564s	sub {} for 1..10_000_000

orig perl, but full closure (CV and scratchpad copied):

    0m18.229s	my $x; sub {$x} for 1..10_000_000

However, there's a problem with copying the CV while sharing the pad:
If one of the sub calls another copy of the sub, then they share the
same set of lexicals and temps. The following contrived example shows
how the inner call corrupts the outer sub's $x:

    push @s, sub { my $x=1; $s[0] && &{pop @s}; print "x=$x\n" } for 1,2;
    &{pop @s};

which gives:

    x=1
    x=

whereas in normal perl it gives

    x=1
    x=1

this is becuase with the CV being shared, they share CvDEPTH and thus
when one sub calls the other, CvDEPTH gets incremented and so they use
different levels in the pad.

-- 
But Pity stayed his hand. "It's a pity I've run out of bullets",
he thought. - "Bored of the Rings"

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About