Skip to content

Commit 273c4db

Browse files
asvetlovclaude
andcommitted
gh-161875: Push NULL and None in GET_ANEXT
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 020c5f3 commit 273c4db

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
@@ -832,11 +832,16 @@ not have to be) the original ``STACK[-2]``.
832832

833833
.. opcode:: GET_ANEXT
834834

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

838839
.. versionadded:: 3.5
839840

841+
.. versionchanged:: next
842+
Also pushes ``NULL`` and ``None``, which were previously pushed by
843+
separate instructions.
844+
840845

841846
.. opcode:: END_ASYNC_FOR
842847

‎Include/internal/pycore_magic_number.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ Known values:
304304
Python 3.16a1 3704 (Replace DELETE_ATTR with PUSH_NULL; STORE_ATTR)
305305
Python 3.16a1 3705 (Add INTRINSIC_ADD_CONDITIONAL_ANNOTATION)
306306
Python 3.16a1 3706 (Add INTRINSIC_MATCH_CLASS_ISINSTANCE)
307+
Python 3.16a1 3707 (GET_ANEXT also pushes NULL and None for SEND)
307308
308309
Python 3.17 will start with 3750
309310
@@ -313,7 +314,7 @@ Known values:
313314
314315
*/
315316

316-
#define PYC_MAGIC_NUMBER 3706
317+
#define PYC_MAGIC_NUMBER 3707
317318
/* This is equivalent to converting PYC_MAGIC_NUMBER to 2 bytes
318319
(little-endian) and then appending b'\r\n'. */
319320
#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)) {

‎Python/codegen.c‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2225,8 +2225,6 @@ codegen_async_for(compiler *c, stmt_ty s)
22252225
/* SETUP_FINALLY to guard the __anext__ call */
22262226
ADDOP_JUMP(c, loc, SETUP_FINALLY, except);
22272227
ADDOP(c, loc, GET_ANEXT);
2228-
ADDOP(c, loc, PUSH_NULL);
2229-
ADDOP_LOAD_CONST(c, loc, Py_None);
22302228
USE_LABEL(c, send);
22312229
ADD_YIELD_FROM(c, loc, 1);
22322230
ADDOP(c, loc, POP_BLOCK); /* for SETUP_FINALLY */
@@ -4843,8 +4841,6 @@ codegen_async_comprehension_generator(compiler *c, location loc,
48434841

48444842
ADDOP_JUMP(c, loc, SETUP_FINALLY, except);
48454843
ADDOP(c, loc, GET_ANEXT);
4846-
ADDOP(c, loc, PUSH_NULL);
4847-
ADDOP_LOAD_CONST(c, loc, Py_None);
48484844
USE_LABEL(c, send);
48494845
ADD_YIELD_FROM(c, loc, 1);
48504846
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)