8000 gh-126835: Move optimization of constant sequence creation from codegen to CFG by Eclips4 · Pull Request #129426 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-126835: Move optimization of constant sequence creation from codegen to CFG #129426

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 16 commits into from
Feb 1, 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 Yan's review
  • Loading branch information
Eclips4 committed Jan 31, 2025
commit 21a21d011d763c099ec1a538ae021dc397f37732
7 changes: 4 additions & 3 deletions Python/flowgraph.c
Original file line number Diff line number Diff line change
Expand Up @@ -1393,12 +1393,13 @@ fold_tuple_on_constants(PyObject *const_cache,
return SUCCESS;
}

#define MIN_CONST_SEQUENCE_SIZE 3
/* 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_const_sequence(PyObject *const_cache, cfg_instr* inst, int n, PyObject *consts)
optimize_build_list_or_set_with_constants(PyObject *const_cache, cfg_instr* inst, int n, PyObject *consts)
{
assert(PyDict_CheckExact(const_cache));
assert(PyList_CheckExact(consts));
Expand All @@ -1408,7 +1409,7 @@ optimize_const_sequence(PyObject *const_cache, cfg_instr* inst, int n, PyObject
assert(build == BUILD_LIST || build == BUILD_SET);
int extend = build == BUILD_LIST ? LIST_EXTEND : SET_UPDATE;

if (n < 3 || !is_sequence_constant(inst, n)) {
if (n < MIN_CONST_SEQUENCE_SIZE || !is_sequence_constant(inst, n)) {
return SUCCESS;
}
PyObject *newconst = PyTuple_New(n);
Expand Down Expand Up @@ -1812,7 +1813,7 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
case BUILD_LIST:
case BUILD_SET:
if (i >= oparg) {
if (optimize_const_sequence(const_cache, inst-oparg, oparg, consts) < 0) {
if (optimize_build_list_or_set_with_constants(const_cache, inst-oparg, oparg, consts) < 0) {
goto error;
}
}
Expand Down
Loading
0