Repository navigation
perf(lsp): find every template expression's Python names in one walk (#497) - #535
Merged
Merged
Conversation
…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]>
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.
Fixes #497.
The editor calls
template_python_querieson each edit to find, for every Python value in a template, the context around it: thec-forvariables it can use, thec-ifconditions 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_queriesnow walks the template once (_collect_template_queriesand_collect_node_queriesinpackages/py/citry/citry/_template_python.py). The walk carries what encloses each value (itsc-ifconditions,c-forvariables 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_nodeso both searches build them the same way._query_start_indexesis 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
divrow with a value per level is added, because #384'sdivrow has no values. Each row is the median of 3 runs oftemplate_python_querieson 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'sv-prechange, which these templates don't use.c-ignoreper levelc-if/c-elifchain per levelc-ifper leveldivwith a value per leveldivwith no valuesDoubling 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
test_template_python.py, and twocitry checkslows 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.packages/py/citry/tests/test_template_python.py:test_each_enumerated_query_is_the_query_at_its_startchecks, over six templates, that every listed query equals the onetemplate_python_query_atgives at its start.test_query_enumeration_reads_grow_linearly_with_depthcounts 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.test_semantic,test_type_analysis,test_engine,test_type_checkandtest_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_pinsaw a stale ty version through a shared path, and onetest_type_checkcase starts a subprocess that lacks myPYTHONPATH.--platform linux) pass on the changed file.No Rust or PyO3 changes.
🤖 Generated with Claude Code