Repository navigation
feat: add a driver that runs recursive walks without a call per level - #355
Merged
Merged
Conversation
Several Python functions walk a template's nodes or its render output by calling themselves once per nested level, so a deep template costs a Python frame per level in each (issue #299, part of #272). `citry._walk` lets such a walker be written as a generator that yields `Call(child, ...)` where it would call itself. `run` keeps the running generators on a list and returns the walk's result, so the number of Python frames stays the same however deep the walk goes. `walk` does the same as a generator and passes on any value a walker yields that is not a `Call`, for walkers that already yield results through `yield from` chains. The driver behaves like the recursive version, and like a `yield from` chain toward the reader of `walk`: - A child's exception is thrown into its caller at the `yield Call(...)`, including an exception from starting the child. - An exception the reader throws into `walk` reaches the walker that yielded the value. - When the reader stops early, the walkers still running are closed innermost first. An exception raised while one closes is thrown into the next walker out, and leaves `close` if none catches it. A `Call` to a function that is not a generator function, or a non-`Call` value yielded under `run`, raises a `TypeError` at the walker's `yield`. The driver costs about 300 ns per `Call`, so it suits walkers that run once per component class or in `citry check`. The tests check result order, each exception path, cleanup order when the walk is abandoned or a cleanup step raises (caught or not), passed-on values and replies, `yield from` beside `Call`, and that a 100,000-level chain uses as many Python frames as an 8-level one. Co-Authored-By: Claude Opus 5.5 <[email protected]>
JuroOravec
force-pushed
the
perf/walk-driver
branch
from
October 24, 2026 22:27
eea698a to
cac4060
Compare
This was referenced Oct 24, 2026
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.
Part of #299 (deep templates, #272): the shared driver for the walker rewrites.
What this adds
Several Python functions walk a template's nodes, or the nested output of a render, by calling themselves once per nested level. A deep template therefore costs a Python frame per level in each of them, which holds back the nesting limit (#272). #299 rewrites them in later PRs: the walkers that run on every render use hand-written stacks, and the ones that run on a component's first render, in
citry check, or in the editor use this driver.citry/_walk.pylets a recursive walker be written as a generator. Where it would call itself, it yieldsCall(child, ...), and the yield gives back the child's result:run(generator)drives the walk from a list of running generators and returns its result, so the number of Python frames stays the same at any depth.walk(generator)does the same as a generator. It passes on any value a walker yields that is not aCall, and sends the reader's reply back. This is for walkers that already yield results throughyield fromchains, such as_simple_json_nested_operations, and works asresult = yield from walk(...).How the driver behaves
yield Call(...), so atry/exceptthere still sees it.walkreaches the walker that yielded the value.closeif none catches it.Callto a function that is not a generator function, or a non-Callvalue underrun, raises aTypeErrorat the walker'syield.The reviewer compared 17 close and throw cases against an equivalent
yield fromchain. 13 behave the same. In the other 4, the module docstring names the difference: a walker that ignoresGeneratorExitruns its cleanup later; a walker that yields after catching a close error gets aRuntimeErrorwith a different message, and aCallit yields there never starts; andwalk'sclosereturns no value even when the outermost walker returns one.Cost: about 300 ns per
Callagainst about 20 ns for a direct call. So the driver suits code that runs once per component class or incitry check. A walker that runs on every render and visits many nodes should use a hand-written stack.Tests
tests/test_walk.pycovers:yield frombesideCall;The reviewer made 9 targeted changes to the driver, one at a time, and a test failed for each.
Checks: ruff, mypy (macOS and
--platform linux), and the new tests. Nothing imports the module yet, so this changes no existing behavior. No changelog entry, since there is no user-visible change.🤖 Generated with Claude Code