Skip to content

perf: walk parsed templates in check and the LSP without one Python call per level - #377

Merged
JuroOravec merged 10 commits into
mainfrom
perf/parsed-ast-walkers
Oct 25, 2026
Merged

JuroOravec merged 10 commits into
mainfrom
perf/parsed-ast-walkers

Conversation

@JuroOravec

Copy link
Copy Markdown
Member

Part of #272. This is PR E4 from the plan for #272: the code that walks a template's parsed tree in citry check, the LSP and citry.analysis (the API that reports what a template uses) no longer calls itself once per level of nesting. So citry check on a deeply nested template needs about the same Python stack at any depth.

A test that caps the Python stack at 256 levels of nesting (test 5 in the plan), the changelog entry for all of PR E, and the extension-authoring docs note come in a small follow-up PR, right after #364 merges.

What was wrong

citry check and the LSP run many walkers over a template's parsed tree. Among them:

  • the markup, registry, CSS and Alpine linters;
  • the browser-expression collectors;
  • the provide-key, component-graph and IDE tag collectors;
  • the template's Python queries;
  • the LSP's own symbol, reference and binding lookups;
  • the Vue static-template writer in _vue/static_template.py.

Each called itself once per nested element, branch, component body or template attribute. citry check on a template of 128 nested <div> tags peaked at 137 Python stack frames, and on 128 nested <c-if> tags at 395. A template 400 <c-if> tags deep raised RecursionError.

What changes

Each walker is now a generator. citry._walk.run (added in #355) keeps the running walkers on a list and steps through them in one loop, so it needs no Python call per level. Where a walker called itself, it yields Call(walker, child, ...) and gets the child's result back from the yield. A function called from another module keeps its signature: for example, template_has_vue_attributes runs its own walk with run, because _checker calls it. The converted walkers:

  • _markup_lint.py: _walk, _v_pre_ranges and _single_root_starts.
  • _browser_expressions.py: the nine _collect_* template walkers.
  • _registry_lint.py: the six template walkers, including the walk that tracks disabled forms and fieldsets.
  • _css_lint.py: walk.
  • analysis.py: the Alpine, attribute-value and misspelled-directive collectors.
  • _provide_keys.py: _walk and _walk_attributes.
  • _checker.py: _trans_nodes and _unknown_component_findings.
  • _portable_ide.py: _collect_template_tags.
  • _template_python.py: _collect_static_input_queries, _query_in_template, _query_in_node and _query_start_indexes.
  • _component_graph.py: _walk_template.
  • The LSP engine: _trans_nodes, _trans_i18n_uses, _template_symbols, the lexical definition, reference and binding lookups, _citry_binding_reference_at, _syntax_reference_at, _first_node_named and _html_projection_slice.
  • The LSP's root_element.py: the functions that work out what type $el has at the template's root.
  • _vue/static_template.py: the writer methods and the fill collector.

Evidence

  • citry check on 128 nested <div> or <c-if> tags now peaks at 28 frames, the same as at depth 8.

  • test_check_depth_frames.py checks six shapes (nested elements, <c-if>, <c-for>, c-if=, fills under control flow, a template in an attribute) at depths 32 and 128. It allows at most 20 frames of growth, and it asserts the deep template's unknown variable is found, which shows the walkers reached the bottom. All six fail without these changes.

  • All 10,049 citry and citry_lsp tests pass with no expected output changed. mypy (also --platform linux) and ruff are clean.

  • An independent review compared the old and new code by probing both:

    • citry check findings were byte-identical across 20 shapes, and across 13 templates producing 11 finding codes;
    • static-template output was identical;
    • check stayed flat at depth 300 for every shape tried, including i18n, events, Vue, svg, c-raw, URLs, Alpine and repeated calls;
    • 400 nested <c-if> tags, which raised RecursionError before, now check.

    Every caller of a converted function now goes through Call or run. None of the converted functions used yield before, so turning them into generators changed nothing else about them.

  • Timing: citry check on a 300-row template took a median of 3.90 s against 3.79 s before (+2.9%, 3 runs each), under the plan's 5% threshold for check.

Known limits

  • Building a template's Python queries for a very deep template is still slow, and the LSP waits on it: template_python_queries takes 21 s at depth 128 (32 s before). That time comes from the amount of work the queries do, not from stack depth, and is outside this PR.
  • The review measured the i18n and events walkers in check, and their stack use stayed flat, but no test checks that yet. The follow-up's test 5 covers them.

🤖 Generated with Claude Code

JuroOravec and others added 10 commits October 25, 2026 09:37
…r level

`_markup_lint`'s `_walk` and the nine `_collect_*` template walkers in
`_browser_expressions` (browser expressions, bindings, declarative
events, component prop sites, mark literals, i18n directives, state
bindings, state target errors and enclosing elements) called themselves
once per nested element or template attribute. They run in `citry
check`, the LSP and on some first renders. Each is now a walk generator
run by `citry._walk.run`, with the same results in the same order.

Part of #272.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The registry lint's template walkers (repeated calls, Vue attributes,
server event calls and forms, the disabled-form walk, template URLs)
and the CSS lint's `walk` called themselves once per nested element or
template attribute. They are now walk generators run by
`citry._walk.run`. `template_has_vue_attributes` keeps its signature for
`_checker`, which calls it.

Part of #272.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ll per level

`analysis`'s `_collect_alpine_attributes`, `_collect_attribute_values` and
`_collect_misspelled_directives` called themselves once per nested
element or template attribute. They are now walk generators run by
`citry._walk.run`.

Part of #272.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`_markup_lint`'s `_v_pre_ranges` and `_single_root_starts` called
themselves once per nested element or c-if chain. They are now walk
generators run by `citry._walk.run`.

Part of #272.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`_provide_keys`' `_walk` and `_walk_attributes` called each other once
per nested element or template attribute. They are now walk generators
run by `citry._walk.run`.

Part of #272.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…evel

`_checker`'s `_trans_nodes` and `_unknown_component_findings`,
`_portable_ide._collect_template_tags`, `_template_python`'s
`_collect_static_input_queries`, `_query_in_template` and
`_query_in_node`, `_component_graph._walk_template` and the LSP engine's
own `_trans_nodes` called themselves once per nested element or
template attribute. They are now walk generators run by
`citry._walk.run`. `citry check` on a template of 128 nested elements
now peaks at the same Python stack as one of 8.

Part of #272.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`static_template`'s writer (`write_nodes`, `write_node`, `write_if`,
`write_for`, `write_component`, `write_slot`) and its fill collector
called each other once per nested element, control-flow branch, component
body or slot fallback. They are now walk generators run by
`citry._walk.run`, so the written template and its data keys stay the
same. `citry check` on 128 nested <c-if> tags now peaks at the same
Python stack as 8.

Part of #272.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…n stack

Six shapes, nested elements, <c-if>, <c-for>, c-if=, fills under
control flow and a template in an attribute, each checked at depth 32
and 128. The deepest stack may grow by at most 20 frames. All six fail
without the parsed-AST walker changes.

Part of #272.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The LSP engine's i18n uses, template symbols, lexical definitions,
references and bindings, binding and syntax references, first-node and
HTML projection lookups, the root element type check in
`root_element.py`, and `_template_python._query_start_indexes` called
themselves once per nested element or body. They are now walk generators
run by `citry._walk.run`.

Part of #272.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Statements that discard a walk result use a bare `yield Call(...)`.
`template_has_vue_attributes` keeps its full docstring, and its comment
says what `run` does. The check frame test asserts that the deep
template's unknown variable is found, so it cannot pass on a template
the walkers never reached.

Part of #272.

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