gh-134584: Eliminate redundant refcounting from _BINARY_OP_SUBSCR_TUPLE_INT (GH-143094)

This commit is contained in:
Hai Zhu 2025-12-24 05:47:12 +08:00 committed by GitHub
parent cbe0cb779a
commit cc48bf0fde
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 65 additions and 34 deletions

View file

@ -975,9 +975,9 @@ dummy_func(
}
macro(BINARY_OP_SUBSCR_TUPLE_INT) =
_GUARD_TOS_INT + _GUARD_NOS_TUPLE + unused/5 + _BINARY_OP_SUBSCR_TUPLE_INT;
_GUARD_TOS_INT + _GUARD_NOS_TUPLE + unused/5 + _BINARY_OP_SUBSCR_TUPLE_INT + _POP_TOP_INT + POP_TOP;
op(_BINARY_OP_SUBSCR_TUPLE_INT, (tuple_st, sub_st -- res)) {
op(_BINARY_OP_SUBSCR_TUPLE_INT, (tuple_st, sub_st -- res, ts, ss)) {
PyObject *sub = PyStackRef_AsPyObjectBorrow(sub_st);
PyObject *tuple = PyStackRef_AsPyObjectBorrow(tuple_st);
@ -991,9 +991,10 @@ dummy_func(
STAT_INC(BINARY_OP, hit);
PyObject *res_o = PyTuple_GET_ITEM(tuple, index);
assert(res_o != NULL);
PyStackRef_CLOSE_SPECIALIZED(sub_st, _PyLong_ExactDealloc);
res = PyStackRef_FromPyObjectNew(res_o);
DECREF_INPUTS();
ts = tuple_st;
ss = sub_st;
INPUTS_DEAD();
}
op(_GUARD_NOS_DICT, (nos, unused -- nos, unused)) {

View file

@ -4888,12 +4888,14 @@
break;
}
case _BINARY_OP_SUBSCR_TUPLE_INT_r21: {
case _BINARY_OP_SUBSCR_TUPLE_INT_r23: {
CHECK_CURRENT_CACHED_VALUES(2);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
_PyStackRef sub_st;
_PyStackRef tuple_st;
_PyStackRef res;
_PyStackRef ts;
_PyStackRef ss;
_PyStackRef _stack_item_0 = _tos_cache0;
_PyStackRef _stack_item_1 = _tos_cache1;
sub_st = _stack_item_1;
@ -4920,23 +4922,13 @@
STAT_INC(BINARY_OP, hit);
PyObject *res_o = PyTuple_GET_ITEM(tuple, index);
assert(res_o != NULL);
PyStackRef_CLOSE_SPECIALIZED(sub_st, _PyLong_ExactDealloc);
res = PyStackRef_FromPyObjectNew(res_o);
stack_pointer[0] = tuple_st;
stack_pointer += 1;
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
_PyFrame_SetStackPointer(frame, stack_pointer);
_PyStackRef tmp = tuple_st;
tuple_st = res;
stack_pointer[-1] = tuple_st;
PyStackRef_CLOSE(tmp);
stack_pointer = _PyFrame_GetStackPointer(frame);
ts = tuple_st;
ss = sub_st;
_tos_cache2 = ss;
_tos_cache1 = ts;
_tos_cache0 = res;
_tos_cache1 = PyStackRef_ZERO_BITS;
_tos_cache2 = PyStackRef_ZERO_BITS;
SET_CURRENT_CACHED_VALUES(1);
stack_pointer += -1;
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
SET_CURRENT_CACHED_VALUES(3);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
break;
}

View file

@ -1024,6 +1024,8 @@
_PyStackRef tuple_st;
_PyStackRef sub_st;
_PyStackRef res;
_PyStackRef ts;
_PyStackRef ss;
// _GUARD_TOS_INT
{
value = stack_pointer[-1];
@ -1067,15 +1069,24 @@
STAT_INC(BINARY_OP, hit);
PyObject *res_o = PyTuple_GET_ITEM(tuple, index);
assert(res_o != NULL);
PyStackRef_CLOSE_SPECIALIZED(sub_st, _PyLong_ExactDealloc);
res = PyStackRef_FromPyObjectNew(res_o);
ts = tuple_st;
ss = sub_st;
}
// _POP_TOP_INT
{
value = ss;
assert(PyLong_CheckExact(PyStackRef_AsPyObjectBorrow(value)));
PyStackRef_CLOSE_SPECIALIZED(value, _PyLong_ExactDealloc);
}
// _POP_TOP
{
value = ts;
stack_pointer[-2] = res;
stack_pointer += -1;
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
_PyFrame_SetStackPointer(frame, stack_pointer);
_PyStackRef tmp = tuple_st;
tuple_st = res;
stack_pointer[-1] = tuple_st;
PyStackRef_CLOSE(tmp);
PyStackRef_XCLOSE(value);
stack_pointer = _PyFrame_GetStackPointer(frame);
}
DISPATCH();

View file

@ -335,7 +335,7 @@ dummy_func(void) {
i = sub_st;
}
op(_BINARY_OP_SUBSCR_TUPLE_INT, (tuple_st, sub_st -- res)) {
op(_BINARY_OP_SUBSCR_TUPLE_INT, (tuple_st, sub_st -- res, ts, ss)) {
assert(sym_matches_type(tuple_st, &PyTuple_Type));
if (sym_is_const(ctx, sub_st)) {
assert(PyLong_CheckExact(sym_get_const(ctx, sub_st)));
@ -354,6 +354,8 @@ dummy_func(void) {
else {
res = sym_new_not_null(ctx);
}
ts = tuple_st;
ss = sub_st;
}
op(_TO_BOOL, (value -- res)) {

View file

@ -836,6 +836,8 @@
JitOptRef sub_st;
JitOptRef tuple_st;
JitOptRef res;
JitOptRef ts;
JitOptRef ss;
sub_st = stack_pointer[-1];
tuple_st = stack_pointer[-2];
assert(sym_matches_type(tuple_st, &PyTuple_Type));
@ -855,9 +857,13 @@
else {
res = sym_new_not_null(ctx);
}
CHECK_STACK_BOUNDS(-1);
ts = tuple_st;
ss = sub_st;
CHECK_STACK_BOUNDS(1);
stack_pointer[-2] = res;
stack_pointer += -1;
stack_pointer[-1] = ts;
stack_pointer[0] = ss;
stack_pointer += 1;
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
break;
}