Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
gh-145860: Eliminate unnecessary refcount ops in BUILD_INTERPOLATION/…
…BUILD_TEMPLATE

Make _PyInterpolation_Build and _PyTemplate_Build steal references to
their arguments instead of borrowing and incref'ing them. This avoids
redundant incref/decref pairs in the bytecode handlers, matching the
pattern already used by all other BUILD instructions.
  • Loading branch information
aleksandr.tseluyko committed Apr 7, 2026
commit 8cc4cc6afa45e5290c62cc16bee5f0b8d9586a02
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Eliminate unnecessary reference count operations in ``BUILD_INTERPOLATION``
and ``BUILD_TEMPLATE`` bytecode instructions by making
:c:func:`!_PyInterpolation_Build` and :c:func:`!_PyTemplate_Build` steal
references to their arguments.
46 changes: 10 additions & 36 deletions Modules/_testinternalcapi/test_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions Objects/interpolationobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,15 @@ _PyInterpolation_Build(PyObject *value, PyObject *str, int conversion, PyObject
{
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) {
Expand Down
16 changes: 6 additions & 10 deletions Objects/templateobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,7 @@ template_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
PyTuple_SET_ITEM(strings, stringsidx++, &_Py_STR(empty));
}

PyObject *template = _PyTemplate_Build(strings, interpolations);
Py_DECREF(strings);
Py_DECREF(interpolations);
return template;
return _PyTemplate_Build(strings, interpolations);
}

static void
Expand Down Expand Up @@ -292,10 +289,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_Build(newstrings, newinterpolations);
}

PyObject *
Expand Down Expand Up @@ -402,11 +396,13 @@ _PyTemplate_Build(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;
}
28 changes: 12 additions & 16 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2329,35 +2329,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_Build(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 *strings_o = PyStackRef_AsPyObjectSteal(strings);
DEAD(strings);
PyObject *interpolations_o = PyStackRef_AsPyObjectSteal(interpolations);
DEAD(interpolations);
PyObject *template_o = _PyTemplate_Build(strings_o, interpolations_o);
PyStackRef_CLOSE(interpolations);
PyStackRef_CLOSE(strings);
ERROR_IF(template_o == NULL);
template = PyStackRef_FromPyObjectSteal(template_o);
}
Expand Down
48 changes: 8 additions & 40 deletions Python/executor_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 10 additions & 36 deletions Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading