Repository navigation
perf: measure formatter columns in the text each pass will produce - #432
Merged
Merged
Conversation
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
force-pushed
the
fix/formatter-pass-loop
branch
from
October 27, 2026 14:03
b6a39af to
27dd7d5
Compare
This was referenced Oct 28, 2026
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.
What this fixes
In a release build,
format_templatetook 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
PlannedEditsinsource.rs). That includes each start tag's layout, which it plans with the printer's ownplan_start_tagwhen 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.<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.plan_start_tagandspan_intersects_protectedbecomepub(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
Transtemplate inpackages/py/citry/citry/ext/i18n/components.py.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 testa_multiline_expression_settles_with_planned_columnslocks the new one.citry formatcannot 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 withcitry formatcomes as its own PR after this one.Timings
These were measured in a release build of
citry_template_formatter, with oneformat_templatecall 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.<div c-for="rN in rows">nested,{{ n }}inside<c-if cond="x">nested,<p></p>inside<div>nested,{{ n }}insideThe
c-ifand 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
a_deep_chain_settles_in_a_few_passes(at most 8 passes for 200 levels);a_multiline_expression_settles_with_planned_columns;tests/deep_elements.rs.fmt: off/fmt: on,fmt: skip, nestedc-bodytemplates, control tags, multiline and attribute expressions,<pre>and CRLF. It also ran 1,500 chains with comments andfmt: skipinside 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.citry_core(524),citry -k format(271), and the LSP formatting tests.Fixes #421.
🤖 Generated with Claude Code