Skip to content

citry check slows down with roughly the cube of template nesting depth #384

Closed

Description

@JuroOravec

What happens

citry check on a deeply nested template takes far longer than a render of
the same template. The time grows roughly with the cube of the nesting depth:
doubling the depth makes the check about five to seven times slower. At the
nesting limits a single component takes up to a minute to check. The editor's
language server reads templates the same way, so it slows down the same way.

Rendering is not affected. A render of the slowest fixture below takes 0.1
seconds at depth 256.

Measurements

Each row is one component checked on its own with check_project. These are
the times for the whole check. The numbers come from a debug build of
citry_core (the local development build). A release build is faster by
some constant factor, but the copying described below happens the same way,
so the growth with depth should be the same.

Template, repeated depth times 8 32 64 128 256
<c-if cond="flag"><div #c-ignore> 0.5 s 1.4 s 7.4 s 49 s
<c-if cond="not flag">no</c-if><c-elif cond="flag"> ... </c-elif><c-else>else</c-else> 0.2 s 1.9 s 10.2 s 70 s
<c-if cond="flag"> 13 s
<div> (512 levels) 4.9 s

Why

The parsed template is a tree of Rust #[pyclass] objects. When Python reads
a child list from one of them (Template.elements, a node's body, a start
tag's attrs), PyO3 builds a new Python object from a copy of the Rust value.
For a node's body, that copy is the node's whole subtree. A walker that visits
every node therefore copies every subtree once per level above it, so one
walk costs time proportional to the square of the depth. citry check runs
about a dozen such walkers. The measured growth is steeper than the square,
probably because some walkers read the same subtree again at each level
(the profile does not show which); that part is not yet confirmed.

A profile of one check at depth 128, using the first template above
(7.1 seconds in total):

  • getattr calls: about 15,000, taking 1.7 seconds in all.
  • Each of about 13 walkers (_unknown_component_findings,
    _collect_mark_literal_findings, _collect_misspelled_directives,
    _collect_template_urls, the CSS and markup lint walks, and others) is
    called 257 times. Each walker spends about 0.32 seconds on its own
    work and 0.42 seconds in total.

The walkers themselves use a fixed number of Python frames at any depth
(#299 and PR E). The time goes to building those copies, not to the walk.

The getters are the #[pyo3(get)] fields in
crates/citry_template_parser/src/ast.rs. They include Template.elements
(ast.rs:1007), HtmlStartTag.attrs (ast.rs:470), and the fields of the
Node enum's variants, among them body (ast.rs:655 onward).

Possible fixes

Each of these changes the AST #[pyclass] surface, which is the Python
contract mirrored in packages/py/citry_core/citry_core/_rust.pyi. So any
fix needs a written plan under Mechanism 2 of CLAUDE.md before work starts,
covering the .pyi stub, the PyO3 glue, and every Python reader of the tree.

  1. Shared references. Store child lists as Py<...> (or behind an
    Arc), so a getter hands Python the same object every time instead of a
    copy. Reads become cheap, but mutation from Python and the Rust code that
    builds and transforms trees have to deal with shared ownership.
  2. A Python view built once. Convert the whole tree to Python objects
    once per template, iteratively, and cache the result with the template.
    The walkers then read plain Python objects. This costs memory per cached
    template, and the view has to stay in step with the Rust tree.
  3. Iteration helpers that do not copy. Add Rust methods such as "visit
    every node with its depth" or "the children of this node as borrowed
    handles", and move the walkers onto them. The getters stay as they are
    for other callers. Each walker then needs rewriting, and the helpers
    widen the PyO3 surface.

How to reproduce

import pathlib
import tempfile
import time

from citry import Citry, Component
from citry._app_selection import CheckAppSelection
from citry._checker import check_project

depth = 128
step = '<c-if cond="flag"><div #c-ignore>'
template = step * depth + "x" + "</div></c-if>" * depth
engine = Citry(autodiscover=False)
type("Page", (Component,), {"citry": engine, "template": template})
start = time.time()
check_project(
    CheckAppSelection(spec="app:engine", engine=engine),
    pathlib.Path(tempfile.mkdtemp()),
)
print(f"{time.time() - start:.1f} s")

Related: #272 (deeply nested templates), and the nesting limits of 512 tags
and 256 logic levels. The tests for those limits currently run citry check
only on the two template shapes that check in a few seconds, because of this
issue.

Activity

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