[Python-Dev] Suggest reverting today's checkin (recursive constant folding in the peephole optimizer)

Cesare Di Mauro cesare.di.mauro at gmail.com
Mon Mar 14 21:15:58 CET 2011


2011/3/14 Alexander Belopolsky <alexander.belopolsky at gmail.com>

> On Sat, Mar 12, 2011 at 1:08 PM, Raymond Hettinger
> <raymond.hettinger at gmail.com> wrote:
> > I would like to withdraw my suggestion for the recursive constant folding
> patch to be reverted.
>
> So what is the status of peephole optimization now?  Is it back under
> active development?   Let me quote a tracker comment that I posted two
> years ago and go no response to ('>'-quote are from Raymond's
> message):
>
> """
> Constant folding promotes more readable code: 24*60*60 is more obvious
> than 86400, prefixing positive numbers with + leads to better visual
> alignment, etc.  Users should not be required to think twice about
> which constant expressions are folded and which are not.
>
> Here is another surprise with the current peepholer:
>
> >>> dis(lambda:1+2*3)
>   1           0 LOAD_CONST               0 (1)
>              3 LOAD_CONST               3 (6)
>              6 BINARY_ADD
>              7 RETURN_VALUE
>
> >>> dis(lambda:2*3+1)
>  1           0 LOAD_CONST               4 (7)
>              3 RETURN_VALUE
>
> I have a fix in the works, but I will wait for your further comments
> before submitting it.
>
> >
> >  More importantly, we decided that the peepholer is the wrong place to
> >  do much of this work.  Most of the peepholer is going to be migrated
> >  up the chain, after the AST is generated, but before the opcodes are
> >  generated.  That is a faster, more reliable, and more general
> >  approach.
> >
>
> I agree.   Constant folding, is an interesting case because peepholer
> has to duplicate a subset of eval logic.  I wonder if the new approach
> could eliminate that.
>

I followed a different approach. Constant folding in WPython is made between
ASDL evalutation and AST building.

The idea is to "intercept" constant values and apply the operations
generating a new value instead of generating the classic AST node (a BinOp
for a binary operation, for example).

This way there's no need to parse the AST tree seeking for cases where to
apply the constant folding logic.

It's faster, because you don't need an additional pass through the AST:
you'll do it while building the AST...

It consumes less memory too, since you don't need to generate complex AST
nodes that must be discarded after applying the folding (which generates new
nodes). Think about a tuple of constant values, for example: you have to
generate a Tuple AST structure from the ASDL, then an AST constant folder
will generate the tuple. In WPython the tuple is generated immediately,
directly from the ADSL seq structure.

It's also efficient, since expressions such as 1 + 2 * 3 can be completely
folded generating 7, instead of 1 + 6 of the (classic) peepholer. That's
because, when parsing the ASDL structures, nodes are evaluated in respect of
operator precedence, so first we evaluate 2 * 3, which produces 6 applying
the folding, and then 1 + 6, which produces 7 in the end.

Cesare
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20110314/d19cf96c/attachment.html>


More information about the Python-Dev mailing list