Skip to content

perf(lsp): find every template expression's Python names in one walk (#497) - #535

Merged
JuroOravec merged 1 commit into
mainfrom
perf/template-python-queries-one-walk
Oct 28, 2026
Merged

JuroOravec merged 1 commit into
mainfrom
perf/template-python-queries-one-walk

Conversation

@JuroOravec

@JuroOravec JuroOravec commented Oct 28, 2026 •

Copy link
Copy Markdown
Member

Fixes #497.

The editor calls template_python_queries on each edit to find, for every Python value in a template, the context around it: the c-for variables it can use, the c-if conditions that hold there, and the slot fill it sits in. It used to collect the start of every value first, then search from the template's root once per value, reading every node and sibling on the way. On a template with an expression at every nesting level, that is about depth² node reads. On a flat template it grows with the square of the number of values.

What changes

template_python_queries now walks the template once (_collect_template_queries and _collect_node_queries in packages/py/citry/citry/_template_python.py). The walk carries what encloses each value (its c-if conditions, c-for variables and slot fill) down to it, and returns the same queries in the same order. Attribute queries come from _attribute_query, which this change moves out of _query_in_node so both searches build them the same way. _query_start_indexes is no longer needed and is removed. template_python_query_at, which answers for one cursor position, is unchanged.

Measurements

The first, second, third and fifth rows are #384's deep templates. The div row with a value per level is added, because #384's div row has no values. Each row is the median of 3 runs of template_python_queries on an already parsed template, on macOS, with a Rust core built from #527's first commit (1cd2e1cdd). Main's Rust code differs from it only by #525's v-pre change, which these templates don't use.

template depth before after
c-ignore per level 64 / 128 / 256 85 / 361 / 1521 ms 3.7 / 7.6 / 15.8 ms
c-if/c-elif chain per level 64 / 128 / 256 208 / 854 / 3647 ms 5.4 / 11.5 / 23.9 ms
c-if per level 64 / 128 / 256 46 / 185 / 788 ms 2.1 / 4.2 / 9.0 ms
div with a value per level 64 / 128 / 256 45 / 176 / 706 ms 2.0 / 3.9 / 8.2 ms
div with no values 64 / 128 / 256 0.2 / 0.4 / 0.8 ms 0.8 / 1.6 / 3.2 ms

Doubling the depth now doubles the time, where before it multiplied it by about four. A flat template with 400 expressions in 200 sibling elements (<p c-title="...">{{ ... }}</p>) takes 7.2 ms instead of 997 ms.

The last row is slower. Before, a template with no Python values was skipped straight away, because the list of start positions came out empty. The new walk still visits every node. That costs about 2.4 ms at 256 levels, and only on templates with no expressions at all. I kept it rather than add a separate pre-check.

Evidence

  • Same results. For 1,146 templates (1,104 Citry UI component templates, 40 template strings from test_template_python.py, and two citry check slows down with roughly the cube of template nesting depth #384 rows each repeated five times), the old and new functions return identical query lists. Enumerating those templates is 2.8 times faster.
  • New tests in packages/py/citry/tests/test_template_python.py:
    • test_each_enumerated_query_is_the_query_at_its_start checks, over six templates, that every listed query equals the one template_python_query_at gives at its start.
    • test_query_enumeration_reads_grow_linearly_with_depth counts how many times the walk reads a condition and checks that doubling the depth at most doubles the count. It fails on the old code.
  • The editor tests pass: test_semantic, test_type_analysis, test_engine, test_type_check and test_template_python, 842 tests in all. Two failed, and both come from my throwaway environment, not this change. test_runtime_ty_version_matches_every_declared_ty_pin saw a stale ty version through a shared path, and one test_type_check case starts a subprocess that lacks my PYTHONPATH.
  • ruff, ruff format and mypy (macOS and --platform linux) pass on the changed file.
  • An independent review found no correctness problems, and its comment fixes are applied.

No Rust or PyO3 changes.

🤖 Generated with Claude Code

…497)

The editor finds the Python names around each template expression with
template_python_queries. It collected the start of every Python value,
then searched from the template's root once for each one, reading every
node and sibling on the way. On a template with an expression at every
level that is about depth squared reads, and on a flat template it grows
with the square of the number of values.

It now walks the template once, carrying the enclosing c-if, c-for and
fill controls down to each host, and returns the same queries in the same
order. At 256 levels the #384 template with a c-if/c-elif chain per level
takes about 25 ms instead of about 4 s, and doubling the depth now about
doubles the time instead of quadrupling it.

Fixes #497.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@JuroOravec
JuroOravec merged commit 4346eca into main Oct 28, 2026
1 of 24 checks passed
@JuroOravec
JuroOravec deleted the perf/template-python-queries-one-walk branch October 28, 2026 22:28
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.

The editor's per-expression template search grows with the square of nesting depth

1 participant