gh-150942: Speed up FrameLocalsProxy.values() - #156055
Open
catlover-bot wants to merge 1 commit into
Open
Conversation
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
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.
Summary
Speed up
FrameLocalsProxy.values()by using_PyList_AppendTakeRef()when adding values returned byframelocalsproxy_getval().framelocalsproxy_getval()returns a new reference. Previously,PyList_Append()added another reference to the value and the originalreference was then decremented by the caller.
Using
_PyList_AppendTakeRef()transfers the existing reference directlyto the result list, avoiding the unnecessary incref/decref pair while
preserving ownership and error handling.
Benchmarks
Benchmarks used
pyperf 2.10.0with CPython 3.16.0a0 built using--enable-optimizations --with-lto.Environment:
FrameLocalsProxy.values()microbenchmarkFrames contained ordinary heap objects rather than immortal small
integers.
An earlier independent run produced a 1.16x geometric-mean speedup.
Tracing/debugger-like workload
A
sys.settrace()workload was also tested where the trace functionenumerates
frame.f_locals.values()on line events.The improvement is mostly hidden by surrounding tracing overhead in the
smaller workloads, while the 256-local case still shows a measurable
improvement.
Validation
python -m test test_frame: passedpython -m test -R 3:3 test_frame: passedgit diff --check: passedAI assistance was used while investigating this issue, designing the
benchmarks, and preparing the change. I reviewed the implementation,
reference ownership semantics, benchmark methodology, and test results
myself.