diff --git a/Include/internal/pycore_interpolation.h b/Include/internal/pycore_interpolation.h index dd610c1609c209..86577819647a35 100644 --- a/Include/internal/pycore_interpolation.h +++ b/Include/internal/pycore_interpolation.h @@ -13,8 +13,9 @@ extern PyTypeObject _PyInterpolation_Type; #define _PyInterpolation_CheckExact(op) Py_IS_TYPE((op), &_PyInterpolation_Type) -PyAPI_FUNC(PyObject *) _PyInterpolation_Build(PyObject *value, PyObject *str, - int conversion, PyObject *format_spec); +// Steals references to value, str, and format_spec (even on failure). +PyAPI_FUNC(PyObject *) _PyInterpolation_BuildSteal(PyObject *value, PyObject *str, + int conversion, PyObject *format_spec); extern PyStatus _PyInterpolation_InitTypes(PyInterpreterState *interp); extern PyObject *_PyInterpolation_GetValueRef(PyObject *interpolation); diff --git a/Include/internal/pycore_template.h b/Include/internal/pycore_template.h index f2f8bf9912d3f6..e4ddb2dc0695f8 100644 --- a/Include/internal/pycore_template.h +++ b/Include/internal/pycore_template.h @@ -17,7 +17,8 @@ extern PyTypeObject _PyTemplateIter_Type; extern PyObject *_PyTemplate_Concat(PyObject *self, PyObject *other); -PyAPI_FUNC(PyObject *) _PyTemplate_Build(PyObject *strings, PyObject *interpolations); +// Steals references to strings and interpolations (even on failure). +PyAPI_FUNC(PyObject *) _PyTemplate_BuildSteal(PyObject *strings, PyObject *interpolations); #ifdef __cplusplus } diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-04-07-12-00-00.gh-issue-145860.pgec7ElZ.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-04-07-12-00-00.gh-issue-145860.pgec7ElZ.rst new file mode 100644 index 00000000000000..7702885d4e9520 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-04-07-12-00-00.gh-issue-145860.pgec7ElZ.rst @@ -0,0 +1,4 @@ +Eliminate unnecessary reference count operations in ``BUILD_INTERPOLATION`` +and ``BUILD_TEMPLATE`` bytecode instructions by making +:c:func:`!_PyInterpolation_BuildSteal` and :c:func:`!_PyTemplate_BuildSteal` +steal references to their arguments. diff --git a/Modules/_testinternalcapi/test_cases.c.h b/Modules/_testinternalcapi/test_cases.c.h index 8bb3f857437792..e835bd7e6c24e6 100644 --- a/Modules/_testinternalcapi/test_cases.c.h +++ b/Modules/_testinternalcapi/test_cases.c.h @@ -1513,42 +1513,21 @@ format = &stack_pointer[-(oparg & 1)]; str = stack_pointer[-1 - (oparg & 1)]; value = stack_pointer[-2 - (oparg & 1)]; - PyObject *value_o = PyStackRef_AsPyObjectBorrow(value); - PyObject *str_o = PyStackRef_AsPyObjectBorrow(str); + PyObject *value_o = PyStackRef_AsPyObjectSteal(value); + PyObject *str_o = PyStackRef_AsPyObjectSteal(str); int conversion = oparg >> 2; PyObject *format_o; if (oparg & 1) { - format_o = PyStackRef_AsPyObjectBorrow(format[0]); + format_o = PyStackRef_AsPyObjectSteal(format[0]); } else { - format_o = &_Py_STR(empty); + format_o = Py_NewRef(&_Py_STR(empty)); } - _PyFrame_SetStackPointer(frame, stack_pointer); - _PyFrame_StackPointerValidate(frame); - PyObject *interpolation_o = _PyInterpolation_Build(value_o, str_o, conversion, format_o); - _PyFrame_StackPointerInvalidate(frame); - if (oparg & 1) { - stack_pointer += -(oparg & 1); - ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); - _PyFrame_SetStackPointer(frame, stack_pointer); - _PyFrame_StackPointerValidate(frame); - PyStackRef_CLOSE(format[0]); - _PyFrame_StackPointerInvalidate(frame); - } - else { - stack_pointer += -(oparg & 1); - } - stack_pointer += -1; + stack_pointer += -2 - (oparg & 1); ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); _PyFrame_SetStackPointer(frame, stack_pointer); _PyFrame_StackPointerValidate(frame); - PyStackRef_CLOSE(str); - _PyFrame_StackPointerInvalidate(frame); - stack_pointer += -1; - ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); - _PyFrame_SetStackPointer(frame, stack_pointer); - _PyFrame_StackPointerValidate(frame); - PyStackRef_CLOSE(value); + PyObject *interpolation_o = _PyInterpolation_BuildSteal(value_o, str_o, conversion, format_o); _PyFrame_StackPointerInvalidate(frame); if (interpolation_o == NULL) { JUMP_TO_LABEL(error); @@ -1750,23 +1729,13 @@ _PyStackRef template; interpolations = stack_pointer[-1]; strings = stack_pointer[-2]; - PyObject *strings_o = PyStackRef_AsPyObjectBorrow(strings); - PyObject *interpolations_o = PyStackRef_AsPyObjectBorrow(interpolations); - _PyFrame_SetStackPointer(frame, stack_pointer); - _PyFrame_StackPointerValidate(frame); - PyObject *template_o = _PyTemplate_Build(strings_o, interpolations_o); - _PyFrame_StackPointerInvalidate(frame); - stack_pointer += -1; - ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); - _PyFrame_SetStackPointer(frame, stack_pointer); - _PyFrame_StackPointerValidate(frame); - PyStackRef_CLOSE(interpolations); - _PyFrame_StackPointerInvalidate(frame); - stack_pointer += -1; + PyObject *strings_o = PyStackRef_AsPyObjectSteal(strings); + PyObject *interpolations_o = PyStackRef_AsPyObjectSteal(interpolations); + stack_pointer += -2; ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); _PyFrame_SetStackPointer(frame, stack_pointer); _PyFrame_StackPointerValidate(frame); - PyStackRef_CLOSE(strings); + PyObject *template_o = _PyTemplate_BuildSteal(strings_o, interpolations_o); _PyFrame_StackPointerInvalidate(frame); if (template_o == NULL) { JUMP_TO_LABEL(error); diff --git a/Modules/_testinternalcapi/testbytecodes.c b/Modules/_testinternalcapi/testbytecodes.c index da3025d1f4fdca..a561b40044c0b7 100644 --- a/Modules/_testinternalcapi/testbytecodes.c +++ b/Modules/_testinternalcapi/testbytecodes.c @@ -15,7 +15,7 @@ #include "pycore_emscripten_signal.h" // _Py_CHECK_EMSCRIPTEN_SIGNALS #include "pycore_function.h" #include "pycore_instruments.h" -#include "pycore_interpolation.h" // _PyInterpolation_Build() +#include "pycore_interpolation.h" // _PyInterpolation_BuildSteal() #include "pycore_intrinsics.h" #include "pycore_long.h" // _PyLong_ExactDealloc(), _PyLong_GetZero() #include "pycore_moduleobject.h" // PyModuleObject @@ -29,7 +29,7 @@ #include "pycore_setobject.h" // _PySet_NextEntry() #include "pycore_sliceobject.h" // _PyBuildSlice_ConsumeRefs #include "pycore_stackref.h" -#include "pycore_template.h" // _PyTemplate_Build() +#include "pycore_template.h" // _PyTemplate_BuildSteal() #include "pycore_tuple.h" // _PyTuple_ITEMS() #include "pycore_typeobject.h" // _PySuper_Lookup() diff --git a/Objects/interpolationobject.c b/Objects/interpolationobject.c index 96fabcf201bb97..02ca1ebe928867 100644 --- a/Objects/interpolationobject.c +++ b/Objects/interpolationobject.c @@ -188,16 +188,19 @@ _PyInterpolation_InitTypes(PyInterpreterState *interp) } PyObject * -_PyInterpolation_Build(PyObject *value, PyObject *str, int conversion, PyObject *format_spec) +_PyInterpolation_BuildSteal(PyObject *value, PyObject *str, int conversion, PyObject *format_spec) { interpolationobject *interpolation = PyObject_GC_New(interpolationobject, &_PyInterpolation_Type); if (!interpolation) { + Py_DECREF(value); + Py_DECREF(str); + Py_DECREF(format_spec); return NULL; } - interpolation->value = Py_NewRef(value); - interpolation->expression = Py_NewRef(str); - interpolation->format_spec = Py_NewRef(format_spec); + interpolation->value = value; + interpolation->expression = str; + interpolation->format_spec = format_spec; interpolation->conversion = NULL; if (conversion == 0) { diff --git a/Objects/templateobject.c b/Objects/templateobject.c index de7448ae882e14..d1b32717ed22b0 100644 --- a/Objects/templateobject.c +++ b/Objects/templateobject.c @@ -186,10 +186,7 @@ template_new_impl(PyTypeObject *type, PyObject * const *args, PyTuple_SET_ITEM(strings, stringsidx++, &_Py_STR(empty)); } - PyObject *template = _PyTemplate_Build(strings, interpolations); - Py_DECREF(strings); - Py_DECREF(interpolations); - return template; + return _PyTemplate_BuildSteal(strings, interpolations); } static void @@ -304,10 +301,7 @@ template_concat_templates(templateobject *self, templateobject *other) return NULL; } - PyObject *newtemplate = _PyTemplate_Build(newstrings, newinterpolations); - Py_DECREF(newstrings); - Py_DECREF(newinterpolations); - return newtemplate; + return _PyTemplate_BuildSteal(newstrings, newinterpolations); } PyObject * @@ -415,15 +409,17 @@ PyTypeObject _PyTemplate_Type = { }; PyObject * -_PyTemplate_Build(PyObject *strings, PyObject *interpolations) +_PyTemplate_BuildSteal(PyObject *strings, PyObject *interpolations) { templateobject *template = PyObject_GC_New(templateobject, &_PyTemplate_Type); if (template == NULL) { + Py_DECREF(strings); + Py_DECREF(interpolations); return NULL; } - template->strings = Py_NewRef(strings); - template->interpolations = Py_NewRef(interpolations); + template->strings = strings; + template->interpolations = interpolations; PyObject_GC_Track(template); return (PyObject *) template; } diff --git a/Python/bytecodes.c b/Python/bytecodes.c index c6f1f61f3e08fe..080a17af798989 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -18,7 +18,7 @@ #include "pycore_function.h" #include "pycore_import.h" // _PyImport_LoadLazyImportTstate() #include "pycore_instruments.h" -#include "pycore_interpolation.h" // _PyInterpolation_Build() +#include "pycore_interpolation.h" // _PyInterpolation_BuildSteal() #include "pycore_intrinsics.h" #include "pycore_lazyimportobject.h" // PyLazyImport_CheckExact() #include "pycore_long.h" // _PyLong_ExactDealloc(), _PyLong_GetZero() @@ -33,7 +33,7 @@ #include "pycore_setobject.h" // _PySet_NextEntry() #include "pycore_sliceobject.h" // _PyBuildSlice_ConsumeRefs #include "pycore_stackref.h" -#include "pycore_template.h" // _PyTemplate_Build() +#include "pycore_template.h" // _PyTemplate_BuildSteal() #include "pycore_tuple.h" // _PyStolenTuple_Free(), _PyTuple_ITEMS() #include "pycore_typeobject.h" // _PySuper_Lookup() @@ -2432,35 +2432,31 @@ dummy_func( } inst(BUILD_INTERPOLATION, (value, str, format[oparg & 1] -- interpolation)) { - PyObject *value_o = PyStackRef_AsPyObjectBorrow(value); - PyObject *str_o = PyStackRef_AsPyObjectBorrow(str); + PyObject *value_o = PyStackRef_AsPyObjectSteal(value); + DEAD(value); + PyObject *str_o = PyStackRef_AsPyObjectSteal(str); + DEAD(str); int conversion = oparg >> 2; PyObject *format_o; if (oparg & 1) { - format_o = PyStackRef_AsPyObjectBorrow(format[0]); - } - else { - format_o = &_Py_STR(empty); - } - PyObject *interpolation_o = _PyInterpolation_Build(value_o, str_o, conversion, format_o); - if (oparg & 1) { - PyStackRef_CLOSE(format[0]); + format_o = PyStackRef_AsPyObjectSteal(format[0]); + DEAD(format); } else { + format_o = Py_NewRef(&_Py_STR(empty)); DEAD(format); } - PyStackRef_CLOSE(str); - PyStackRef_CLOSE(value); + PyObject *interpolation_o = _PyInterpolation_BuildSteal(value_o, str_o, conversion, format_o); ERROR_IF(interpolation_o == NULL); interpolation = PyStackRef_FromPyObjectSteal(interpolation_o); } inst(BUILD_TEMPLATE, (strings, interpolations -- template)) { - PyObject *strings_o = PyStackRef_AsPyObjectBorrow(strings); - PyObject *interpolations_o = PyStackRef_AsPyObjectBorrow(interpolations); - PyObject *template_o = _PyTemplate_Build(strings_o, interpolations_o); - PyStackRef_CLOSE(interpolations); - PyStackRef_CLOSE(strings); + PyObject *strings_o = PyStackRef_AsPyObjectSteal(strings); + DEAD(strings); + PyObject *interpolations_o = PyStackRef_AsPyObjectSteal(interpolations); + DEAD(interpolations); + PyObject *template_o = _PyTemplate_BuildSteal(strings_o, interpolations_o); ERROR_IF(template_o == NULL); template = PyStackRef_FromPyObjectSteal(template_o); } diff --git a/Python/ceval.h b/Python/ceval.h index 00db67265a4f3a..aa1829d2294d02 100644 --- a/Python/ceval.h +++ b/Python/ceval.h @@ -17,7 +17,7 @@ #include "pycore_import.h" // _PyImport_IsDefaultImportFunc() #include "pycore_instruments.h" #include "pycore_interpframe.h" // _PyFrame_SetStackPointer() -#include "pycore_interpolation.h" // _PyInterpolation_Build() +#include "pycore_interpolation.h" // _PyInterpolation_BuildSteal() #include "pycore_intrinsics.h" #include "pycore_jit.h" #include "pycore_lazyimportobject.h" @@ -35,7 +35,7 @@ #include "pycore_setobject.h" // _PySet_Update() #include "pycore_sliceobject.h" // _PyBuildSlice_ConsumeRefs #include "pycore_sysmodule.h" // _PySys_GetOptionalAttrString() -#include "pycore_template.h" // _PyTemplate_Build() +#include "pycore_template.h" // _PyTemplate_BuildSteal() #include "pycore_traceback.h" // _PyTraceBack_FromFrame #include "pycore_tuple.h" // _PyTuple_ITEMS() #include "pycore_uop_ids.h" // Uops diff --git a/Python/executor_cases.c.h b/Python/executor_cases.c.h index 5df8120e7424b9..7aecdc96d26cf1 100644 --- a/Python/executor_cases.c.h +++ b/Python/executor_cases.c.h @@ -10904,42 +10904,21 @@ format = &stack_pointer[-(oparg & 1)]; str = stack_pointer[-1 - (oparg & 1)]; value = stack_pointer[-2 - (oparg & 1)]; - PyObject *value_o = PyStackRef_AsPyObjectBorrow(value); - PyObject *str_o = PyStackRef_AsPyObjectBorrow(str); + PyObject *value_o = PyStackRef_AsPyObjectSteal(value); + PyObject *str_o = PyStackRef_AsPyObjectSteal(str); int conversion = oparg >> 2; PyObject *format_o; if (oparg & 1) { - format_o = PyStackRef_AsPyObjectBorrow(format[0]); - } - else { - format_o = &_Py_STR(empty); - } - _PyFrame_SetStackPointer(frame, stack_pointer); - _PyFrame_StackPointerValidate(frame); - PyObject *interpolation_o = _PyInterpolation_Build(value_o, str_o, conversion, format_o); - _PyFrame_StackPointerInvalidate(frame); - if (oparg & 1) { - stack_pointer += -(oparg & 1); - ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); - _PyFrame_SetStackPointer(frame, stack_pointer); - _PyFrame_StackPointerValidate(frame); - PyStackRef_CLOSE(format[0]); - _PyFrame_StackPointerInvalidate(frame); + format_o = PyStackRef_AsPyObjectSteal(format[0]); } else { - stack_pointer += -(oparg & 1); + format_o = Py_NewRef(&_Py_STR(empty)); } - stack_pointer += -1; + stack_pointer += -2 - (oparg & 1); ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); _PyFrame_SetStackPointer(frame, stack_pointer); _PyFrame_StackPointerValidate(frame); - PyStackRef_CLOSE(str); - _PyFrame_StackPointerInvalidate(frame); - stack_pointer += -1; - ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); - _PyFrame_SetStackPointer(frame, stack_pointer); - _PyFrame_StackPointerValidate(frame); - PyStackRef_CLOSE(value); + PyObject *interpolation_o = _PyInterpolation_BuildSteal(value_o, str_o, conversion, format_o); _PyFrame_StackPointerInvalidate(frame); if (interpolation_o == NULL) { SET_CURRENT_CACHED_VALUES(0); @@ -10964,27 +10943,11 @@ _PyStackRef _stack_item_1 = _tos_cache1; interpolations = _stack_item_1; strings = _stack_item_0; - PyObject *strings_o = PyStackRef_AsPyObjectBorrow(strings); - PyObject *interpolations_o = PyStackRef_AsPyObjectBorrow(interpolations); - stack_pointer[0] = strings; - stack_pointer[1] = interpolations; - stack_pointer += 2; - ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); - _PyFrame_SetStackPointer(frame, stack_pointer); - _PyFrame_StackPointerValidate(frame); - PyObject *template_o = _PyTemplate_Build(strings_o, interpolations_o); - _PyFrame_StackPointerInvalidate(frame); - stack_pointer += -1; - ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); - _PyFrame_SetStackPointer(frame, stack_pointer); - _PyFrame_StackPointerValidate(frame); - PyStackRef_CLOSE(interpolations); - _PyFrame_StackPointerInvalidate(frame); - stack_pointer += -1; - ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); + PyObject *strings_o = PyStackRef_AsPyObjectSteal(strings); + PyObject *interpolations_o = PyStackRef_AsPyObjectSteal(interpolations); _PyFrame_SetStackPointer(frame, stack_pointer); _PyFrame_StackPointerValidate(frame); - PyStackRef_CLOSE(strings); + PyObject *template_o = _PyTemplate_BuildSteal(strings_o, interpolations_o); _PyFrame_StackPointerInvalidate(frame); if (template_o == NULL) { SET_CURRENT_CACHED_VALUES(0); diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 3af7863e80d2c6..1845cb74c82447 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -1513,42 +1513,21 @@ format = &stack_pointer[-(oparg & 1)]; str = stack_pointer[-1 - (oparg & 1)]; value = stack_pointer[-2 - (oparg & 1)]; - PyObject *value_o = PyStackRef_AsPyObjectBorrow(value); - PyObject *str_o = PyStackRef_AsPyObjectBorrow(str); + PyObject *value_o = PyStackRef_AsPyObjectSteal(value); + PyObject *str_o = PyStackRef_AsPyObjectSteal(str); int conversion = oparg >> 2; PyObject *format_o; if (oparg & 1) { - format_o = PyStackRef_AsPyObjectBorrow(format[0]); + format_o = PyStackRef_AsPyObjectSteal(format[0]); } else { - format_o = &_Py_STR(empty); + format_o = Py_NewRef(&_Py_STR(empty)); } - _PyFrame_SetStackPointer(frame, stack_pointer); - _PyFrame_StackPointerValidate(frame); - PyObject *interpolation_o = _PyInterpolation_Build(value_o, str_o, conversion, format_o); - _PyFrame_StackPointerInvalidate(frame); - if (oparg & 1) { - stack_pointer += -(oparg & 1); - ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); - _PyFrame_SetStackPointer(frame, stack_pointer); - _PyFrame_StackPointerValidate(frame); - PyStackRef_CLOSE(format[0]); - _PyFrame_StackPointerInvalidate(frame); - } - else { - stack_pointer += -(oparg & 1); - } - stack_pointer += -1; + stack_pointer += -2 - (oparg & 1); ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); _PyFrame_SetStackPointer(frame, stack_pointer); _PyFrame_StackPointerValidate(frame); - PyStackRef_CLOSE(str); - _PyFrame_StackPointerInvalidate(frame); - stack_pointer += -1; - ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); - _PyFrame_SetStackPointer(frame, stack_pointer); - _PyFrame_StackPointerValidate(frame); - PyStackRef_CLOSE(value); + PyObject *interpolation_o = _PyInterpolation_BuildSteal(value_o, str_o, conversion, format_o); _PyFrame_StackPointerInvalidate(frame); if (interpolation_o == NULL) { JUMP_TO_LABEL(error); @@ -1750,23 +1729,13 @@ _PyStackRef template; interpolations = stack_pointer[-1]; strings = stack_pointer[-2]; - PyObject *strings_o = PyStackRef_AsPyObjectBorrow(strings); - PyObject *interpolations_o = PyStackRef_AsPyObjectBorrow(interpolations); - _PyFrame_SetStackPointer(frame, stack_pointer); - _PyFrame_StackPointerValidate(frame); - PyObject *template_o = _PyTemplate_Build(strings_o, interpolations_o); - _PyFrame_StackPointerInvalidate(frame); - stack_pointer += -1; - ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); - _PyFrame_SetStackPointer(frame, stack_pointer); - _PyFrame_StackPointerValidate(frame); - PyStackRef_CLOSE(interpolations); - _PyFrame_StackPointerInvalidate(frame); - stack_pointer += -1; + PyObject *strings_o = PyStackRef_AsPyObjectSteal(strings); + PyObject *interpolations_o = PyStackRef_AsPyObjectSteal(interpolations); + stack_pointer += -2; ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); _PyFrame_SetStackPointer(frame, stack_pointer); _PyFrame_StackPointerValidate(frame); - PyStackRef_CLOSE(strings); + PyObject *template_o = _PyTemplate_BuildSteal(strings_o, interpolations_o); _PyFrame_StackPointerInvalidate(frame); if (template_o == NULL) { JUMP_TO_LABEL(error);