Skip to content

Commit f15999d

Browse files
asvetlovclaude
andauthored
gh-161875: Push NULL and None in GET_AWAITABLE (#161876)
* Push NULL and None in GET_AWAITABLE Every await site compiled to GET_AWAITABLE; PUSH_NULL; LOAD_CONST None; SEND. GET_AWAITABLE is always followed by that pair, so let it push (iter, NULL, None) itself. `await f()` now compiles to CALL; GET_AWAITABLE; SEND, saving two dispatches per await. The tier-2 optimizer gets an override so the NULL and None slots are known constants. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Add NEWS entry Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent ec912e7 commit f15999d

14 files changed

Lines changed: 57 additions & 33 deletions

File tree

‎Doc/library/dis.rst‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,8 @@ not have to be) the original ``STACK[-2]``.
817817

818818
.. opcode:: GET_AWAITABLE (where)
819819

820-
Implements ``STACK[-1] = get_awaitable(STACK[-1])``, where ``get_awaitable(o)``
820+
Replaces ``STACK[-1]`` with ``get_awaitable(STACK[-1])``, ``NULL`` and
821+
``None``, ready for :opcode:`SEND`. ``get_awaitable(o)``
821822
returns ``o`` if ``o`` is a coroutine object or a generator object with
822823
the :data:`~inspect.CO_ITERABLE_COROUTINE` flag, or resolves
823824
``o.__await__``.
@@ -833,6 +834,10 @@ not have to be) the original ``STACK[-2]``.
833834
.. versionchanged:: 3.11
834835
Previously, this instruction did not have an oparg.
835836

837+
.. versionchanged:: next
838+
Also pushes ``NULL`` and ``None``, which were previously pushed by
839+
separate instructions.
840+
836841

837842
.. opcode:: GET_AITER
838843

‎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_AWAITABLE 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.

‎Lib/test/test_dis.py‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -615,8 +615,6 @@ async def _asyncwith(c):
615615
LOAD_SPECIAL 2 (__aenter__)
616616
CALL 0
617617
GET_AWAITABLE 1
618-
PUSH_NULL
619-
LOAD_COMMON_CONSTANT 7 (None)
620618
L2: SEND 4 (to L5)
621619
L3: YIELD_VALUE 1
622620
L4: RESUME 3
@@ -632,8 +630,6 @@ async def _asyncwith(c):
632630
LOAD_COMMON_CONSTANT 7 (None)
633631
CALL 3
634632
GET_AWAITABLE 2
635-
PUSH_NULL
636-
LOAD_COMMON_CONSTANT 7 (None)
637633
L8: SEND 4 (to L11)
638634
L9: YIELD_VALUE 1
639635
L10: RESUME 3
@@ -647,14 +643,12 @@ async def _asyncwith(c):
647643
RETURN_VALUE
648644
649645
%4d L12: CLEANUP_THROW
650-
L13: JUMP_BACKWARD_NO_INTERRUPT 28 (to L5)
646+
L13: JUMP_BACKWARD_NO_INTERRUPT 26 (to L5)
651647
L14: CLEANUP_THROW
652648
L15: JUMP_BACKWARD_NO_INTERRUPT 10 (to L11)
653649
L16: PUSH_EXC_INFO
654650
WITH_EXCEPT_START
655651
GET_AWAITABLE 2
656-
PUSH_NULL
657-
LOAD_COMMON_CONSTANT 7 (None)
658652
L17: SEND 5 (to L21)
659653
L18: YIELD_VALUE 1
660654
L19: RESUME 3
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The :opcode:`GET_AWAITABLE` instruction now also pushes the ``NULL`` and
2+
``None`` that start the :opcode:`SEND` loop, so ``await`` no longer needs
3+
separate :opcode:`PUSH_NULL` and ``LOAD_CONST None`` 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
@@ -1649,11 +1649,13 @@ dummy_func(
16491649
awaitable = PyStackRef_FromPyObjectSteal(awaitable_o);
16501650
}
16511651

