Skip to content

Commit

Permalink
enable inlining async comprehensions also
Browse files Browse the repository at this point in the history
  • Loading branch information
carljm committed Jan 31, 2023
1 parent 22c4a86 commit c1b54f0
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 9 deletions.
6 changes: 3 additions & 3 deletions Lib/test/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1363,7 +1363,7 @@ async def f():
compiled_code, _ = self.check_positions_against_ast(snippet)
g = {}
eval(compiled_code, g)
compiled_code = g['f'].__code__.co_consts[1]
compiled_code = g['f'].__code__
self.assertIsInstance(compiled_code, types.CodeType)
self.assertOpcodeSourcePositionIs(compiled_code, 'LIST_APPEND',
line=2, end_line=3, column=5, end_column=12, occurrence=1)
Expand Down Expand Up @@ -1404,7 +1404,7 @@ async def f():
compiled_code, _ = self.check_positions_against_ast(snippet)
g = {}
eval(compiled_code, g)
compiled_code = g['f'].__code__.co_consts[1]
compiled_code = g['f'].__code__
self.assertIsInstance(compiled_code, types.CodeType)
self.assertOpcodeSourcePositionIs(compiled_code, 'SET_ADD',
line=2, end_line=3, column=5, end_column=12, occurrence=1)
Expand Down Expand Up @@ -1445,7 +1445,7 @@ async def f():
compiled_code, _ = self.check_positions_against_ast(snippet)
g = {}
eval(compiled_code, g)
compiled_code = g['f'].__code__.co_consts[1]
compiled_code = g['f'].__code__
self.assertIsInstance(compiled_code, types.CodeType)
self.assertOpcodeSourcePositionIs(compiled_code, 'MAP_ADD',
line=2, end_line=3, column=5, end_column=11, occurrence=1)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Inline (non-async) list, dict and set comprehensions to reduce performance
and bytecode size overhead.
Inline list, dict and set comprehensions to improve performance
and reduce bytecode size.
6 changes: 2 additions & 4 deletions Python/symtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -907,13 +907,11 @@ analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
assert(c && PySTEntry_Check(c));
entry = (PySTEntryObject*)c;

// we inline comprehensions if they are inside a function, are not a
// generator, and are not async
// we inline comprehensions if inside a function and not a generator
int inline_comp =
entry->ste_comprehension &&
ste->ste_type == FunctionBlock &&
!entry->ste_generator &&
!entry->ste_coroutine;
!entry->ste_generator;

if (!analyze_child_block(entry, newbound, newfree, newglobal,
&child_free))
Expand Down

0 comments on commit c1b54f0

Please sign in to comment.