Skip to content

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

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

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

Conversation

@Siyet

@Siyet Siyet commented Apr 7, 2026

Copy link
Copy Markdown

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

github-actions Bot commented May 9, 2026

Copy link
Copy Markdown

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

@markshannon markshannon left a comment

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.

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

Comment thread Include/internal/pycore_interpolation.h Outdated
#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.

Comment thread Include/internal/pycore_template.h Outdated
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

bedevere-app Bot commented May 28, 2026

Copy link
Copy Markdown

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.

@github-actions github-actions Bot removed the stale Stale PR or inactive for long period of time. label Jun 3, 2026
@github-actions

github-actions Bot commented Sep 5, 2026

Copy link
Copy Markdown

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

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Sep 5, 2026
@bedevere-app bedevere-app Bot added the type-feature A feature request or enhancement label Nov 24, 2026
@python-cla-bot

Copy link
Copy Markdown

The following commit authors need to sign the Contributor License Agreement:

CLA not signed

@Siyet

Siyet commented Nov 24, 2026

Copy link
Copy Markdown
Author

Thx, I rename both to _PyInterpolation_BuildSteal and _PyTemplate_BuildSteal and merged main.

I have made the requested changes; please review again

@bedevere-app

bedevere-app Bot commented Nov 24, 2026

Copy link
Copy Markdown

Thanks for making the requested changes!

@markshannon: please review the changes made to this pull request.

@bedevere-app
bedevere-app Bot requested a review from markshannon November 24, 2026 18:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting change review stale Stale PR or inactive for long period of time. type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants