Skip to content

perf: measure formatter columns in the text each pass will produce - #432

Merged
JuroOravec merged 1 commit into
mainfrom
fix/formatter-pass-loop
Oct 27, 2026
Merged

JuroOravec merged 1 commit into
mainfrom
fix/formatter-pass-loop

Conversation

@JuroOravec

@JuroOravec JuroOravec commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

What this fixes

In a release build, format_template took about 42 seconds for 400 <div c-for> tags nested one inside the next. Each doubling of the depth multiplied the time by 5 to 7 or more, rising with depth (#421).

The formatter runs edit passes until the text stops changing. Each pass measured an element's column in the text the pass started from. When a tag broke across lines, the tag nested right after it moved in that same pass, but the pass had already laid it out at its old column. So each pass settled one more level of the chain: a 200-level chain took 196 passes. Each pass also reparses the text and rebuilds the model, and the formatted text grows with the square of the depth, so the total time grew with about the cube of the depth.

What changes

  • Each pass measures columns in the text it will produce. The formatter walks the template to build its source model, its record of every tag, expression and gap it may edit. During that walk, it now records the edits it has decided so far (PlannedEdits in source.rs). That includes each start tag's layout, which it plans with the printer's own plan_start_tag when it reaches the tag. Each element's position and each expression's column are then measured through those edits. A 200-level chain settled in 2 passes when measured; the test allows up to 8.
  • Every format pass measures this way. Two model builds look at text the formatter has already settled: the verification step's model of its result, and the step that prepares <script> and <style> bodies for their own formatters. Those still read columns from the text. That text has no pending edits, so both ways of measuring give the same column.
  • The printer itself is unchanged. plan_start_tag and span_intersects_protected become pub(crate) so the walk can call them.
  • citry-core's changelog gets a Performance entry: formatting deeply nested tags that break across lines is much faster, and a few templates get a slightly different layout the first time.

Layout changes

Some templates now get a slightly different layout. The first time you format them with this version, a few lines can move. Of 5,491 templates collected from this repository and generated nestings, one changed: the Trans template in packages/py/citry/citry/ext/i18n/components.py.

<c-for each="segment in segments"><c-if cond="segment['type'] == 'text'">{{ segment["value"] }}</c-if><c-else><bdi dir="auto">{{ segment["value"] }}</bdi></c-else></c-for>

Before, both expressions became multiline. Now the second one, measured at <bdi>'s column, fits on one line. The first expression's closing line keeps the rest of the source line, so its width budget is zero, and Ruff splits its subscript across three lines. Both layouts are stable: formatting either one again changes nothing. The test a_multiline_expression_settles_with_planned_columns locks the new one.

citry format cannot reach that template today, because the class sits inside a function and discovery skips it. So no template in the repository is reformatted by this PR. Reformatting the repository with citry format comes as its own PR after this one.

Timings

These were measured in a release build of citry_template_formatter, with one format_template call per row, on a thread with plenty of stack, on macOS arm64. The machine was loaded (1-minute load average about 10), so treat the numbers as rough.

Template Depth main this PR
<div c-for="rN in rows"> nested, {{ n }} inside 50 0.14 s 0.009 s
100 0.80 s 0.025 s
200 5.69 s 0.068 s
400 41.97 s 0.21 s
<c-if cond="x"> nested, <p></p> inside 400 0.084 s 0.118 s
plain <div> nested, {{ n }} inside 400 0.034 s 0.050 s

The c-if and plain <div> chains were never slow. Both measured 40 to 50% slower here. That may be noise on the loaded machine, or the cost of planning each start tag twice, once during the walk and once in the printer; either way it is well under 0.1 s at 400 levels. The review measured a related template that still grows faster than linear: nested <b c-for> tags, each with {{ value_l(a, b) }} before the next tag, took 0.17 s at depth 150, 0.95 s at 300 and 2.8 s at 600. Its likely cause is the existing scan for the rest of an expression's source line, which this PR does not change.

Checks

  • The formatter's full test suite passes, including the shared test cases, which lock exact outputs and check that formatting an output again changes nothing. No corpus case changed.
  • New and changed tests:
    • a_deep_chain_settles_in_a_few_passes (at most 8 passes for 200 levels);
    • a_multiline_expression_settles_with_planned_columns;
    • a 400-level chain that breaks across lines at every level, formatted on a 256 KiB thread in tests/deep_elements.rs.
  • An independent review looked for templates that never stop changing or change when formatted again. It formatted 6,000 random templates (3,631 parse) mixing fmt: off/fmt: on, fmt: skip, nested c-body templates, control tags, multiline and attribute expressions, <pre> and CRLF. It also ran 1,500 chains with comments and fmt: skip inside start tags, and 800 templates with a <script>. None failed to converge, and a second format changed none of them. For each <script>, the embedded step's indent matched the tag's real column.
  • The Python formatter tests pass: citry_core (524), citry -k format (271), and the LSP formatting tests.

Fixes #421.

🤖 Generated with Claude Code

Each formatter pass measured an element's column in the text it started
from. When a tag broke across lines, the tag nested right after it moved
in the same pass, so a chain of nested tags settled one level per pass,
and the time grew with about the cube of the depth: 400 nested
`<div c-for>` tags took about 42 seconds in a release build.

While the walk builds the model, it now records the edits it has decided
so far, including each start tag's layout, and measures every element's
and expression's column through them. A chain settles in a few passes
however deep it is. A few templates get a slightly different layout,
which the new test a_multiline_expression_settles_with_planned_columns
locks.

Fixes #421.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@JuroOravec
JuroOravec force-pushed the fix/formatter-pass-loop branch from b6a39af to 27dd7d5 Compare October 27, 2026 14:03
@JuroOravec JuroOravec changed the title perf: settle deeply nested tags in a few formatter passes perf: measure formatter columns in the text each pass will produce Oct 27, 2026
@JuroOravec
JuroOravec merged commit 811caac into main Oct 27, 2026
19 of 32 checks passed
@JuroOravec
JuroOravec deleted the fix/formatter-pass-loop branch October 27, 2026 16:15
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 template formatter slows down sharply when elements with Python expressions are nested deeply

1 participant