Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Prev Previous commit
Merge branch 'main' into binaryops_shift
# Conflicts:
#	Python/specialize.c
  • Loading branch information
eendebakpt committed Apr 23, 2026
commit 06900f5700dd48ae97af4e265fa8457cca556569
104 changes: 88 additions & 16 deletions Python/specialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,28 @@ _PyCode_Quicken(_Py_CODEUNIT *instructions, Py_ssize_t size, int enable_counters
#define SPEC_FAIL_BINARY_OP_OR_DIFFERENT_TYPES 30
#define SPEC_FAIL_BINARY_OP_XOR_INT 31
#define SPEC_FAIL_BINARY_OP_XOR_DIFFERENT_TYPES 32
#define SPEC_FAIL_BINARY_OP_LSHIFT_INT 33
#define SPEC_FAIL_BINARY_OP_LSHIFT_DIFFERENT_TYPES 34
#define SPEC_FAIL_BINARY_OP_RSHIFT_INT 33
#define SPEC_FAIL_BINARY_OP_RSHIFT_DIFFERENT_TYPES 34
#define SPEC_FAIL_BINARY_OP_SUBSCR 33
#define SPEC_FAIL_BINARY_OP_SUBSCR_LIST_SLICE 34
#define SPEC_FAIL_BINARY_OP_SUBSCR_TUPLE_SLICE 35
#define SPEC_FAIL_BINARY_OP_SUBSCR_STRING_SLICE 36
#define SPEC_FAIL_BINARY_OP_SUBSCR_NOT_HEAP_TYPE 37
#define SPEC_FAIL_BINARY_OP_SUBSCR_OTHER_SLICE 38
#define SPEC_FAIL_BINARY_OP_SUBSCR_MAPPINGPROXY 39
#define SPEC_FAIL_BINARY_OP_SUBSCR_RE_MATCH 40
#define SPEC_FAIL_BINARY_OP_SUBSCR_ARRAY 41
#define SPEC_FAIL_BINARY_OP_SUBSCR_DEQUE 42
#define SPEC_FAIL_BINARY_OP_SUBSCR_ENUMDICT 43
#define SPEC_FAIL_BINARY_OP_SUBSCR_STACKSUMMARY 44
#define SPEC_FAIL_BINARY_OP_SUBSCR_DEFAULTDICT 45
#define SPEC_FAIL_BINARY_OP_SUBSCR_COUNTER 46
#define SPEC_FAIL_BINARY_OP_SUBSCR_ORDEREDDICT 47
#define SPEC_FAIL_BINARY_OP_SUBSCR_BYTES 48
#define SPEC_FAIL_BINARY_OP_SUBSCR_STRUCTTIME 49
#define SPEC_FAIL_BINARY_OP_SUBSCR_RANGE 50
#define SPEC_FAIL_BINARY_OP_LSHIFT_INT 51
#define SPEC_FAIL_BINARY_OP_LSHIFT_DIFFERENT_TYPES 52
#define SPEC_FAIL_BINARY_OP_RSHIFT_INT 53
#define SPEC_FAIL_BINARY_OP_RSHIFT_DIFFERENT_TYPES 54

/* Calls */

Expand Down Expand Up @@ -2127,6 +2145,56 @@ is_compactnonnegativelong(PyObject *v)
_PyLong_IsNonNegativeCompact((PyLongObject *)v);
}

/* sequence * int helpers: bypass PyNumber_Multiply dispatch overhead
by calling sq_repeat directly with PyLong_AsSsize_t. */

static inline PyObject *
seq_int_multiply(PyObject *seq, PyObject *n,
ssizeargfunc repeat)
{
Py_ssize_t count = PyLong_AsSsize_t(n);
if (count == -1 && PyErr_Occurred()) {
return NULL;
}
return repeat(seq, count);
}

static PyObject *
str_int_multiply(PyObject *lhs, PyObject *rhs)
{
return seq_int_multiply(lhs, rhs, PyUnicode_Type.tp_as_sequence->sq_repeat);
}

