Skip to content

Commit 7264e37

Browse files
committed
gh-100239: specialize long tail of binary operations
1 parent 802556a commit 7264e37

17 files changed

+865
-491
lines changed

Include/internal/pycore_code.h

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ typedef struct {
100100

101101
typedef struct {
102102
_Py_BackoffCounter counter;
103+
uint16_t external_cache[4];
103104
} _PyBinaryOpCache;
104105

105106
#define INLINE_CACHE_ENTRIES_BINARY_OP CACHE_ENTRIES(_PyBinaryOpCache)
@@ -345,8 +346,8 @@ extern void _Py_Specialize_Call(_PyStackRef callable, _Py_CODEUNIT *instr,
345346
int nargs);
346347
extern void _Py_Specialize_CallKw(_PyStackRef callable, _Py_CODEUNIT *instr,
347348
int nargs);
348-
extern void _Py_Specialize_BinaryOp(_PyStackRef lhs, _PyStackRef rhs, _Py_CODEUNIT *instr,
349-
int oparg, _PyStackRef *locals);
349+
extern int _Py_Specialize_BinaryOp(_PyStackRef lhs, _PyStackRef rhs, _Py_CODEUNIT *instr,
350+
int oparg, _PyStackRef *locals);
350351
extern void _Py_Specialize_CompareOp(_PyStackRef lhs, _PyStackRef rhs,
351352
_Py_CODEUNIT *instr, int oparg);
352353
extern void _Py_Specialize_UnpackSequence(_PyStackRef seq, _Py_CODEUNIT *instr,
@@ -443,6 +444,12 @@ write_obj(uint16_t *p, PyObject *val)
443444
memcpy(p, &val, sizeof(val));
444445
}
445446

447+
static inline void
448+
write_void(uint16_t *p, void *val)
449+
{
450+
memcpy(p, &val, sizeof(val));
451+
}
452+
446453
static inline uint16_t
447454
read_u16(uint16_t *p)
448455
{
@@ -473,6 +480,14 @@ read_obj(uint16_t *p)
473480
return val;
474481
}
475482

483+
static inline void *
484+
read_void(uint16_t *p)
485+
{
486+
void *val;
487+
memcpy(&val, p, sizeof(val));
488+
return val;
489+
}
490+
476491
/* See Objects/exception_handling_notes.txt for details.
477492
*/
478493
static inline unsigned char *
@@ -576,6 +591,17 @@ adaptive_counter_backoff(_Py_BackoffCounter counter) {
576591
return restart_backoff_counter(counter);
577592
}
578593

594+
/* Specialization Extensions */
595+
596+
/* callbacks for an external specialization */
597+
typedef int (*binaryopguardfunc)(PyObject *lhs, PyObject *rhs);
598+
typedef PyObject *(*binaryopactionfunc)(PyObject *lhs, PyObject *rhs);
599+
600+
typedef struct _PyBinaryOpSpecializationDescr {
601+
binaryopguardfunc guard;
602+
binaryopactionfunc action;
603+
struct _PyBinaryOpSpecializationDescr *prev, *next; /* For the tstate linked list */
604+
} PyBinaryOpSpecializationDescr;
579605

580606
/* Comparison bit masks. */
581607

Include/internal/pycore_interp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,8 @@ struct _is {
287287
// _initial_thread should be the last field of PyInterpreterState.
288288
// See https://github.com/python/cpython/issues/127117.
289289

290+
PyBinaryOpSpecializationDescr* binary_op_specialization_list;
291+
290292
#if !defined(Py_GIL_DISABLED) && defined(Py_STACKREF_DEBUG)
291293
uint64_t next_stackref;
292294
_Py_hashtable_t *stackref_debug_table;

Include/internal/pycore_magic_number.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ Known values:
265265
Python 3.14a4 3610 (Add VALUE_WITH_FAKE_GLOBALS format to annotationlib)
266266
Python 3.14a4 3611 (Add NOT_TAKEN instruction)
267267
Python 3.14a4 3612 (Add POP_ITER and INSTRUMENTED_POP_ITER)
268+
Python 3.14a4 3613 (Add BINARY_OP_EXTEND)
268269
269270
Python 3.15 will start with 3650
270271
@@ -277,7 +278,7 @@ PC/launcher.c must also be updated.
277278
278279
*/
279280

280-
#define PYC_MAGIC_NUMBER 3612
281+
#define PYC_MAGIC_NUMBER 3613
281282
/* This is equivalent to converting PYC_MAGIC_NUMBER to 2 bytes
282283
(little-endian) and then appending b'\r\n'. */
283284
#define PYC_MAGIC_NUMBER_TOKEN \

Include/internal/pycore_opcode_metadata.h

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

0 commit comments

Comments
 (0)