-
-
Notifications
You must be signed in to change notification settings - Fork 32.7k
bpo-29469: peephole: Remove const_stack #4879
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Constant folding was moved to AST optimizer. But compiler may emit LOAD_CONSTs + BUILD_TUPLE. For example, default arguments can be constant tuple if all arguments are constant. This commit makes peephole's tuple folding simple. It doesn't support nested tuples because nested tuples are folded by AST optimizer already.
2a0d3d4
to
915a1a4
Compare
Python/peephole.c
Outdated
Py_ssize_t len_consts = PyList_GET_SIZE(consts); | ||
Py_ssize_t i, pos; | ||
|
||
for (i=0, pos=c_start; i<n; i++, pos++) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While we are here please add spaces around operators to conform PEP 8.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added few style comments.
Python/peephole.c
Outdated
for (i=0 ; i<n ; i++) { | ||
constant = objs[i]; | ||
|
||
Py_ssize_t len_consts = PyList_GET_SIZE(consts); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this have been moved? It is better to get a size just before calling PyList_Append()
as in previous code.
Python/peephole.c
Outdated
constant = objs[i]; | ||
|
||
Py_ssize_t len_consts = PyList_GET_SIZE(consts); | ||
Py_ssize_t i, pos; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why declarations were moved in the middle of the code?
Python/peephole.c
Outdated
Py_ssize_t const_stack_top = -1; | ||
Py_ssize_t const_stack_size = 0; | ||
int in_consts = 0; /* whether we are in a LOAD_CONST sequence */ | ||
unsigned int cumlc=0, lastlc=0; // Count runs of consecutive LOAD_CONSTs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add spaces around =
.
This works, but I think the code can be simpler and more robust. |
The code LGTM in any case (besides style nits). |
Constant folding was moved to AST optimizer.
But compiler may emit LOAD_CONSTs + BUILD_TUPLE.
For example, default arguments can be constant tuple
if all arguments are constant.
This commit makes peephole's tuple folding simple.
It doesn't support nested tuples because nested
tuples are folded by AST optimizer already.
https://bugs.python.org/issue29469