Skip to content

perf[next]: read free variables from the closure cells - #2869

Merged
havogt merged 1 commit into
GridTools:mainfrom
havogt:closure-var-free-vars-from-cells
Sep 9, 2026
Merged

perf[next]: read free variables from the closure cells#2869
havogt merged 1 commit into
GridTools:mainfrom
havogt:closure-var-free-vars-from-cells

Conversation

@havogt

@havogt havogt commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Description

get_closure_vars_from_function calls inspect.getclosurevars only for its nonlocals, but getclosurevars disassembles the whole function to also find the global names, which since #2866 come from the cached symbol-table analysis anyway. Read the free variables from co_freevars and 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

  • All fixes and/or new features come with corresponding tests.
  • Important design decisions have been documented in the appropriate ADR inside the docs/development/ADRs/ folder.

https://claude.ai/code/session_01Sr9xdMdZgG4wjLLAKbweYb

`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
@havogt
havogt requested a review from egparedes September 9, 2026 14:26

@egparedes egparedes left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@havogt
havogt merged commit 43dd688 into GridTools:main Sep 9, 2026
24 checks passed
@havogt
havogt deleted the closure-var-free-vars-from-cells branch September 9, 2026 15:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants