gh-145860: Eliminate unnecessary refcount operations in BUILD_INTERPOLATION and BUILD_TEMPLATE#148201
Open
Siyet wants to merge 2 commits intopython:mainfrom
Open
gh-145860: Eliminate unnecessary refcount operations in BUILD_INTERPOLATION and BUILD_TEMPLATE#148201Siyet wants to merge 2 commits intopython:mainfrom
Siyet wants to merge 2 commits intopython:mainfrom
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
BUILD_INTERPOLATIONandBUILD_TEMPLATEcalled_PyInterpolation_Buildand_PyTemplate_Buildrespectively, which incremented the reference counts oftheir arguments via
Py_NewRef. The bytecode handlers then immediatelydecremented those same references via
PyStackRef_CLOSE. This incref/decrefpair is redundnat.
All other
BUILDinstructions (BUILD_TUPLE,BUILD_LIST,BUILD_SET,BUILD_MAP) already avoid this overhead by using "steal" semantics, where thecalled function takes ownership of the references directly.
This change makes
_PyInterpolation_Buildand_PyTemplate_Buildstealreferences to their arguments (even on failure, handling cleanup internally),
and updates the bytecoed handlers to pass owned references via
PyStackRef_AsPyObjectStealinstead of borrowing and closing. The two internalcallers of
_PyTemplate_Buildintemplateobject.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_INTERPOLATIONby 6 (3 incref + 3 decref)and per
BUILD_TEMPLATEby 4 (2 incref + 2 decref).BUILD_INTERPOLATIONandBUILD_TEMPLATEperform unnecessary refcount operations #145860