Skip to content

Commit 865d8d8

Browse files
asvetlovclaude
andauthored
gh-161875: Push NULL and None in GET_ANEXT (#161877)
Every async for iteration compiled to GET_ANEXT; PUSH_NULL; LOAD_CONST None; SEND. GET_ANEXT is always followed by that pair, so let it push (aiter, awaitable, NULL, None) itself, saving two dispatches per iteration. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent d456158 commit 865d8d8

13 files changed

Lines changed: 54 additions & 24 deletions

File tree

‎Doc/library/dis.rst‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -851,11 +851,16 @@ not have to be) the original ``STACK[-2]``.
851851

852852
.. opcode:: GET_ANEXT
853853

854-
Implement ``STACK.append(get_awaitable(STACK[-1].__anext__()))`` to the stack.
854+
Implement ``STACK.append(get_awaitable(STACK[-1].__anext__()))`` to the stack,
855+
then push ``NULL`` and ``None``, ready for :opcode:`SEND`.
855856
See ``GET_AWAITABLE`` for details about ``get_awaitable``.
856857

857858
.. versionadded:: 3.5
858859

860+
.. versionchanged:: next
861+
Also pushes ``NULL`` and ``None``, which were previously pushed by
862+
separate instructions.
863+
859864

860865
.. opcode:: END_ASYNC_FOR
861866

‎Include/internal/pycore_magic_number.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ Known values:
305305
Python 3.16a1 3705 (Add INTRINSIC_ADD_CONDITIONAL_ANNOTATION)
306306
Python 3.16a1 3706 (Add INTRINSIC_MATCH_CLASS_ISINSTANCE)
307307
Python 3.16a1 3707 (GET_AWAITABLE also pushes NULL and None for SEND)
308+
Python 3.16a1 3708 (GET_ANEXT also pushes NULL and None for SEND)
308309
309310
Python 3.17 will start with 3750
310311
@@ -314,7 +315,7 @@ Known values:
314315
315316
*/
316317

317-
#define PYC_MAGIC_NUMBER 3707
318+
#define PYC_MAGIC_NUMBER 3708
318319
/* This is equivalent to converting PYC_MAGIC_NUMBER to 2 bytes
319320
(little-endian) and then appending b'\r\n'. */
320321
#define PYC_MAGIC_NUMBER_TOKEN \

‎Include/internal/pycore_opcode_metadata.h‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Include/internal/pycore_uop_ids.h‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Include/internal/pycore_uop_metadata.h‎

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
The :opcode:`GET_ANEXT` instruction now also pushes the ``NULL`` and
2+
``None`` that start the :opcode:`SEND` loop, so each :keyword:`async for`
3+
iteration no longer needs separate :opcode:`PUSH_NULL` and ``LOAD_CONST None``
4+
instructions.

‎Modules/_testinternalcapi/test_cases.c.h‎

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Python/bytecodes.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1641,12 +1641,14 @@ dummy_func(
16411641
iter = PyStackRef_FromPyObjectSteal(iter_o);
16421642
}
16431643

1644-
inst(GET_ANEXT, (aiter -- aiter, awaitable)) {
1644+
inst(GET_ANEXT, (aiter -- aiter, awaitable, null, none)) {
16451645
PyObject *awaitable_o = _PyEval_GetANext(PyStackRef_AsPyObjectBorrow(aiter));
16461646
if (awaitable_o == NULL) {
16471647
ERROR_NO_POP();
16481648
}
16491649
awaitable = PyStackRef_FromPyObjectSteal(awaitable_o);
1650+
null = PyStackRef_NULL;
1651+
none = PyStackRef_None;
16501652
}
16511653

16521654
inst(GET_AWAITABLE, (iterable -- iter, null, none)) {

‎Python/codegen.c‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2223,8 +2223,6 @@ codegen_async_for(compiler *c, stmt_ty s)
22232223
/* SETUP_FINALLY to guard the __anext__ call */
22242224
ADDOP_JUMP(c, loc, SETUP_FINALLY, except);
22252225
ADDOP(c, loc, GET_ANEXT);
2226-
ADDOP(c, loc, PUSH_NULL);
2227-
ADDOP_LOAD_CONST(c, loc, Py_None);
22282226
USE_LABEL(c, send);
22292227
ADD_YIELD_FROM(c, loc, 1);
22302228
ADDOP(c, loc, POP_BLOCK); /* for SETUP_FINALLY */
@@ -4841,8 +4839,6 @@ codegen_async_comprehension_generator(compiler *c, location loc,
48414839

48424840
ADDOP_JUMP(c, loc, SETUP_FINALLY, except);
48434841
ADDOP(c, loc, GET_ANEXT);
4844-
ADDOP(c, loc, PUSH_NULL);
4845-
ADDOP_LOAD_CONST(c, loc, Py_None);
48464842
USE_LABEL(c, send);
48474843
ADD_YIELD_FROM(c, loc, 1);
48484844
ADDOP(c, loc, POP_BLOCK);

‎Python/executor_cases.c.h‎

Lines changed: 9 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)