Skip to content

gh-145860: Eliminate unnecessary refcount operations in BUILD_INTERPOLATION and BUILD_TEMPLATE#148201

Open
Siyet wants to merge 2 commits into
python:mainfrom
Siyet:gh-145860-build-interpolation-steal-refs
Open

gh-145860: Eliminate unnecessary refcount operations in BUILD_INTERPOLATION and BUILD_TEMPLATE#148201
Siyet wants to merge 2 commits into
python:mainfrom
Siyet:gh-145860-build-interpolation-steal-refs

Conversation

@Siyet
Copy link
Copy Markdown

@Siyet Siyet commented Apr 7, 2026

BUILD_INTERPOLATION and BUILD_TEMPLATE called _PyInterpolation_Build and
_PyTemplate_Build respectively, which incremented the reference counts of
their arguments via Py_NewRef. The bytecode handlers then immediately
decremented those same references via PyStackRef_CLOSE. This incref/decref
pair is redundnat.

All other BUILD instructions (BUILD_TUPLE, BUILD_LIST, BUILD_SET,
BUILD_MAP) already avoid this overhead by using "steal" semantics, where the
called function takes ownership of the references directly.

This change makes _PyInterpolation_Build and _PyTemplate_Build steal
references to their arguments (even on failure, handling cleanup internally),
and updates the bytecoed handlers to pass owned references via
PyStackRef_AsPyObjectSteal instead of borrowing and closing. The two internal
callers of _PyTemplate_Build in templateobject.c (template_new,
template_concat_templates) are updated accordingly.

This is a pure optimization with no semantic change. Reduces the number of
atomic refcount operations per BUILD_INTERPOLATION by 6 (3 incref + 3 decref)
and per BUILD_TEMPLATE by 4 (2 incref + 2 decref).

…ATION/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.
Add comments to _PyInterpolation_Build and _PyTemplate_Build
declarations in pycore_interpolation.h and pycore_template.h.
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 9, 2026

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label May 9, 2026
Copy link
Copy Markdown
Member

@markshannon markshannon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall.
Can you rename the two functions, adding a "Steal" suffix?

#define _PyInterpolation_CheckExact(op) Py_IS_TYPE((op), &_PyInterpolation_Type)

// Steals references to value, str, and format_spec (even on failure).
PyAPI_FUNC(PyObject *) _PyInterpolation_Build(PyObject *value, PyObject *str,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
PyAPI_FUNC(PyObject *) _PyInterpolation_Build(PyObject *value, PyObject *str,
PyAPI_FUNC(PyObject *) _PyInterpolation_BuildSteal(PyObject *value, PyObject *str,

If a function steals references, we usually add the "Steal" suffix to the name, to make that clear.

extern PyObject *_PyTemplate_Concat(PyObject *self, PyObject *other);

// Steals references to strings and interpolations (even on failure).
PyAPI_FUNC(PyObject *) _PyTemplate_Build(PyObject *strings, PyObject *interpolations);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add "Steal" suffux

@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 28, 2026

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting changes stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants