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

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
handle frame locals materialization in class/module scope
  • Loading branch information
carljm committed Mar 14, 2023
commit b52046b8dd73f9d64d1f721af7b2f12155dba64c
1 change: 1 addition & 0 deletions Include/internal/pycore_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ struct callable_cache {
// Note that these all fit within a byte, as do combinations.
// Later, we will use the smaller numbers to differentiate the different
// kinds of locals (e.g. pos-only arg, varkwargs, local-only).
#define CO_FAST_HIDDEN 0x10
#define CO_FAST_LOCAL 0x20
#define CO_FAST_CELL 0x40
#define CO_FAST_FREE 0x80
Expand Down
21 changes: 16 additions & 5 deletions Lib/test/test_listcomps.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,20 @@ def _check_in_scopes(self, code, outputs, ns=None, scopes=None):
with self.subTest(scope=scope):
if scope == "class":
newcode = textwrap.dedent("""
class C:
class _C:
{code}
""").format(code=textwrap.indent(code, " "))
def get_output(moddict, name):
return getattr(moddict["C"], name)
return getattr(moddict["_C"], name)
elif scope == "function":
newcode = textwrap.dedent("""
def f():
def _f():
{code}
return locals()
_out = _f()
""").format(code=textwrap.indent(code, " "))
def get_output(moddict, name):
return moddict["f"]()[name]
return moddict["_out"][name]
else:
newcode = code
def get_output(moddict, name):
Expand Down Expand Up @@ -143,7 +144,7 @@ def test_inner_cell_shadows_outer(self):
i = 20
y = [x() for x in items]
"""
outputs = {"y": [4, 4, 4, 4, 4]}
outputs = {"y": [4, 4, 4, 4, 4], "i": 20}
self._check_in_scopes(code, outputs)

def test_closure_can_jump_over_comp_scope(self):
Expand Down Expand Up @@ -225,6 +226,16 @@ def g():
outputs = {"x": 1}
self._check_in_scopes(code, outputs)

def test_introspecting_frame_locals(self):
code = """
import sys
[i for i in range(2)]
i = 20
sys._getframe().f_locals
"""
outputs = {"i": 20}
self._check_in_scopes(code, outputs)

def test_unbound_local_after_comprehension(self):
def f():
if False:
Expand Down
4 changes: 4 additions & 0 deletions Objects/frameobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,10 @@ _PyFrame_FastToLocalsWithError(_PyInterpreterFrame *frame)
}

PyObject *name = PyTuple_GET_ITEM(co->co_localsplusnames, i);
_PyLocals_Kind kind = _PyLocals_GetKind(co->co_localspluskinds, i);
if (kind & CO_FAST_HIDDEN) {
continue;
}
if (value == NULL) {
if (PyObject_DelItem(locals, name) != 0) {
if (PyErr_ExceptionMatches(PyExc_KeyError)) {
Expand Down
45 changes: 25 additions & 20 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -630,9 +630,9 @@ struct compiler_unit {
PyObject *u_cellvars; /* cell variables */
PyObject *u_freevars; /* free variables */

// A set of names that should use fast-locals, even if not in a function */
PyObject *u_fastlocals;

PyObject *u_fasthidden; /* dict; keys are names that are fast-locals only
temporarily within an inlined comprehension. When
value is True, treat as fast-local. */
PyObject *u_private; /* for private name mangling */

Py_ssize_t u_argcount; /* number of arguments for block */
Expand Down Expand Up @@ -1000,6 +1000,7 @@ compiler_unit_free(struct compiler_unit *u)
Py_CLEAR(u->u_varnames);
Py_CLEAR(u->u_freevars);
Py_CLEAR(u->u_cellvars);
Py_CLEAR(u->u_fasthidden);
Py_CLEAR(u->u_private);
PyObject_Free(u);
}
Expand Down Expand Up @@ -1671,6 +1672,12 @@ compiler_enter_scope(struct compiler *c, identifier name,
return ERROR;
}

u->u_fasthidden = PyDict_New();
if (!u->u_fasthidden) {
compiler_unit_free(u);
return ERROR;
}

u->u_nfblocks = 0;
u->u_firstlineno = lineno;
u->u_consts = PyDict_New();
Expand Down Expand Up @@ -4118,8 +4125,7 @@ compiler_nameop(struct compiler *c, location loc,
break;
case LOCAL:
if (c->u->u_ste->ste_type == FunctionBlock ||
(c->u->u_fastlocals &&
PySet_Contains(c->u->u_fastlocals, mangled)))
(PyDict_GetItem(c->u->u_fasthidden, mangled) == Py_True))
optype = OP_FAST;
break;
case GLOBAL_IMPLICIT:
Expand Down Expand Up @@ -5298,16 +5304,6 @@ push_inlined_comprehension_state(struct compiler *c, location loc,
// them from the outer scope as needed
PyObject *k, *v;
Py_ssize_t pos = 0;
assert(c->u->u_fastlocals == NULL);
if (c->u->u_ste->ste_type != FunctionBlock) {
// comprehension in non-function scope; for isolation, we'll need to
// temporarily override names bound in the comprehension to use fast
// locals, even though nothing else in this frame will
c->u->u_fastlocals = PySet_New(0);
if (c->u->u_fastlocals == NULL) {
return ERROR;
}
}
while (PyDict_Next(entry->ste_symbols, &pos, &k, &v)) {
assert(PyLong_Check(v));
long symbol = PyLong_AS_LONG(v);
Expand All @@ -5316,9 +5312,9 @@ push_inlined_comprehension_state(struct compiler *c, location loc,
// assignment expression to a nonlocal in the comprehension, these don't
// need handling here since they shouldn't be isolated
if (symbol & DEF_LOCAL && ~symbol & DEF_NONLOCAL) {
if (c->u->u_fastlocals) {
if (c->u->u_ste->ste_type != FunctionBlock) {
// non-function scope: override this name to use fast locals
PySet_Add(c->u->u_fastlocals, k);
PyDict_SetItem(c->u->u_fasthidden, k, Py_True);
}
long scope = (symbol >> SCOPE_OFFSET) & SCOPE_MASK;
PyObject *outv = PyDict_GetItemWithError(c->u->u_ste->ste_symbols, k);
Expand Down Expand Up @@ -5392,7 +5388,6 @@ pop_inlined_comprehension_state(struct compiler *c, location loc,
PyObject *k, *v;
Py_ssize_t pos = 0;
if (state.temp_symbols) {
// restore scope for globals that we temporarily set as locals
while (PyDict_Next(state.temp_symbols, &pos, &k, &v)) {
if (PyDict_SetItem(c->u->u_ste->ste_symbols, k, v)) {
return ERROR;
Expand All @@ -5415,8 +5410,15 @@ pop_inlined_comprehension_state(struct compiler *c, location loc,
ADDOP_NAME(c, loc, STORE_FAST_MAYBE_NULL, k, varnames);
}
}
if (c->u->u_fastlocals) {
Py_CLEAR(c->u->u_fastlocals);
pos = 0;
while (PyDict_Next(c->u->u_fasthidden, &pos, &k, &v)) {
if (v == Py_True) {
// we set to False instead of clearing, so we can track which names
// were temporarily fast-locals and should use CO_FAST_HIDDEN
if (PyDict_SetItem(c->u->u_fasthidden, k, Py_False)) {
return ERROR;
}
}
}
return SUCCESS;
}
Expand Down Expand Up @@ -8333,6 +8335,9 @@ compute_localsplus_info(struct compiler *c, 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)) {
kind |= CO_FAST_HIDDEN;
}
if (PyDict_GetItem(c->u->u_cellvars, k) != NULL) {
kind |= CO_FAST_CELL;
}
Expand Down