perf[next]: read free variables from the closure cells - #2869
Merged
Conversation
`get_closure_vars_from_function` called `inspect.getclosurevars` only for its `nonlocals`, but `getclosurevars` disassembles the whole function to also find the global names, which are taken from the cached symbol-table analysis anyway. Read the free variables from `co_freevars` and the closure cells directly, in a function named for what it does. Claude-Session: https://claude.ai/code/session_01Sr9xdMdZgG4wjLLAKbweYb
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.
Description
get_closure_vars_from_functioncallsinspect.getclosurevarsonly for itsnonlocals, butgetclosurevarsdisassembles the whole function to also find the global names, which since #2866 come from the cached symbol-table analysis anyway. Read the free variables fromco_freevarsand the closure cells directly, in_free_variables_from_closure_cells.The result is identical (checked on 21 function shapes on 3.12, 3.13 and 3.14); per call the collector goes from the cost of the disassembly (0.1 ms for a 5-line operator, 7 ms for a 200-line one) to a dictionary lookup.
Requirements
https://claude.ai/code/session_01Sr9xdMdZgG4wjLLAKbweYb