Repository navigation
Description
What happens
format_template takes about 6 seconds in a release build to format 200 nested <div> elements that each carry a c-for attribute. When the depth doubles, the time grows about 5 to 7 times, and the factor rises with depth. So the time grows faster than the square of the depth and approaches its cube.
Nesting without expressions is much cheaper. Both of the plain shapes below grow about four times per doubling. Some of that is the formatted text itself: it gets two more spaces of indentation per level, so its size grows with the square of the depth.
Measurements
These are release builds of citry_template_formatter on macOS arm64. Each run is a single call to format_template, on a thread with plenty of stack.
| Template | Depth | Time |
|---|---|---|
<div c-for="rN in rows"> nested, {{ n }} inside |
25 | 0.026 s |
| 50 | 0.135 s | |
| 100 | 0.815 s | |
| 200 | 5.958 s | |
plain <div> nested, {{ n }} inside |
250 | 0.016 s |
| 500 | 0.052 s | |
| 1,000 | 0.194 s | |
| 2,000 | 0.746 s | |
<c-if cond="x"> nested, <p></p> inside |
50 | 0.003 s |
| 100 | 0.008 s | |
| 200 | 0.025 s | |
| 400 | 0.087 s |
A debug build is much slower. Formatting 256 nested c-for elements with 144 plain <div> elements inside took several minutes.
Where the time goes
The suspect is the pass loop in format_once_with_options (crates/citry_template_formatter/src/formatter.rs). Each pass applies the edit plan, which is the list of text changes the formatter wants to make. Then it reparses the result and rebuilds the source model, its record of every tag, expression and whitespace gap between elements. It repeats until the text stops changing, up to one pass per tag, gap and expression, plus two.
For the c-for template, a temporary counter showed that the loop ran depth minus 4 passes: 21 passes at depth 25 and 196 at depth 200. So the loop seems to settle one nesting level per pass. A likely reason, not yet confirmed: a change to one level's indentation changes the width left for the expressions inside it, and those are only laid out again in the next pass. Each pass reparses and rebuilds the model, and the model grows with the text. That gives about depth times the text size, close to the cube of the depth.
The loop also runs three times per call: once to format, and twice more in verify_candidate_with_options, which checks that the edit plan gives the same result again and that a second format changes nothing. So each format pays the cost three times.
A second, smaller cost: project_body calls analyze_elements at every level. For a transparent control tag such as c-if, analyze_elements walks the whole chain below that level again.
What a fix could look like
The plan could compute the final indentation of every level in one pass, so it does not need one pass per level. Or the loop could reuse the work from passes that did not change. Either fix should keep the formatter's checks that the formatted text reparses, means the same as the original (the formatter compares a summary of what each version renders), and does not change when formatted again.
Activity
perf: measure formatter columns in the text each pass will produce