You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee='https://github.com/rhettinger'closed_at=<Date2009-11-18.20:10:08.040>created_at=<Date2008-03-28.04:05:17.936>labels= ['interpreter-core', 'performance']
title='Fold unary + and not on constants'updated_at=<Date2009-11-18.20:10:08.038>user='https://github.com/abalkin'
It would be helpful if we talked before going further on build-outs to
the peephole optimizer. IIRC, we chose to not do this one because it
interfered with other more important optimizations.
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.
The constant folding anti-duplication patch should also not be done
for this same reason. That patch slows down compilation and makes it
more fragile but does not add speed (just like the dead code
elimination patches). When the peepholer is moved-up, the anti-
duplication code won't be needed (as you won't need its attendant
rescan/rewrite pass of the bytecode).
You're writing these faster than I have time to review and likely
reject them. Please show some moderation. The peepholer is
intentionally very conservative.
It would be helpful if we talked before going further on build-outs to
the peephole optimizer.
Sure.
IIRC, we chose to not do this one because it
interfered with other more important optimizations.
There are two optimization in my patch: one for + and the other
not . I think you recall that not optimization could
interfere with not a is b --> a is not b and similar transformations.
My patch, however does not affect those. With respect to unary +, I
don't think it was intentionally omitted: I would think it is more
common to use unary + on constants than .., but case UNARY_CONVERT
is there in 2.x.
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:
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.
BTW, what is the rationale for leading +/- not being part of the
number literal? Unary +/- optimization seems mostly useful for the
simple +/-x case which could be handled by the tokenizer.
What is the timeline for that project? Maybe a comment should be
placed in peephole.c explaining that there is a plan to move
uptimization logic up the compilation chain and announcing a
moratorium on further peephole.c work. I am not the only one
submitting peephole optimizer patches recently.
You're writing these faster than I have time to review and likely
reject them. Please show some moderation.
ok :-) One more peephole optimizer related idea that I have in the
pipeline is to implement an option to disable optimization altogether.
Having such an option would help debugging/profiling the optimizer
and will give users a simple check when they suspect that their code
is improperly optimized. I would propose a patch already, but I
could not think of a good command line option. Most compilers use
-O0, but that would be confusingly similar to -OO. What do you think?
Activity
abalkin commentedon Mar 28, 2008
Before:
After:
rhettinger commentedon Mar 28, 2008
It would be helpful if we talked before going further on build-outs to
the peephole optimizer. IIRC, we chose to not do this one because it
interfered with other more important optimizations.
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.
The constant folding anti-duplication patch should also not be done
for this same reason. That patch slows down compilation and makes it
more fragile but does not add speed (just like the dead code
elimination patches). When the peepholer is moved-up, the anti-
duplication code won't be needed (as you won't need its attendant
rescan/rewrite pass of the bytecode).
You're writing these faster than I have time to review and likely
reject them. Please show some moderation. The peepholer is
intentionally very conservative.
abalkin commentedon Mar 28, 2008
On Fri, Mar 28, 2008 at 3:25 AM, Raymond Hettinger
<report@bugs.python.org> wrote:
Sure.
There are two optimization in my patch: one for + and the other
not . I think you recall that not optimization could
interfere with not a is b --> a is not b and similar transformations.
My patch, however does not affect those. With respect to unary +, I
don't think it was intentionally omitted: I would think it is more
common to use unary + on constants than
.., but case UNARY_CONVERTis there in 2.x.
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:
1 0 LOAD_CONST 0 (1)
3 LOAD_CONST 3 (6)
6 BINARY_ADD
7 RETURN_VALUE
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.
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.
BTW, what is the rationale for leading +/- not being part of the
number literal? Unary +/- optimization seems mostly useful for the
simple +/-x case which could be handled by the tokenizer.
What is the timeline for that project? Maybe a comment should be
placed in peephole.c explaining that there is a plan to move
uptimization logic up the compilation chain and announcing a
moratorium on further peephole.c work. I am not the only one
submitting peephole optimizer patches recently.
ok :-) One more peephole optimizer related idea that I have in the
pipeline is to implement an option to disable optimization altogether.
Having such an option would help debugging/profiling the optimizer
and will give users a simple check when they suspect that their code
is improperly optimized. I would propose a patch already, but I
could not think of a good command line option. Most compilers use
-O0, but that would be confusingly similar to -OO. What do you think?
pitrou commentedon Nov 4, 2009
Folding UNARY_POSITIVE was done by Raymond in r75601.
rhettinger commentedon Nov 18, 2009
Closing this. The Unary Positive is already implemented and there are
no known use cases for constant folding a Unary Not.