develooper Front page | perl.perl5.porters | Postings from July 2010

RE: [perl #76438] peephole optimiser could prune more dead code

Thread Previous | Thread Next
From:
Jan Dubois
Date:
July 20, 2010 15:47
Subject:
RE: [perl #76438] peephole optimiser could prune more dead code
Message ID:
010601cb285d$77c28f20$6747ad60$@activestate.com
Eirik Berg Hanssen wrote:
> On Tue, Jul 20, 2010 at 10:12 PM, Jan Dubois <jand@activestate.com> wrote:
>> And could you also explain why it makes sense that $a.$a has to invoke
>> magic twice, while $a x 2 will only call it once?
>
>  For the same reason that f().f() calls &f twice, while f() x 2 will only call it once?

Yes, but f() may have side-effects, whereas $a shouldn't have any (IMO).

As I wrote earlier to Nicholas, FETCH may be called more than you expect
anyways, which already implicitly forbids side effect (unless you
consider calling it more than strictly needed a bug):

-----------------------------------------------------
sub foo::TIESCALAR { bless \my $x => "foo" }
sub foo::FETCH { print "FETCH\n"; ${$_[0]} }
sub foo::STORE { print "STORE\n"; ${$_[0]} = $_[1] }

tie $a, "foo";

print "NV\n";  $a = 1.;
print "Inc\n"; ++$a;
print "IV\n";  $a = 1;
print "Inc\n"; ++$a;
-----------------------------------------------------
NV
STORE
Inc
FETCH
FETCH
STORE
IV
STORE
Inc
FETCH
STORE
-----------------------------------------------------

So FETCH is called twice when $a is a floating point number and only
once when it is an integer.

But even assuming there is a canonical number of times FETCH should
be called, what is that number for "$b = $a++;"?  Should it be 2, because
the expression is just a shorthand for "$b = $a; $a = $a + 1;"?  Or is
it fine to allow Perl to optimize this into a single access?

perlop.pod implies that there should be 2 accesses:

       "++" and "--" work as in C.  That is, if placed before a variable, they
       increment or decrement the variable by one before returning the value,
       and if placed after, increment or decrement after returning the value.

There is nothing there stating that the returned value can be re-used to
increment the variable, so by your logic it will have to be fetched again.
Which is not how it is currently implemented (reasonably again, IMO).

Cheers,
-Jan


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