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
| 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.
While we are here please add spaces around operators to conform PEP 8.
There was a problem hiding this comment.
Added few style comments.
| 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.
Why this have been moved? It is better to get a size just before calling PyList_Append() as in previous code.
| constant = objs[i]; | ||
|
|
||
| Py_ssize_t len_consts = PyList_GET_SIZE(consts); | ||
| Py_ssize_t i, pos; |
There was a problem hiding this comment.
Why declarations were moved in the middle of the code?
| 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 |
|
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