Skip to content

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

Closed

Description

@JuroOravec

What happens

The language server finds the Python names around each expression in a template with template_python_queries (packages/py/citry/citry/_template_python.py:254). It collects the start of every Python value in the template, then searches from the root once for each one (_query_in_template, :1923), going down only into the bodies that contain that position (_query_in_node and _node_body_contains, :2030, :2240). Each search also builds the chain of control tags around the position.

So one search reads about as many nodes as the template is deep, and a template with an expression at every level makes the editor read about depth² nodes for each change. The editor calls this on every document update (packages/py/citry_lsp/citry_lsp/engine.py:1088, :5591). citry check does not use it.

#384 makes each read of the parsed tree copy one node instead of a whole subtree, so this search gets much cheaper, but it still grows with the square of the depth. Ordinary templates are a few dozen levels deep at most, so this matters mainly for deep or generated templates near the nesting limits (256 Citry logic levels, 512 tags).

Fix direction

  1. Measure first. Time the language server's diagnostics for the four deep templates in citry check slows down with roughly the cube of template nesting depth #384 at depths 128 and 256, after citry check slows down with roughly the cube of template nesting depth #384 lands. If doubling the depth costs about 4 times as much and the time at 256 is small next to the rest of the update, close this issue.
  2. Otherwise, collect every query in one walk: keep the stack of enclosing control tags while walking down, and emit a query for each Python value as the walk reaches it. That costs time proportional to the number of nodes, and it replaces one search per position with one walk per template.

The result must stay the same: the same queries, in the same order, with the same lexical context. The existing _template_python tests cover that.

Activity

  1. added a commit that references this issue on Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions