mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-08-20 22:52:29 +02:00
`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>