Skip to content

feat: add a driver that runs recursive walks without a call per level - #355

Merged
JuroOravec merged 1 commit into
mainfrom
perf/walk-driver
Oct 24, 2026
Merged

JuroOravec merged 1 commit into
mainfrom
perf/walk-driver

Conversation

@JuroOravec

@JuroOravec JuroOravec commented Oct 24, 2026 •

Copy link
Copy Markdown
Member

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.py lets a recursive walker be written as a generator. Where it would call itself, it yields Call(child, ...), and the yield gives back the child's result:

def depth(node):
    best = 0
    for child in node.children:
        best = max(best, (yield Call(depth, child)))
    return best + 1

run(depth(root))
  • 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 a Call, and sends the reader's reply back. This is for walkers that already yield results through yield from chains, such as _simple_json_nested_operations, and works as result = yield from walk(...).

How the driver behaves

  • A child's exception, including one raised while starting the child, is thrown into its caller at the yield Call(...), so a try/except there still sees it.
  • 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 under run, raises a TypeError at the walker's yield.

The reviewer compared 17 close and throw cases against an equivalent yield from chain. 13 behave the same. In the other 4, the module docstring names the difference: a walker that ignores GeneratorExit runs its cleanup later; a walker that yields after catching a close error gets a RuntimeError with a different message, and a Call it yields there never starts; and walk's close returns no value even when the outermost walker returns one.

Cost: about 300 ns per Call against about 20 ns for a direct call. So the driver suits code that runs once per component class or in citry check. A walker that runs on every render and visits many nodes should use a hand-written stack.

Tests

tests/test_walk.py covers:

  • result order against the recursive version;
  • each exception path;
  • cleanup order when the walk is abandoned (checked while the driver is still referenced, so garbage collection cannot do it) and when a cleanup step raises, caught or not, including a walker that yields while being closed;
  • passed-on values and replies, and yield from beside Call;
  • that a 100,000-level chain uses as many Python frames as an 8-level one.

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

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]>
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.

1 participant