Skip to content

Commit f29d8be

Browse files
committed
static specs
1 parent fd88c8d commit f29d8be

File tree

3 files changed

+25
-87
lines changed

3 files changed

+25
-87
lines changed

Include/internal/pycore_code.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,6 @@ typedef PyObject *(*binaryopactionfunc)(PyObject *lhs, PyObject *rhs);
600600
typedef struct _PyBinaryOpSpecializationDescr {
601601
binaryopguardfunc guard;
602602
binaryopactionfunc action;
603-
struct _PyBinaryOpSpecializationDescr *prev, *next; /* For the tstate linked list */
604603
} PyBinaryOpSpecializationDescr;
605604

606605
/* Comparison bit masks. */

Include/internal/pycore_interp.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,6 @@ 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-
292290
#if !defined(Py_GIL_DISABLED) && defined(Py_STACKREF_DEBUG)
293291
uint64_t next_stackref;
294292
_Py_hashtable_t *stackref_debug_table;

Python/specialize.c

Lines changed: 25 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -2400,13 +2400,6 @@ FLOAT_LONG_ACTION(float_compactlong_multiply, *)
24002400
FLOAT_LONG_ACTION(float_compactlong_true_div, /)
24012401
#undef FLOAT_LONG_ACTION
24022402

2403-
static binaryopactionfunc float_compactlong_actions[NB_OPARG_LAST+1] = {
2404-
[NB_ADD] = float_compactlong_add,
2405-
[NB_SUBTRACT] = float_compactlong_subtract,
2406-
[NB_TRUE_DIVIDE] = float_compactlong_true_div,
2407-
[NB_INPLACE_MULTIPLY] = float_compactlong_multiply,
2408-
};
2409-
24102403
/* long-float */
24112404

24122405
static int
@@ -2433,80 +2426,36 @@ LONG_FLOAT_ACTION(compactlong_float_multiply, *)
24332426
LONG_FLOAT_ACTION(compactlong_float_true_div, /)
24342427
#undef LONG_FLOAT_ACTION
24352428

