Skip to content

Conversation

methane
Copy link
Member

@methane methane commented Dec 15, 2017

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

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.
@methane methane force-pushed the peephole-remove-conststack branch from 2a0d3d4 to 915a1a4 Compare December 15, 2017 09:32
Py_ssize_t len_consts = PyList_GET_SIZE(consts);
Py_ssize_t i, pos;

for (i=0, pos=c_start; i<n; i++, pos++) {
Copy link
Member

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.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a 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.

for (i=0 ; i<n ; i++) {
constant = objs[i];

Py_ssize_t len_consts = PyList_GET_SIZE(consts);
Copy link
Member

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.

constant = objs[i];

Py_ssize_t len_consts = PyList_GET_SIZE(consts);
Py_ssize_t i, pos;
Copy link
Member

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?

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add spaces around =.

@serhiy-storchaka
Copy link
Member

This works, but I think the code can be simpler and more robust. cumlc and lastlc were needed for the old variable-sized bytecode. But now we can iterate the bytecode back. lastn_const_start() can stop if encounter the opcode different from LOAD_CONST.

@serhiy-storchaka
Copy link
Member

The code LGTM in any case (besides style nits).

@methane methane merged commit 87010e8 into python:master Dec 18, 2017
@methane methane deleted the peephole-remove-conststack branch December 18, 2017 06:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants