Skip to content

Commit 9731dd2

Browse files
authored
GH-135379: Specialize int operations for compact ints only (GH-135668)
1 parent 5c25c88 commit 9731dd2

17 files changed

+515
-283
lines changed

Include/cpython/longintrepr.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ _PyLong_IsCompact(const PyLongObject* op) {
124124
return op->long_value.lv_tag < (2 << _PyLong_NON_SIZE_BITS);
125125
}
126126

127+
static inline int
128+
PyLong_CheckCompact(const PyObject *op)
129+
{
130+
return PyLong_CheckExact(op) && _PyLong_IsCompact((PyLongObject *)op);
131+
}
132+
127133
#define PyUnstable_Long_IsCompact _PyLong_IsCompact
128134

129135
static inline Py_ssize_t

Include/internal/pycore_long.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ PyAPI_DATA(PyObject*) _PyLong_Rshift(PyObject *, int64_t);
112112
// Export for 'math' shared extension
113113
PyAPI_DATA(PyObject*) _PyLong_Lshift(PyObject *, int64_t);
114114

115-
PyAPI_FUNC(PyObject*) _PyCompactLong_Add(PyLongObject *left, PyLongObject *right);
116-
PyAPI_FUNC(PyObject*) _PyCompactLong_Multiply(PyLongObject *left, PyLongObject *right);
117-
PyAPI_FUNC(PyObject*) _PyCompactLong_Subtract(PyLongObject *left, PyLongObject *right);
115+
PyAPI_FUNC(_PyStackRef) _PyCompactLong_Add(PyLongObject *left, PyLongObject *right);
116+
PyAPI_FUNC(_PyStackRef) _PyCompactLong_Multiply(PyLongObject *left, PyLongObject *right);
117+
PyAPI_FUNC(_PyStackRef) _PyCompactLong_Subtract(PyLongObject *left, PyLongObject *right);
118118

119119
// Export for 'binascii' shared extension.
120120
PyAPI_DATA(unsigned char) _PyLong_DigitValue[256];
@@ -213,7 +213,6 @@ _PyLong_BothAreCompact(const PyLongObject* a, const PyLongObject* b) {
213213
assert(PyLong_Check(b));
214214
return (a->long_value.lv_tag | b->long_value.lv_tag) < (2 << NON_SIZE_BITS);
215215
}
216-
217216
static inline bool
218217
_PyLong_IsZero(const PyLongObject *op)
219218
{

Include/internal/pycore_opcode_metadata.h

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

Include/internal/pycore_optimizer.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ typedef enum _JitSymType {
179179
JIT_SYM_KNOWN_VALUE_TAG = 7,
180180
JIT_SYM_TUPLE_TAG = 8,
181181
JIT_SYM_TRUTHINESS_TAG = 9,
182+
JIT_SYM_COMPACT_INT = 10,
182183
} JitSymType;
183184

184185
typedef struct _jit_opt_known_class {
@@ -211,13 +212,18 @@ typedef struct {
211212
uint16_t value;
212213
} JitOptTruthiness;
213214

215+
typedef struct {
216+
uint8_t tag;
217+
} JitOptCompactInt;
218+
214219
typedef union _jit_opt_symbol {
215220
uint8_t tag;
216221
JitOptKnownClass cls;
217222
JitOptKnownValue value;
218223
JitOptKnownVersion version;
219224
JitOptTuple tuple;
220225
JitOptTruthiness truthiness;
226+
JitOptCompactInt compact;
221227
} JitOptSymbol;
222228

223229

@@ -308,6 +314,7 @@ extern JitOptRef _Py_uop_sym_new_unknown(JitOptContext *ctx);
308314
extern JitOptRef _Py_uop_sym_new_not_null(JitOptContext *ctx);
309315
extern JitOptRef _Py_uop_sym_new_type(
310316
JitOptContext *ctx, PyTypeObject *typ);
317+
311318
extern JitOptRef _Py_uop_sym_new_const(JitOptContext *ctx, PyObject *const_val);
312319
extern JitOptRef _Py_uop_sym_new_null(JitOptContext *ctx);
313320
extern bool _Py_uop_sym_has_type(JitOptRef sym);
@@ -325,6 +332,9 @@ extern JitOptRef _Py_uop_sym_new_tuple(JitOptContext *ctx, int size, JitOptRef *
325332
extern JitOptRef _Py_uop_sym_tuple_getitem(JitOptContext *ctx, JitOptRef sym, int item);
326333
extern int _Py_uop_sym_tuple_length(JitOptRef sym);
327334
extern JitOptRef _Py_uop_sym_new_truthiness(JitOptContext *ctx, JitOptRef value, bool truthy);
335+
extern bool _Py_uop_sym_is_compact_int(JitOptRef sym);
336+
extern JitOptRef _Py_uop_sym_new_compact_int(JitOptContext *ctx);
337+
extern void _Py_uop_sym_set_compact_int(JitOptContext *ctx, JitOptRef sym);
328338

329339
extern void _Py_uop_abstractcontext_init(JitOptContext *ctx);
330340
extern void _Py_uop_abstractcontext_fini(JitOptContext *ctx);

0 commit comments

Comments
 (0)