Files
langgraph/libs
Sydney RunkleandClaude Sonnet 4.6 8cd53c408d perf(pregel): cache source+AST analysis in get_function_nonlocals by code object
`get_function_nonlocals` was parsing Python source and walking the AST
on every graph compilation to discover which closure variables a node
function references. For a typical `create_agent` call this ran
`inspect.getsource` + `ast.parse` + visitor traversal on the same
`model_node`/`tool_node` closures every time — with zero caching.

Split into `_get_nonlocal_names(code)` (cached by code object via
`@lru_cache`) + a lightweight `get_function_nonlocals` wrapper that
only calls the cheap `inspect.getclosurevars` on each invocation.
Add a fast-path early exit for functions with no free variables
(`co_freevars` empty).

In profiling, `find_subgraph_pregel` + `get_function_nonlocals` +
stdlib `dis`/`inspect`/`ast` accounted for ~54% of `create_agent` wall
time; this reduces that to a single cache miss per unique function
definition.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-22 16:13:24 -04:00
..