static PyObject *
int_str_multiply(PyObject *lhs, PyObject *rhs)
{
return seq_int_multiply(rhs, lhs, PyUnicode_Type.tp_as_sequence->sq_repeat);
}

static PyObject *
bytes_int_multiply(PyObject *lhs, PyObject *rhs)
{
return seq_int_multiply(lhs, rhs, PyBytes_Type.tp_as_sequence->sq_repeat);
}

static PyObject *
int_bytes_multiply(PyObject *lhs, PyObject *rhs)
{
return seq_int_multiply(rhs, lhs, PyBytes_Type.tp_as_sequence->sq_repeat);
}

static PyObject *
tuple_int_multiply(PyObject *lhs, PyObject *rhs)
{
return seq_int_multiply(lhs, rhs, PyTuple_Type.tp_as_sequence->sq_repeat);
}

static PyObject *
int_tuple_multiply(PyObject *lhs, PyObject *rhs)
{
return seq_int_multiply(rhs, lhs, PyTuple_Type.tp_as_sequence->sq_repeat);
}

static int
compactlongs_guard(PyObject *lhs, PyObject *rhs)
{
Expand Down Expand Up @@ -2238,18 +2306,22 @@ LONG_FLOAT_ACTION(compactlong_float_multiply, *)
LONG_FLOAT_ACTION(compactlong_float_true_div, /)
#undef LONG_FLOAT_ACTION

static _PyBinaryOpSpecializationDescr compactlongs_specs[NB_OPARG_LAST+1] = {
[NB_OR] = {compactlongs_guard, compactlongs_or},
[NB_AND] = {compactlongs_guard, compactlongs_and},
[NB_XOR] = {compactlongs_guard, compactlongs_xor},
[NB_LSHIFT] = {shift_guard, compactlongs_lshift},
[NB_RSHIFT] = {shift_guard, compactlongs_rshift},
[NB_INPLACE_OR] = {compactlongs_guard, compactlongs_or},
[NB_INPLACE_AND] = {compactlongs_guard, compactlongs_and},
[NB_INPLACE_XOR] = {compactlongs_guard, compactlongs_xor},
[NB_INPLACE_LSHIFT] = {shift_guard, compactlongs_lshift},
[NB_INPLACE_RSHIFT] = {shift_guard, compactlongs_rshift},
};
static _PyBinaryOpSpecializationDescr binaryop_extend_descrs[] = {
/* long-long arithmetic: guards also check _PyLong_IsCompact, so
type alone is not sufficient to eliminate the guard. */
{NB_OR, compactlongs_guard, compactlongs_or, &PyLong_Type, 1, NULL, NULL},
{NB_AND, compactlongs_guard, compactlongs_and, &PyLong_Type, 1, NULL, NULL},
{NB_XOR, compactlongs_guard, compactlongs_xor, &PyLong_Type, 1, NULL, NULL},
{NB_INPLACE_OR, compactlongs_guard, compactlongs_or, &PyLong_Type, 1, NULL, NULL},
{NB_INPLACE_AND, compactlongs_guard, compactlongs_and, &PyLong_Type, 1, NULL, NULL},
{NB_INPLACE_XOR, compactlongs_guard, compactlongs_xor, &PyLong_Type, 1, NULL, NULL},

/* long-long shifts: guards also check rhs is non-negative and <= 16 to
avoid undefined behavior and overflow, so type alone is not sufficient. */
{NB_LSHIFT, shift_guard, compactlongs_lshift, &PyLong_Type, 1, NULL, NULL},
{NB_RSHIFT, shift_guard, compactlongs_rshift, &PyLong_Type, 1, NULL, NULL},
{NB_INPLACE_LSHIFT, shift_guard, compactlongs_lshift, &PyLong_Type, 1, NULL, NULL},
{NB_INPLACE_RSHIFT, shift_guard, compactlongs_rshift, &PyLong_Type, 1, NULL, NULL},

/* float-long arithmetic: guards also check NaN and compactness. */
{NB_ADD, float_compactlong_guard, float_compactlong_add, &PyFloat_Type, 1, NULL, NULL},
Expand Down
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.