Repository navigation
Description
What happens
The Vue render function that Citry generates indents each line by two spaces per nesting level. On a deeply nested template most of the generated code is leading spaces, so its size grows with the square of the depth. Found while working on #501.
For the c-if/c-elif/c-else template from #501 at 256 levels, code is 2.49 MB, and 2.37 MB of that is leading spaces. For 512 nested <div> elements, 0.52 MB of the 0.54 MB is leading spaces. The browser downloads this code as part of the component bundle.
Measured on a release build with the per-element hash fix from the #501 PR (#538) applied, compile plus serialization of the c-elif template:
| Depth | With indentation | Without indentation |
|---|---|---|
| 128 | 7.8 ms | 4.9 ms |
| 256 | 24.9 ms | 15.5 ms |
Where it comes from
The indentation comes from vize itself, not from Citry's patches. Both code emitters indent the same way Vue's own compiler does:
- the S2 emitter (
vize_s1_to_s2, the code generator Citry tries first), inthird_party/rust/vize_s1_to_s2/src/emit/buf.rs(Buf::newline); - the fallback emitter, which Citry uses when the S2 emitter declines a template, in
third_party/rust/vize_atelier_core/src/codegen/context.rs(newline).
Proposed fix
Stop increasing the indentation after a fixed depth, for example 8 levels, in both emitters. Lines below that depth keep the indentation of level 8. This changes only whitespace, provided newline() never runs inside a string or template literal; the change must confirm that for both emitters. Shallow templates, which are almost all real ones, produce exactly the same code as today.
What moves with it:
codeandcodeSha256change for templates nested deeper than the limit, so anything keyed on the generated code changes for those templates.Buf::indent_width(buf.rs:68-70) feedscached_element_rhsinhoist.rsand must follow the same limit.- The exact-string emitter tests in
third_party/rust/vize_s1_to_s2/testsneed cases past the limit, and the corpus tests that compare with Vue's output (davinci_dom_corpus*.rs) need checking for templates deeper than the limit. - The patch is recorded in both crates' patch notes (
README.citry.md), and both crates' version moves to0.420.0+citry.8, withCargo.lock.
Alternatives considered
- Emit no indentation at all. This is smaller, but it changes the code of every template and makes the generated code hard to read when debugging.
- Leave indentation as it is and compress the bundle. Compression hides the download size but not the time spent building, hashing and serializing the code.
Error modes
The limit is a constant, not a setting, so it has no invalid values. A template shallower than the limit is unaffected.
Part of the follow-up from #501.
Activity