2436-
static binaryopactionfunc compactlong_float_actions[NB_OPARG_LAST+1] = {
2437-
[NB_ADD] = compactlong_float_add,
2438-
[NB_SUBTRACT] = compactlong_float_subtract,
2439-
[NB_TRUE_DIVIDE] = compactlong_float_true_div,
2440-
[NB_INPLACE_MULTIPLY] = compactlong_float_multiply,
2429+
static PyBinaryOpSpecializationDescr float_compactlong_specs[NB_OPARG_LAST+1] = {
2430+
[NB_ADD] = {float_compactlong_guard, float_compactlong_add},
2431+
[NB_SUBTRACT] = {float_compactlong_guard, float_compactlong_subtract},
2432+
[NB_TRUE_DIVIDE] = {float_compactlong_guard, float_compactlong_true_div},
2433+
[NB_INPLACE_MULTIPLY] = {float_compactlong_guard, float_compactlong_multiply},
2434+
};
2435+
2436+
static PyBinaryOpSpecializationDescr compactlong_float_specs[NB_OPARG_LAST+1] = {
2437+
[NB_ADD] = {compactlong_float_guard, compactlong_float_add},
2438+
[NB_SUBTRACT] = {compactlong_float_guard, compactlong_float_subtract},
2439+
[NB_TRUE_DIVIDE] = {compactlong_float_guard, compactlong_float_true_div},
2440+
[NB_INPLACE_MULTIPLY] = {compactlong_float_guard, compactlong_float_multiply},
24412441
};
24422442

24432443
static int
24442444
binary_op_extended_specialization(PyObject *lhs, PyObject *rhs, int oparg,
2445-
binaryopguardfunc *guard, binaryopactionfunc *action)
2446-
{
2447-
if (compactlong_float_actions[oparg]) {
2448-
if (compactlong_float_guard(lhs, rhs)) {
2449-
*guard = compactlong_float_guard;
2450-
*action = compactlong_float_actions[oparg];
2451-
return 1;
2452-
}
2453-
}
2454-
if (float_compactlong_actions[oparg]) {
2455-
if (float_compactlong_guard(lhs, rhs)) {
2456-
*guard = float_compactlong_guard;
2457-
*action = float_compactlong_actions[oparg];
2458-
return 1;
2459-
}
2460-
}
2461-
return 0;
2462-
}
2463-
2464-
static PyBinaryOpSpecializationDescr*
2465-
new_binary_op_specialization_descr(binaryopguardfunc guard, binaryopactionfunc action)
2466-
{
2467-
PyBinaryOpSpecializationDescr *new_descr = PyMem_Malloc(sizeof(PyBinaryOpSpecializationDescr));
2468-
if (new_descr == NULL) {
2469-
return NULL;
2470-
}
2471-
new_descr->guard = guard;
2472-
new_descr->action = action;
2473-
2474-
PyThreadState *tstate = _PyThreadState_GET();
2475-
PyBinaryOpSpecializationDescr *head = tstate->interp->binary_op_specialization_list;
2476-
if (head != NULL) {
2477-
head->prev = new_descr;
2478-
}
2479-
new_descr->next = head;
2480-
new_descr->prev = NULL;
2481-
tstate->interp->binary_op_specialization_list = new_descr;
2482-
2483-
return new_descr;
2484-
}
2485-
2486-
static void
2487-
free_binary_op_specialization_descr(PyBinaryOpSpecializationDescr* descr)
2445+
PyBinaryOpSpecializationDescr **descr)
24882446
{
2489-
if (descr->prev != NULL) {
2490-
descr->prev->next = descr->next;
2491-
}
2492-
else {
2493-
PyThreadState *tstate = _PyThreadState_GET();
2494-
assert(tstate->interp->binary_op_specialization_list == descr);
2495-
tstate->interp->binary_op_specialization_list = descr->next;
2496-
}
2497-
if (descr->next != NULL) {
2498-
descr->next->prev = descr->prev;
2447+
#define LOOKUP_SPEC(TABLE, OPARG) \
2448+
if ((TABLE)[(OPARG)].action) { \
2449+
if ((TABLE)[(OPARG)].guard(lhs, rhs)) { \
2450+
*descr = &((TABLE)[OPARG]); \
2451+
return 1; \
2452+
} \
24992453
}
2500-
PyMem_Free(descr);
2501-
}
25022454

2503-
void
2504-
_Py_Specialize_FreeAllSpecializationDescrs(PyInterpreterState *interp)
2505-
{
2506-
while(interp->binary_op_specialization_list) {
2507-
free_binary_op_specialization_descr(
2508-
interp->binary_op_specialization_list);
2509-
}
2455+
LOOKUP_SPEC(compactlong_float_specs, oparg);
2456+
LOOKUP_SPEC(float_compactlong_specs, oparg);
2457+
#undef LOOKUP_SPEC
2458+
return 0;
25102459
}
25112460

25122461
int
@@ -2520,9 +2469,7 @@ _Py_Specialize_BinaryOp(_PyStackRef lhs_st, _PyStackRef rhs_st, _Py_CODEUNIT *in
25202469

25212470
_PyBinaryOpCache *cache = (_PyBinaryOpCache *)(instr + 1);
25222471
if (instr->op.code == BINARY_OP_EXTEND) {
2523-
void *data = read_void(cache->external_cache);
25242472
write_void(cache->external_cache, NULL);
2525-
free_binary_op_specialization_descr(data);
25262473
}
25272474

25282475
switch (oparg) {
@@ -2580,14 +2527,8 @@ _Py_Specialize_BinaryOp(_PyStackRef lhs_st, _PyStackRef rhs_st, _Py_CODEUNIT *in
25802527
break;
25812528
}
25822529

2583-
binaryopguardfunc guard = NULL;
2584-
binaryopactionfunc action = NULL;
2585-
if (binary_op_extended_specialization(lhs, rhs, oparg, &guard, &action)) {
2586-
PyBinaryOpSpecializationDescr *descr =
2587-
new_binary_op_specialization_descr(guard, action);
2588-
if (descr == NULL) {
2589-
return -1;
2590-
}
2530+
PyBinaryOpSpecializationDescr *descr;
2531+
if (binary_op_extended_specialization(lhs, rhs, oparg, &descr)) {
25912532
specialize(instr, BINARY_OP_EXTEND);
25922533
write_void(cache->external_cache, (void*)descr);
25932534
return 0;

0 commit comments

Comments
 (0)