develooper Front page | perl.perl5.porters | Postings from November 2000

[PATCH: perl@7825] SvTEMP-ness on rhs of aassign can wreak havoc

Thread Next
From:
Benjamin Holzman
Date:
November 25, 2000 18:09
Subject:
[PATCH: perl@7825] SvTEMP-ness on rhs of aassign can wreak havoc
Message ID:
20001125211014.B25040@ecnvantage.com
Pursuant to bug 20000212.002, and the following test case:

	sub foo { "a" } @foo=(foo())[0,0];

which in at least versions 5.005_03, 5.6.0 and 5.7.0+DEVEL7825 results in
$foo[1] being undef, and not "a", I offer the attached patch.

The patch turns off SvTEMP on the rhs of any aassign.  While I figure that
Spider is probably right in <200002130317.WAA18009@Orb.Nashua.NH.US>:

	The rest of the claims aren't strictly required.  Change the '-1'
	above to '1' and it fails the same way.  The problem is that the
	function return of a list gets transmogrified into a list of
	temps in leavesub, but the slice doesn't care that it gets the
	*same* temp sv on the stack twice.  The aasign op then consumes
	the temp by 'stealing' from it, and then has the now-empty temp
	for the next assignment in its loop.  Oops.

	I'm not sure whether the right answer here so to have the lslice
	op notice when it puts a temp on the stack twice, and to clone it
	if so, or whether the aassign should avoid temp-stealing on its
	embedded assignments.  Either way, I suspect that if I worked at
	it I could find some other way that temp-stealing breaks a
	reasonable assumption.  I'm starting to think that it might not
	have been such a great optimisation after all.

and there are other ways that the temp-optimization can fail, at least this
fixes a bunch of them.  I tried running the following to see if there
is any related performance penalty:

	use Benchmark;
	eval 'sub foo { undef, "'.('a'x1_000_000).'" }';
	timethis(1000, '@foo{1}=(foo())[0]');

but I couldn't detect any difference.

The same fix also works on 5.005_03, but not on 5.6.0; still not sure why.

Ben

-- 
Benjamin Holzman				ECNvantage Corp.
Chief Technical Officer 			295 Park Avenue S., Suite 7C
(212) 358-0436 : bah@ecnvantage.com		New York, NY, 10010
$ perl -le 'print join $" ,reverse map ucfirst ,qw{ hacker perl another just}'

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