8000 gh-97933: inline list/dict/set comprehensions by carljm · Pull Request #101441 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-97933: inline list/dict/set comprehensions #101441

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 56 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
72afa83
gh-97933: inline sync list/dict/set comprehensions
carljm Jan 30, 2023
8988234
simplify cell handling code slightly
carljm Jan 31, 2023
22c4a86
clarify comments
carljm Jan 31, 2023
c1b54f0
enable inlining async comprehensions also
carljm Jan 31, 2023
43db9b8
fix typo
carljm Jan 31, 2023
ed3209b
Merge branch 'main' into inlinecomp2
carljm Jan 31, 2023
aceb6c7
fix outer-cell, inner-local case
carljm Jan 31, 2023
e57c354
fix restoring NULL (unbound outer name) followed by load
carljm Jan 31, 2023
795d854
emit 1 x SWAP N+1 instead of N x SWAP 2
carljm Jan 31, 2023
686221a
remove stray dis.dis() call
carljm Jan 31, 2023
ac99697
fix compiler warning about Py_ssize_t -> int conversion
carljm Jan 31, 2023
db208d5
Merge branch 'main' into inlinecomp2
carljm Jan 31, 2023
8b76051
Merge branch 'main' into inlinecomp2
carljm Feb 1, 2023
4620856
add a couple more tests
carljm Feb 1, 2023
8773653
clear comp locals on entry, eval iter expr first
carljm Feb 1, 2023
be3becc
Merge branch 'main' into inlinecomp2
carljm Feb 9, 2023
f0c051e
fix double decref in error case
carljm Feb 9, 2023
142859a
adjust to RETURN_CONST
carljm Feb 9, 2023
568a470
fix up refcounting
carljm Feb 10, 2023
add772e
Merge branch 'main' into inlinecomp2
carljm Feb 10, 2023
17d5d84
improve importlib comment
carljm Feb 10, 2023
b87d209
mark STORE_FAST_MAYBE_NULL as possibly NULLing a local
carljm Feb 10, 2023
36b2917
Merge branch 'main' into inlinecomp2
carljm Feb 10, 2023
ae0bd02
Merge branch 'main' into inlinecomp2
carljm Feb 13, 2023
9f0fc5b
Merge branch 'main' into inlinecomp2
carljm Feb 20, 2023
463c740
add test for NameError/UnboundLocalError
carljm Feb 28, 2023
67f50ba
fix case where iter var is free in outer scope
carljm Feb 28, 2023
73dc0ed
Merge branch 'main' into inlinecomp2
carljm Feb 28, 2023
ecb313c
Merge branch 'main' into inlinecomp2
carljm Mar 6, 2023
4109baa
add inlining of non-function-scope comprehensions
carljm Mar 7, 2023
d8802a1
simplify scope handling
carljm Mar 7, 2023
1c019a7
Merge branch 'main' into inlinecomp2
carljm Mar 7, 2023
24a9d9f
Merge branch 'main' into inlinecomp2
carljm Mar 8, 2023
b6a025b
add tests for comprehensions in class scope
carljm Mar 8, 2023
90b34de
run all listcomp scope tests in module, class, and func scope
carljm Mar 8, 2023
06db319
Merge branch 'main' into inlinecomp2
carljm Mar 8, 2023
b52046b
handle frame locals materialization in class/module scope
carljm Mar 14, 2023
6c5f269
Merge branch 'main' into inlinecomp2
carljm Mar 14, 2023
1a8f4a0
Merge branch 'main' into inlinecomp2
carljm Mar 17, 2023
1274e2b
Merge branch 'main' into inlinecomp2
carljm May 1, 2023
0727d6f
Merge branch 'main' into inlinecomp2
carljm May 2, 2023
51a1294
update comment
carljm May 2, 2023
8a78a36
Merge branch 'main' into inlinecomp2
carljm May 5, 2023
43722b4
review comments
carljm May 5, 2023
bf9e1f1
fix single backticks
carljm May 5, 2023
ca636a5
better nested test
carljm May 5, 2023
46c7a4f
fix u_fasthidden in nested case
carljm May 5, 2023
fb9f89e
fix refleak
carljm May 5, 2023
baacf5f
Merge branch 'main' into inlinecomp2
carljm May 5, 2023
5914d77
remove assumption that class scopes can't have cellvars
carljm May 5, 2023
ffae4e6
Apply suggestions from code review
carljm May 9, 2023
76077cd
Merge branch 'main' into inlinecomp2
carljm May 9, 2023
a8425a6
review comments
carljm May 9, 2023
656e46b
Apply suggestions from code review
carljm May 9, 2023
1402e7a
Apply suggestions from code review
carljm May 9, 2023
95401fe
Merge branch 'main' into inlinecomp2
carljm May 9, 2023
File filter