1652-
inst(GET_AWAITABLE, (iterable -- iter)) {
1652+
inst(GET_AWAITABLE, (iterable -- iter, null, none)) {
16531653
PyObject *iter_o = _PyEval_GetAwaitable(PyStackRef_AsPyObjectBorrow(iterable), oparg);
16541654
PyStackRef_CLOSE(iterable);
16551655
ERROR_IF(iter_o == NULL);
16561656
iter = PyStackRef_FromPyObjectSteal(iter_o);
1657+
null = PyStackRef_NULL;
1658+
none = PyStackRef_None;
16571659
}
16581660

16591661
family(SEND, INLINE_CACHE_ENTRIES_SEND) = {

‎Python/codegen.c‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -618,8 +618,6 @@ codegen_unwind_fblock(compiler *c, location *ploc,
618618
RETURN_IF_ERROR(codegen_call_exit_with_nones(c, *ploc));
619619
if (info->fb_type == COMPILE_FBLOCK_ASYNC_WITH) {
620620
ADDOP_I(c, *ploc, GET_AWAITABLE, 2);
621-
ADDOP(c, *ploc, PUSH_NULL);
622-
ADDOP_LOAD_CONST(c, *ploc, Py_None);
623621
ADD_YIELD_FROM(c, *ploc, 1);
624622
}
625623
ADDOP(c, *ploc, POP_TOP);
@@ -5167,8 +5165,6 @@ codegen_comprehension(compiler *c, expr_ty e, int type,
51675165

51685166
if (is_async_comprehension && type != COMP_GENEXP) {
51695167
ADDOP_I(c, loc, GET_AWAITABLE, 0);
5170-
ADDOP(c, loc, PUSH_NULL);
5171-
ADDOP_LOAD_CONST(c, loc, Py_None);
51725168
ADD_YIELD_FROM(c, loc, 1);
51735169
}
51745170

@@ -5312,8 +5308,6 @@ codegen_async_with_inner(compiler *c, stmt_ty s, int pos)
53125308
ADDOP_I(c, loc, LOAD_SPECIAL, SPECIAL___AENTER__);
53135309
ADDOP_I(c, loc, CALL, 0);
53145310
ADDOP_I(c, loc, GET_AWAITABLE, 1);
5315-
ADDOP(c, loc, PUSH_NULL);
5316-
ADDOP_LOAD_CONST(c, loc, Py_None);
53175311
ADD_YIELD_FROM(c, loc, 1);
53185312

53195313
ADDOP_JUMP(c, loc, SETUP_WITH, final);
@@ -5349,8 +5343,6 @@ codegen_async_with_inner(compiler *c, stmt_ty s, int pos)
53495343
*/
53505344
RETURN_IF_ERROR(codegen_call_exit_with_nones(c, loc));
53515345
ADDOP_I(c, loc, GET_AWAITABLE, 2);
5352-
ADDOP(c, loc, PUSH_NULL);
5353-
ADDOP_LOAD_CONST(c, loc, Py_None);
53545346
ADD_YIELD_FROM(c, loc, 1);
53555347

53565348
ADDOP(c, loc, POP_TOP);
@@ -5364,8 +5356,6 @@ codegen_async_with_inner(compiler *c, stmt_ty s, int pos)
53645356
ADDOP(c, loc, PUSH_EXC_INFO);
53655357
ADDOP(c, loc, WITH_EXCEPT_START);
53665358
ADDOP_I(c, loc, GET_AWAITABLE, 2);
5367-
ADDOP(c, loc, PUSH_NULL);
5368-
ADDOP_LOAD_CONST(c, loc, Py_None);
53695359
ADD_YIELD_FROM(c, loc, 1);
53705360
RETURN_IF_ERROR(codegen_with_except_finish(c, cleanup));
53715361

@@ -5552,8 +5542,6 @@ codegen_visit_expr_impl(compiler *c, expr_ty e, bool result_is_unused)
55525542
case Await_kind:
55535543
VISIT(c, expr, e->v.Await.value);
55545544
ADDOP_I(c, loc, GET_AWAITABLE, 0);
5555-
ADDOP(c, loc, PUSH_NULL);
5556-
ADDOP_LOAD_CONST(c, loc, Py_None);
55575545
ADD_YIELD_FROM(c, loc, 1);
55585546
break;
55595547
case Compare_kind:

0 commit comments

Comments
 (0)