From: Nadeshiko Manju Date: Tue, 6 Jan 2026 21:11:53 +0000 (+0800) Subject: gh-134584: Eliminate redundant refcounting from `TO_BOOL_STR` (GH-143417) X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0a5c04a5ce56a208ba2a3a0f945d006296766876;p=thirdparty%2FPython%2Fcpython.git gh-134584: Eliminate redundant refcounting from `TO_BOOL_STR` (GH-143417) Signed-off-by: Manjusaka --- diff --git a/Include/internal/pycore_opcode_metadata.h b/Include/internal/pycore_opcode_metadata.h index 05d22ea7c468..489efba8ca02 100644 --- a/Include/internal/pycore_opcode_metadata.h +++ b/Include/internal/pycore_opcode_metadata.h @@ -1307,7 +1307,7 @@ const struct opcode_metadata _PyOpcode_opcode_metadata[267] = { [TO_BOOL_INT] = { true, INSTR_FMT_IXC00, HAS_EXIT_FLAG | HAS_ESCAPES_FLAG }, [TO_BOOL_LIST] = { true, INSTR_FMT_IXC00, HAS_EXIT_FLAG | HAS_ESCAPES_FLAG }, [TO_BOOL_NONE] = { true, INSTR_FMT_IXC00, HAS_EXIT_FLAG }, - [TO_BOOL_STR] = { true, INSTR_FMT_IXC00, HAS_EXIT_FLAG | HAS_ESCAPES_FLAG }, + [TO_BOOL_STR] = { true, INSTR_FMT_IXC00, HAS_EXIT_FLAG }, [TRACE_RECORD] = { true, INSTR_FMT_IB, HAS_ARG_FLAG | HAS_ERROR_FLAG | HAS_ESCAPES_FLAG }, [UNARY_INVERT] = { true, INSTR_FMT_IX, HAS_ERROR_FLAG | HAS_ESCAPES_FLAG }, [UNARY_NEGATIVE] = { true, INSTR_FMT_IX, HAS_ERROR_FLAG | HAS_ESCAPES_FLAG }, @@ -1520,7 +1520,7 @@ _PyOpcode_macro_expansion[256] = { [TO_BOOL_INT] = { .nuops = 1, .uops = { { _TO_BOOL_INT, OPARG_SIMPLE, 3 } } }, [TO_BOOL_LIST] = { .nuops = 2, .uops = { { _GUARD_TOS_LIST, OPARG_SIMPLE, 0 }, { _TO_BOOL_LIST, OPARG_SIMPLE, 3 } } }, [TO_BOOL_NONE] = { .nuops = 1, .uops = { { _TO_BOOL_NONE, OPARG_SIMPLE, 3 } } }, - [TO_BOOL_STR] = { .nuops = 2, .uops = { { _GUARD_TOS_UNICODE, OPARG_SIMPLE, 0 }, { _TO_BOOL_STR, OPARG_SIMPLE, 3 } } }, + [TO_BOOL_STR] = { .nuops = 3, .uops = { { _GUARD_TOS_UNICODE, OPARG_SIMPLE, 0 }, { _TO_BOOL_STR, OPARG_SIMPLE, 3 }, { _POP_TOP_UNICODE, OPARG_SIMPLE, 3 } } }, [UNARY_INVERT] = { .nuops = 1, .uops = { { _UNARY_INVERT, OPARG_SIMPLE, 0 } } }, [UNARY_NEGATIVE] = { .nuops = 1, .uops = { { _UNARY_NEGATIVE, OPARG_SIMPLE, 0 } } }, [UNARY_NOT] = { .nuops = 1, .uops = { { _UNARY_NOT, OPARG_SIMPLE, 0 } } }, diff --git a/Include/internal/pycore_uop_ids.h b/Include/internal/pycore_uop_ids.h index 01f99deb05db..4d71f3526f99 100644 --- a/Include/internal/pycore_uop_ids.h +++ b/Include/internal/pycore_uop_ids.h @@ -1159,22 +1159,24 @@ extern "C" { #define _TO_BOOL_NONE_r11 1353 #define _TO_BOOL_NONE_r22 1354 #define _TO_BOOL_NONE_r33 1355 -#define _TO_BOOL_STR_r11 1356 -#define _TRACE_RECORD_r00 1357 -#define _UNARY_INVERT_r11 1358 -#define _UNARY_NEGATIVE_r11 1359 -#define _UNARY_NOT_r01 1360 -#define _UNARY_NOT_r11 1361 -#define _UNARY_NOT_r22 1362 -#define _UNARY_NOT_r33 1363 -#define _UNPACK_EX_r10 1364 -#define _UNPACK_SEQUENCE_r10 1365 -#define _UNPACK_SEQUENCE_LIST_r10 1366 -#define _UNPACK_SEQUENCE_TUPLE_r10 1367 -#define _UNPACK_SEQUENCE_TWO_TUPLE_r12 1368 -#define _WITH_EXCEPT_START_r33 1369 -#define _YIELD_VALUE_r11 1370 -#define MAX_UOP_REGS_ID 1370 +#define _TO_BOOL_STR_r02 1356 +#define _TO_BOOL_STR_r12 1357 +#define _TO_BOOL_STR_r23 1358 +#define _TRACE_RECORD_r00 1359 +#define _UNARY_INVERT_r11 1360 +#define _UNARY_NEGATIVE_r11 1361 +#define _UNARY_NOT_r01 1362 +#define _UNARY_NOT_r11 1363 +#define _UNARY_NOT_r22 1364 +#define _UNARY_NOT_r33 1365 +#define _UNPACK_EX_r10 1366 +#define _UNPACK_SEQUENCE_r10 1367 +#define _UNPACK_SEQUENCE_LIST_r10 1368 +#define _UNPACK_SEQUENCE_TUPLE_r10 1369 +#define _UNPACK_SEQUENCE_TWO_TUPLE_r12 1370 +#define _WITH_EXCEPT_START_r33 1371 +#define _YIELD_VALUE_r11 1372 +#define MAX_UOP_REGS_ID 1372 #ifdef __cplusplus } diff --git a/Include/internal/pycore_uop_metadata.h b/Include/internal/pycore_uop_metadata.h index 4e0065157332..1613dbd7b9eb 100644 --- a/Include/internal/pycore_uop_metadata.h +++ b/Include/internal/pycore_uop_metadata.h @@ -97,7 +97,7 @@ const uint32_t _PyUop_Flags[MAX_UOP_ID+1] = { [_GUARD_NOS_COMPACT_ASCII] = HAS_EXIT_FLAG, [_GUARD_NOS_UNICODE] = HAS_EXIT_FLAG, [_GUARD_TOS_UNICODE] = HAS_EXIT_FLAG, - [_TO_BOOL_STR] = HAS_ESCAPES_FLAG, + [_TO_BOOL_STR] = 0, [_REPLACE_WITH_TRUE] = 0, [_UNARY_INVERT] = HAS_ERROR_FLAG | HAS_ESCAPES_FLAG, [_GUARD_NOS_INT] = HAS_EXIT_FLAG, @@ -934,11 +934,11 @@ const _PyUopCachingInfo _PyUop_Caching[MAX_UOP_ID+1] = { }, }, [_TO_BOOL_STR] = { - .best = { 1, 1, 1, 1 }, + .best = { 0, 1, 2, 2 }, .entries = { - { -1, -1, -1 }, - { 1, 1, _TO_BOOL_STR_r11 }, - { -1, -1, -1 }, + { 2, 0, _TO_BOOL_STR_r02 }, + { 2, 1, _TO_BOOL_STR_r12 }, + { 3, 2, _TO_BOOL_STR_r23 }, { -1, -1, -1 }, }, }, @@ -3500,7 +3500,9 @@ const uint16_t _PyUop_Uncached[MAX_UOP_REGS_ID+1] = { [_GUARD_TOS_UNICODE_r11] = _GUARD_TOS_UNICODE, [_GUARD_TOS_UNICODE_r22] = _GUARD_TOS_UNICODE, [_GUARD_TOS_UNICODE_r33] = _GUARD_TOS_UNICODE, - [_TO_BOOL_STR_r11] = _TO_BOOL_STR, + [_TO_BOOL_STR_r02] = _TO_BOOL_STR, + [_TO_BOOL_STR_r12] = _TO_BOOL_STR, + [_TO_BOOL_STR_r23] = _TO_BOOL_STR, [_REPLACE_WITH_TRUE_r02] = _REPLACE_WITH_TRUE, [_REPLACE_WITH_TRUE_r12] = _REPLACE_WITH_TRUE, [_REPLACE_WITH_TRUE_r23] = _REPLACE_WITH_TRUE, @@ -5099,7 +5101,9 @@ const char *const _PyOpcode_uop_name[MAX_UOP_REGS_ID+1] = { [_TO_BOOL_NONE_r22] = "_TO_BOOL_NONE_r22", [_TO_BOOL_NONE_r33] = "_TO_BOOL_NONE_r33", [_TO_BOOL_STR] = "_TO_BOOL_STR", - [_TO_BOOL_STR_r11] = "_TO_BOOL_STR_r11", + [_TO_BOOL_STR_r02] = "_TO_BOOL_STR_r02", + [_TO_BOOL_STR_r12] = "_TO_BOOL_STR_r12", + [_TO_BOOL_STR_r23] = "_TO_BOOL_STR_r23", [_UNARY_INVERT] = "_UNARY_INVERT", [_UNARY_INVERT_r11] = "_UNARY_INVERT_r11", [_UNARY_NEGATIVE] = "_UNARY_NEGATIVE", diff --git a/Lib/test/test_capi/test_opt.py b/Lib/test/test_capi/test_opt.py index 271681a80d62..37b2fbe0a714 100644 --- a/Lib/test/test_capi/test_opt.py +++ b/Lib/test/test_capi/test_opt.py @@ -2937,6 +2937,23 @@ class TestUopsOptimization(unittest.TestCase): self.assertLessEqual(count_ops(ex, "_POP_TOP"), 1) self.assertIn("_POP_TOP_NOP", uops) + def test_to_bool_str(self): + def f(n): + for i in range(n): + false = i == TIER2_THRESHOLD + empty = "X"[:false] + if empty: + return 1 + return 0 + + res, ex = self._run_with_optimizer(f, TIER2_THRESHOLD) + self.assertEqual(res, 0) + self.assertIsNotNone(ex) + uops = get_opnames(ex) + self.assertIn("_TO_BOOL_STR", uops) + self.assertLessEqual(count_ops(ex, "_POP_TOP"), 3) + self.assertIn("_POP_TOP_NOP", uops) + def test_to_bool_always_true(self): def testfunc(n): class A: diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-01-04-23-53-42.gh-issue-134584.CNrxI_.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-01-04-23-53-42.gh-issue-134584.CNrxI_.rst new file mode 100644 index 000000000000..cc59971955b7 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-01-04-23-53-42.gh-issue-134584.CNrxI_.rst @@ -0,0 +1 @@ +Eliminate redundant refcounting from ``TO_BOOL_STR``. diff --git a/Python/bytecodes.c b/Python/bytecodes.c index c83f5033af7b..8db1de6ca384 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -547,23 +547,16 @@ dummy_func( EXIT_IF(!PyUnicode_CheckExact(value_o)); } - op(_TO_BOOL_STR, (value -- res)) { + op(_TO_BOOL_STR, (value -- res, v)) { STAT_INC(TO_BOOL, hit); PyObject *value_o = PyStackRef_AsPyObjectBorrow(value); - if (value_o == &_Py_STR(empty)) { - assert(_Py_IsImmortal(value_o)); - DEAD(value); - res = PyStackRef_False; - } - else { - assert(Py_SIZE(value_o)); - PyStackRef_CLOSE(value); - res = PyStackRef_True; - } + res = value_o == &_Py_STR(empty) ? PyStackRef_False : PyStackRef_True; + v = value; + DEAD(value); } macro(TO_BOOL_STR) = - _GUARD_TOS_UNICODE + unused/1 + unused/2 + _TO_BOOL_STR; + _GUARD_TOS_UNICODE + unused/1 + unused/2 + _TO_BOOL_STR + _POP_TOP_UNICODE; op(_REPLACE_WITH_TRUE, (value -- res, v)) { res = PyStackRef_True; diff --git a/Python/executor_cases.c.h b/Python/executor_cases.c.h index a56482d411fe..e78c37611ce6 100644 --- a/Python/executor_cases.c.h +++ b/Python/executor_cases.c.h @@ -3553,30 +3553,62 @@ break; } - case _TO_BOOL_STR_r11: { + case _TO_BOOL_STR_r02: { + CHECK_CURRENT_CACHED_VALUES(0); + assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE()); + _PyStackRef value; + _PyStackRef res; + _PyStackRef v; + value = stack_pointer[-1]; + STAT_INC(TO_BOOL, hit); + PyObject *value_o = PyStackRef_AsPyObjectBorrow(value); + res = value_o == &_Py_STR(empty) ? PyStackRef_False : PyStackRef_True; + v = value; + _tos_cache1 = v; + _tos_cache0 = res; + SET_CURRENT_CACHED_VALUES(2); + stack_pointer += -1; + ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); + assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE()); + break; + } + + case _TO_BOOL_STR_r12: { CHECK_CURRENT_CACHED_VALUES(1); assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE()); _PyStackRef value; _PyStackRef res; + _PyStackRef v; _PyStackRef _stack_item_0 = _tos_cache0; value = _stack_item_0; STAT_INC(TO_BOOL, hit); PyObject *value_o = PyStackRef_AsPyObjectBorrow(value); - if (value_o == &_Py_STR(empty)) { - assert(_Py_IsImmortal(value_o)); - res = PyStackRef_False; - } - else { - assert(Py_SIZE(value_o)); - _PyFrame_SetStackPointer(frame, stack_pointer); - PyStackRef_CLOSE(value); - stack_pointer = _PyFrame_GetStackPointer(frame); - res = PyStackRef_True; - } + res = value_o == &_Py_STR(empty) ? PyStackRef_False : PyStackRef_True; + v = value; + _tos_cache1 = v; _tos_cache0 = res; - _tos_cache1 = PyStackRef_ZERO_BITS; - _tos_cache2 = PyStackRef_ZERO_BITS; - SET_CURRENT_CACHED_VALUES(1); + SET_CURRENT_CACHED_VALUES(2); + assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE()); + break; + } + + case _TO_BOOL_STR_r23: { + CHECK_CURRENT_CACHED_VALUES(2); + assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE()); + _PyStackRef value; + _PyStackRef res; + _PyStackRef v; + _PyStackRef _stack_item_0 = _tos_cache0; + _PyStackRef _stack_item_1 = _tos_cache1; + value = _stack_item_1; + STAT_INC(TO_BOOL, hit); + PyObject *value_o = PyStackRef_AsPyObjectBorrow(value); + res = value_o == &_Py_STR(empty) ? PyStackRef_False : PyStackRef_True; + v = value; + _tos_cache2 = v; + _tos_cache1 = res; + _tos_cache0 = _stack_item_0; + SET_CURRENT_CACHED_VALUES(3); assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE()); break; } diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index b134d6c12cf4..ece1146924bc 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -11759,6 +11759,7 @@ static_assert(INLINE_CACHE_ENTRIES_TO_BOOL == 3, "incorrect cache size"); _PyStackRef value; _PyStackRef res; + _PyStackRef v; // _GUARD_TOS_UNICODE { value = stack_pointer[-1]; @@ -11775,20 +11776,14 @@ { STAT_INC(TO_BOOL, hit); PyObject *value_o = PyStackRef_AsPyObjectBorrow(value); - if (value_o == &_Py_STR(empty)) { - assert(_Py_IsImmortal(value_o)); - res = PyStackRef_False; - } - else { - assert(Py_SIZE(value_o)); - stack_pointer += -1; - ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); - _PyFrame_SetStackPointer(frame, stack_pointer); - PyStackRef_CLOSE(value); - stack_pointer = _PyFrame_GetStackPointer(frame); - res = PyStackRef_True; - stack_pointer += 1; - } + res = value_o == &_Py_STR(empty) ? PyStackRef_False : PyStackRef_True; + v = value; + } + // _POP_TOP_UNICODE + { + value = v; + assert(PyUnicode_CheckExact(PyStackRef_AsPyObjectBorrow(value))); + PyStackRef_CLOSE_SPECIALIZED(value, _PyUnicode_ExactDealloc); } stack_pointer[-1] = res; DISPATCH(); diff --git a/Python/optimizer_analysis.c b/Python/optimizer_analysis.c index 81479a8f28e3..29a088e43c2a 100644 --- a/Python/optimizer_analysis.c +++ b/Python/optimizer_analysis.c @@ -214,7 +214,8 @@ optimize_to_bool( _PyUOpInstruction *this_instr, JitOptContext *ctx, JitOptRef value, - JitOptRef *result_ptr) + JitOptRef *result_ptr, + bool insert_mode) { if (sym_matches_type(value, &PyBool_Type)) { REPLACE_OP(this_instr, _NOP, 0, 0); @@ -224,7 +225,10 @@ optimize_to_bool( int truthiness = sym_truthiness(ctx, value); if (truthiness >= 0) { PyObject *load = truthiness ? Py_True : Py_False; - REPLACE_OP(this_instr, _POP_TOP_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)load); + int opcode = insert_mode ? + _INSERT_1_LOAD_CONST_INLINE_BORROW : + _POP_TOP_LOAD_CONST_INLINE_BORROW; + REPLACE_OP(this_instr, opcode, 0, (uintptr_t)load); *result_ptr = sym_new_const(ctx, load); return 1; } diff --git a/Python/optimizer_bytecodes.c b/Python/optimizer_bytecodes.c index 9388862e7dbc..ef7bc1d8cd87 100644 --- a/Python/optimizer_bytecodes.c +++ b/Python/optimizer_bytecodes.c @@ -44,7 +44,8 @@ optimize_to_bool( _PyUOpInstruction *this_instr, JitOptContext *ctx, JitOptSymbol *value, - JitOptSymbol **result_ptr); + JitOptSymbol **result_ptr, + bool insert_mode); extern void eliminate_pop_guard(_PyUOpInstruction *this_instr, bool exit); @@ -377,21 +378,21 @@ dummy_func(void) { } op(_TO_BOOL, (value -- res)) { - int already_bool = optimize_to_bool(this_instr, ctx, value, &res); + int already_bool = optimize_to_bool(this_instr, ctx, value, &res, false); if (!already_bool) { res = sym_new_truthiness(ctx, value, true); } } op(_TO_BOOL_BOOL, (value -- value)) { - int already_bool = optimize_to_bool(this_instr, ctx, value, &value); + int already_bool = optimize_to_bool(this_instr, ctx, value, &value, false); if (!already_bool) { sym_set_type(value, &PyBool_Type); } } op(_TO_BOOL_INT, (value -- res)) { - int already_bool = optimize_to_bool(this_instr, ctx, value, &res); + int already_bool = optimize_to_bool(this_instr, ctx, value, &res, false); if (!already_bool) { sym_set_type(value, &PyLong_Type); res = sym_new_truthiness(ctx, value, true); @@ -399,14 +400,14 @@ dummy_func(void) { } op(_TO_BOOL_LIST, (value -- res)) { - int already_bool = optimize_to_bool(this_instr, ctx, value, &res); + int already_bool = optimize_to_bool(this_instr, ctx, value, &res, false); if (!already_bool) { res = sym_new_type(ctx, &PyBool_Type); } } op(_TO_BOOL_NONE, (value -- res)) { - int already_bool = optimize_to_bool(this_instr, ctx, value, &res); + int already_bool = optimize_to_bool(this_instr, ctx, value, &res, false); if (!already_bool) { sym_set_const(value, Py_None); res = sym_new_const(ctx, Py_False); @@ -431,8 +432,9 @@ dummy_func(void) { sym_set_type(value, &PyUnicode_Type); } - op(_TO_BOOL_STR, (value -- res)) { - int already_bool = optimize_to_bool(this_instr, ctx, value, &res); + op(_TO_BOOL_STR, (value -- res, v)) { + int already_bool = optimize_to_bool(this_instr, ctx, value, &res, true); + v = value; if (!already_bool) { res = sym_new_truthiness(ctx, value, true); } diff --git a/Python/optimizer_cases.c.h b/Python/optimizer_cases.c.h index 9e877cea8c8d..f25cb6b5fd58 100644 --- a/Python/optimizer_cases.c.h +++ b/Python/optimizer_cases.c.h @@ -299,7 +299,7 @@ JitOptRef value; JitOptRef res; value = stack_pointer[-1]; - int already_bool = optimize_to_bool(this_instr, ctx, value, &res); + int already_bool = optimize_to_bool(this_instr, ctx, value, &res, false); if (!already_bool) { res = sym_new_truthiness(ctx, value, true); } @@ -310,7 +310,7 @@ case _TO_BOOL_BOOL: { JitOptRef value; value = stack_pointer[-1]; - int already_bool = optimize_to_bool(this_instr, ctx, value, &value); + int already_bool = optimize_to_bool(this_instr, ctx, value, &value, false); if (!already_bool) { sym_set_type(value, &PyBool_Type); } @@ -322,7 +322,7 @@ JitOptRef value; JitOptRef res; value = stack_pointer[-1]; - int already_bool = optimize_to_bool(this_instr, ctx, value, &res); + int already_bool = optimize_to_bool(this_instr, ctx, value, &res, false); if (!already_bool) { sym_set_type(value, &PyLong_Type); res = sym_new_truthiness(ctx, value, true); @@ -359,7 +359,7 @@ JitOptRef value; JitOptRef res; value = stack_pointer[-1]; - int already_bool = optimize_to_bool(this_instr, ctx, value, &res); + int already_bool = optimize_to_bool(this_instr, ctx, value, &res, false); if (!already_bool) { res = sym_new_type(ctx, &PyBool_Type); } @@ -371,7 +371,7 @@ JitOptRef value; JitOptRef res; value = stack_pointer[-1]; - int already_bool = optimize_to_bool(this_instr, ctx, value, &res); + int already_bool = optimize_to_bool(this_instr, ctx, value, &res, false); if (!already_bool) { sym_set_const(value, Py_None); res = sym_new_const(ctx, Py_False); @@ -410,12 +410,18 @@ case _TO_BOOL_STR: { JitOptRef value; JitOptRef res; + JitOptRef v; value = stack_pointer[-1]; - int already_bool = optimize_to_bool(this_instr, ctx, value, &res); + int already_bool = optimize_to_bool(this_instr, ctx, value, &res, true); + v = value; if (!already_bool) { res = sym_new_truthiness(ctx, value, true); } + CHECK_STACK_BOUNDS(1); stack_pointer[-1] = res; + stack_pointer[0] = v; + stack_pointer += 1; + ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); break; }