Skip to content

Commit 70bd1c2

Browse files
authored
GH-143732: SEND specialization (GH-148963)
* SEND specialization. Adds 2 new specialized instructions: * SEND_VIRTUAL: for sends to virtual iterators e.g lists and tuples * SEND_ASYNC_GEN: for sends to async generators Tweak FOR_ITER_VIRTUAL so that SEND_VIRTUAL and FOR_ITER_VIRTUAL use equivalent guards
1 parent ffb543d commit 70bd1c2

29 files changed

Lines changed: 2689 additions & 1575 deletions

Include/cpython/object.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,11 @@ struct _typeobject {
241241
* Otherwise, limited to MAX_VERSIONS_PER_CLASS (defined elsewhere).
242242
*/
243243
uint16_t tp_versions_used;
244-
_Py_iteritemfunc _tp_iteritem; /* Virtual iterator next function */
244+
245+
/* Virtual iterator next function.
246+
* This function must escape to any code that can result in
247+
* the GC being run, such as Py_DECREF. */
248+
_Py_iteritemfunc _tp_iteritem;
245249
};
246250

247251
#define _Py_ATTR_CACHE_UNUSED (30000) // (see tp_versions_used)

Include/internal/pycore_abstract.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ PyAPI_FUNC(int) _Py_convert_optional_to_non_negative_ssize_t(PyObject *, void *)
6060
// Export for 'math' shared extension.
6161
PyAPI_FUNC(PyObject*) _PyNumber_Index(PyObject *o);
6262

63+
typedef struct {
64+
PyObject *object;
65+
PySendResult kind;
66+
} PySendResultPair;
67+
68+
// Same as PyIter_Send but returns a struct for MSVC tailcall support
69+
PyAPI_FUNC(PySendResultPair) _PyIter_Send(PyObject *iter, PyObject *arg);
70+
6371
#ifdef __cplusplus
6472
}
6573
#endif

Include/internal/pycore_genobject.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ extern PyTypeObject _PyCoroWrapper_Type;
4040
extern PyTypeObject _PyAsyncGenWrappedValue_Type;
4141
extern PyTypeObject _PyAsyncGenAThrow_Type;
4242

43+
PyAPI_FUNC(PySendResult) _PyAsyncGenASend_Send(PyObject *iter, PyObject *arg, PyObject **result);
44+
4345
#ifdef __cplusplus
4446
}
4547
#endif

Include/internal/pycore_magic_number.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ Known values:
296296
Python 3.15a8 3663 (Merge GET_ITER and GET_YIELD_FROM_ITER. Modify SEND to make it a bit more like FOR_ITER)
297297
Python 3.15a8 3664 (Fix __qualname__ for __annotate__ functions)
298298
Python 3.15a8 3665 (Add FOR_ITER_VIRTUAL and GET_ITER specializations)
299+
Python 3.15b1 3666 (Add SEND_VIRTUAL and SEND_ASYNC_GEN specializations)
299300
300301
301302
Python 3.16 will start with 3700
@@ -309,7 +310,7 @@ PC/launcher.c must also be updated.
309310
310311
*/
311312

312-
#define PYC_MAGIC_NUMBER 3665
313+
#define PYC_MAGIC_NUMBER 3666
313314
/* This is equivalent to converting PYC_MAGIC_NUMBER to 2 bytes
314315
(little-endian) and then appending b'\r\n'. */
315316
#define PYC_MAGIC_NUMBER_TOKEN \

Include/internal/pycore_opcode_metadata.h

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

0 commit comments

Comments
 (0)