Filter by extension

Filter by extension 8000

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
Merge branch 'main' into inlinecomp2
* main: (50 commits)
  gh-102674: Remove _specialization_stats from Lib/opcode.py (#102685)
  gh-102660: Handle m_copy Specially for the sys and builtins Modules (gh-102661)
  gh-102354: change python3 to python in docs examples (#102696)
  gh-81057: Add a CI Check for New Unsupported C Global Variables (gh-102506)
  GH-94851: check unicode consistency of static strings in debug mode (#102684)
  gh-100315: clarification to `__slots__` docs. (#102621)
  GH-100227: cleanup initialization of global interned dict (#102682)
  doc: Remove a duplicate 'versionchanged' in library/asyncio-task (gh-102677)
  gh-102013: Add PyUnstable_GC_VisitObjects (#102014)
  GH-102670: Use sumprod() to simplify, speed up, and improve accuracy of statistics functions (GH-102649)
  gh-102627: Replace address pointing toward malicious web page (#102630)
  gh-98831: Use DECREF_INPUTS() more (#102409)
  gh-101659: Avoid Allocation for Shared Exceptions in the _xxsubinterpreters Module (gh-102659)
  gh-101524: Fix the ChannelID tp_name (gh-102655)
  gh-102069: Fix `__weakref__` descriptor generation for custom dataclasses (#102075)
  gh-98169 dataclasses.astuple support DefaultDict (#98170)
  gh-102650: Remove duplicate include directives from multiple source files (#102651)
  GH-100987: Don't cache references to the names and consts array in `_PyEval_EvalFrameDefault`. (#102640)
  gh-87092: refactor assemble() to a number of separate functions, which do not need the compiler struct (#102562)
  gh-102192: Replace PyErr_Fetch/Restore etc by more efficient alternatives (#102631)
  ...
  • Loading branch information
carljm committed Mar 14, 2023
commit 6c5f269eceb005e4d7fe5de2320d81108cd6ad3c
11 changes: 6 additions & 5 deletions Lib/importlib/_bootstrap_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,10 +431,11 @@ def _write_atomic(path, data, mode=0o666):
# Python 3.12a5 3515 (Embed jump mask in COMPARE_OP oparg)
# Python 3.12a5 3516 (Add COMPARE_AND_BRANCH instruction)
# Python 3.12a5 3517 (Change YIELD_VALUE oparg to exception block depth)
# Python 3.12a5 3518 (Add RETURN_CONST instruction)
# Python 3.12a5 3519 (Modify SEND instruction)
# Python 3.12a5 3520 (Remove PREP_RERAISE_STAR, add CALL_INTRINSIC_2)
# Python 3.12a5 3521 (Inline list/dict/set comprehensions)
# Python 3.12a6 3518 (Add RETURN_CONST instruction)
# Python 3.12a6 3519 (Modify SEND instruction)
# Python 3.12a6 3520 (Remove PREP_RERAISE_STAR, add CALL_INTRINSIC_2)
# Python 3.12a7 3521 (Shrink the LOAD_GLOBAL caches)
# Python 3.12a7 3522 (Inline list/dict/set comprehensions)

# Python 3.13 will start with 3550

Expand All @@ -451,7 +452,7 @@ def _write_atomic(path, data, mode=0o666):
# Whenever MAGIC_NUMBER is changed, the ranges in the magic_values array
# in PC/launcher.c must also be updated.

MAGIC_NUMBER = (3521).to_bytes(2, 'little') + b'\r\n'
MAGIC_NUMBER = (3522).to_bytes(2, 'little') + b'\r\n'

_RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c

Expand Down
158 changes: 4 additions & 154 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -8401,10 +8401,10 @@ compute_localsplus_info(struct compiler_unit *u, int nlocalsplus,
assert(offset < nlocalsplus);
// For now we do not distinguish arg kinds.
_PyLocals_Kind kind = CO_FAST_LOCAL;
if (PyDict_Contains(c->u->u_fasthidden, k)) {
if (PyDict_Contains(u->u_fasthidden, k)) {
kind |= CO_FAST_HIDDEN;
}
if (PyDict_GetItem(c->u->u_cellvars, k) != NULL) {
if (PyDict_GetItem(u->u_cellvars, k) != NULL) {
kind |= CO_FAST_CELL;
}
_Py_set_localsplus_info(offset, k, kind, names, kinds);
Expand Down Expand Up @@ -8984,7 +8984,7 @@ assemble_code_unit(struct compiler_unit *u, PyObject *const_cache,
}
/* TO DO -- For 3.12, make sure that `maxdepth <= MAX_ALLOWED_STACK_USE` */

convert_exception_handlers_to_nops(g.g_entryblock);
convert_pseudo_ops(g.g_entryblock);

/* Order of basic blocks must have been determined by now */
if (normalize_jumps(&g) < 0) {
Expand Down Expand Up @@ -9026,157 +9026,7 @@ assemble(struct compiler *c, int addNone)
return NULL;
}

/** Preprocessing **/
if (instr_sequence_to_cfg(INSTR_SEQUENCE(c), g) < 0) {
goto error;
}
assert(cfg_builder_check(g));

int nblocks = 0;
for (basicblock *b = g->g_block_list; b != NULL; b = b->b_list) {
nblocks++;
}
if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) {
PyErr_NoMemory();
goto error;
}

/* Set firstlineno if it wasn't explicitly set. */
if (!c->u->u_firstlineno) {
if (g->g_entryblock->b_instr && g->g_entryblock->b_instr->i_loc.lineno) {
c->u->u_firstlineno = g->g_entryblock->b_instr->i_loc.lineno;
}
else {
c->u->u_firstlineno = 1;
}
}

/* Map labels to targets and mark exception handlers */
if (translate_jump_labels_to_targets(g->g_entryblock) < 0) {
goto error;
}
if (mark_except_handlers(g->g_entryblock) < 0) {
goto error;
}
if (label_exception_targets(g->g_entryblock)) {
goto error;
}

/** Optimization **/
consts = consts_dict_keys_inorder(c->u->u_consts);
if (consts == NULL) {
goto error;
}
if (optimize_cfg(g, consts, c->c_const_cache)) {
goto error;
}
if (remove_unused_consts(g->g_entryblock, consts) < 0) {
goto error;
}
if (add_checks_for_loads_of_uninitialized_variables(g->g_entryblock, c) < 0) {
goto error;
}

/** line numbers (TODO: move this before optimization stage) */
if (duplicate_exits_without_lineno(g) < 0) {
goto error;
}
propagate_line_numbers(g->g_entryblock);
guarantee_lineno_for_exits(g->g_entryblock, c->u->u_firstlineno);

if (push_cold_blocks_to_end(g, code_flags) < 0) {
goto error;
}

/** Assembly **/

int nlocalsplus = prepare_localsplus(c, g, code_flags);
if (nlocalsplus < 0) {
goto error;
}

int maxdepth = stackdepth(g->g_entryblock, code_flags);
if (maxdepth < 0) {
goto error;
}
/* TO DO -- For 3.12, make sure that `maxdepth <= MAX_ALLOWED_STACK_USE` */

convert_pseudo_ops(g->g_entryblock);

/* Order of basic blocks must have been determined by now */
if (normalize_jumps(g) < 0) {
goto error;
}
assert(no_redundant_jumps(g));
assert(opcode_metadata_is_sane(g));

/* Can't modify the bytecode after computing jump offsets. */
assemble_jump_offsets(g->g_entryblock);

/* Create assembler */
if (assemble_init(&a, c->u->u_firstlineno) < 0) {
goto error;
}

/* Emit code. */
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
for (int j = 0; j < b->b_iused; j++) {
if (assemble_emit(&a, &b->b_instr[j]) < 0) {
goto error;
}
}
}

/* Emit location info */
a.a_lineno = c->u->u_firstlineno;
location loc = NO_LOCATION;
int size = 0;
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
for (int j = 0; j < b->b_iused; j++) {
if (!same_location(loc, b->b_instr[j].i_loc)) {
if (assemble_emit_location(&a, loc, size)) {
goto error;
}
loc = b->b_instr[j].i_loc;
size = 0;
}
size += instr_size(&b->b_instr[j]);
}
}
if (assemble_emit_location(&a, loc, size)) {
goto error;
}

if (assemble_exception_table(&a, g->g_entryblock) < 0) {
goto error;
}
if (_PyBytes_Resize(&a.a_except_table, a.a_except_table_off) < 0) {
goto error;
}
if (merge_const_one(c->c_const_cache, &a.a_except_table) < 0) {
goto error;
}

if (_PyBytes_Resize(&a.a_linetable, a.a_location_off) < 0) {
goto error;
}
if (merge_const_one(c->c_const_cache, &a.a_linetable) < 0) {
goto error;
}

if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0) {
goto error;
}
if (merge_const_one(c->c_const_cache, &a.a_bytecode) < 0) {
goto error;
}

co = makecode(c, &a, consts, maxdepth, nlocalsplus, code_flags);
error:
Py_XDECREF(consts);
cfg_builder_fini(g);
assemble_free(&a);
return co;
return assemble_code_unit(u, const_cache, code_flags, filename);
}

static PyObject*
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.
0