8000 gh-126835: Move const list & set folding in for_iter & contains from ast_opt.c to flowgraph.c by WolframAlph · Pull Request #130032 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-126835: Move const list & set folding in for_iter & contains from ast_opt.c to flowgraph.c #130032

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

Merged
merged 9 commits into from
Feb 13, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
address review
  • Loading branch information
WolframAlph committed Feb 13, 2025
commit eb3abc24f639f46672f69e64f26b288a08054806
24 changes: 12 additions & 12 deletions Python/flowgraph.c
Original file line number Diff line number Diff line change
Expand Up @@ -1429,35 +1429,34 @@ fold_tuple_of_constants(basicblock *bb, int n, PyObject *consts, PyObject *const

#define MIN_CONST_SEQUENCE_SIZE 3
/*
Optimize literal list/set for:
Optimize lists and sets for:
1. "for" loop, comprehension or "in"/"not in" tests:
Change literal list or set of constants into constant
tuple or frozenset respectively. Change literal list of
tuple or frozenset respectively. Change list of
non-constants into tuple.
2. Constant literal lists/set with length >= MIN_CONST_SEQUENCE_SIZE:
Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cN, BUILD_LIST N
with BUILD_LIST 0, LOAD_CONST (c1, c2, ... cN), LIST_EXTEND 1,
or BUILD_SET & SET_UPDATE respectively.
*/
static int
optimize_list_or_set_literal(basicblock *bb, int i, int nextop,
PyObject *consts, PyObject *const_cache)
optimize_lists_and_sets(basicblock *bb, int i, int nextop,
PyObject *consts, PyObject *const_cache)
{
assert(PyDict_CheckExact(const_cache));
assert(PyList_CheckExact(consts));
cfg_instr *instr = &bb->b_instr[i];
assert(instr->i_opcode == BUILD_LIST || instr->i_opcode == BUILD_SET);
bool contains_or_iter_literal = nextop == GET_ITER || nextop == CONTAINS_OP;
bool contains_or_iter = nextop == GET_ITER || nextop == CONTAINS_OP;
int seq_size = instr->i_oparg;
if (seq_size < MIN_CONST_SEQUENCE_SIZE && !contains_or_iter_literal) {
if (seq_size < MIN_CONST_SEQUENCE_SIZE && !contains_or_iter) {
return SUCCESS;
}
PyObject *newconst;
RETURN_IF_ERROR(get_constant_sequence(bb, i-1, seq_size, consts, &newconst));
if (newconst == NULL) {
/* not a const sequence */
if (contains_or_iter_literal && instr->i_opcode == BUILD_LIST) {
/* convert list iterable to tuple */
if (newconst == NULL) { /* not a const sequence */
if (contains_or_iter && instr->i_opcode == BUILD_LIST) {
/* iterate over a tuple instead of list */
INSTR_SET_OP1(instr, BUILD_TUPLE, instr->i_oparg);
}
return SUCCESS;
Expand All @@ -1474,11 +1473,12 @@ optimize_list_or_set_literal(basicblock *bb, int i, int nextop,
int index = add_const(newconst, consts, const_cache);
RETURN_IF_ERROR(index);
nop_out(bb, i-1, seq_size);
if (contains_or_iter_literal) {
if (contains_or_iter) {
INSTR_SET_OP1(instr, LOAD_CONST, index);
}
else {
assert(i >= 2);
assert(instr->i_opcode == BUILD_LIST || instr->i_opcode == BUILD_SET);
INSTR_SET_OP1(&bb->b_instr[i-2], instr->i_opcode, 0);
INSTR_SET_OP1(&bb->b_instr[i-1], LOAD_CONST, index);
INSTR_SET_OP1(&bb->b_instr[i], instr->i_opcode == BUILD_LIST ? LIST_EXTEND : SET_UPDATE, 1);
Expand Down Expand Up @@ -1939,7 +1939,7 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
break;
case BUILD_LIST:
case BUILD_SET:
RETURN_IF_ERROR(optimize_list_or_set_literal(bb, i, nextop, consts, const_cache));
RETURN_IF_ERROR(optimize_lists_and_sets(bb, i, nextop, consts, const_cache));
break;
case POP_JUMP_IF_NOT_NONE:
case POP_JUMP_IF_NONE:
Expand Down
Loading
0