Perl and Moose internals: continuous folding optimization

I was curious how the constant bends Perl performs are optimized, but it happened that when the code has Moose, there is a chance that constant folding will not work (please correct me if I'm wrong).

I have Moose code that contains a method as shown below:

sub foo { my ($self) = shift; my $test_y = $self->pos->[1]; #... if ($self->is_map_val($self->pos->[0]+8, $test_y+32) || $self->is_map_val($self->pos->[0]+32-8, $test_y+32)) { { heavy_stuff(); } #... } 

and when I run perl -MO=Deparse ./program.pl , I get an almost identical line of code:

 if ($self->is_map_val($self->pos->[0] + 8, $test_y + 32) or $self->is_map_val($self->pos->[0] + 32 - 8, $test_y + 32)) { heavy_stuff(); } 

I wonder why Perl did not optimize 32-8 as 24 ? Are there any real reasons Perl didn't do this (maybe the Moose subsystem makes life harder?).

If this helps, I run Perl (v.5.14.2)

+4
source share
1 answer

This has nothing to do with Musa. IN

 $x + 32 - 8 

evaluation order is equivalent

 ($x + 32) - 8 

(i.e. + and - have the same priority level and are left-associative). Like a tree:

  (-) / \ (+) 8 / \ $x 32 

No part of this syntax tree has only constant nodes: $x + 32 not a constant, and PREVIOUS_PART - 8 also not a constant. Therefore, the constant folding (which works only at this level of the tree and cannot change the order of parts of the tree) does not see any chances for optimization.

You will get reordering optimization at 32 - 8 + $x .

perlguts is a constant crease of a document and, in particular, states that it works by replacing parts of a tree.

+7
source

Source: https://habr.com/ru/post/1500197/


All Articles