Skip to content

perf: make whitespace and quadratic pipeline phases linear - #737

Merged
Brooooooklyn merged 10 commits into
mainfrom
perf/linear-hotspots
Nov 25, 2026
Merged

Brooooooklyn merged 10 commits into
mainfrom
perf/linear-hotspots

Conversation

@Brooooooklyn

Copy link
Copy Markdown
Member

Follow-up to #735. Fixes the whitespace pass speed-up from that review and every quadratic hot spot we found in the compile pipeline.

What changed

Commit Where Before After
perf(parser) whitespace.rs char loop, 2+ copies per text one byte-table pass, Cow, reuse the Ident when the text did not change
perf(naming) naming.rs .nth(index) on a linked list per op one walk
perf(pipeline) empty_elements.rs 2 walks from the head per empty element one pass
perf(emit) emit.rs each view scans all views for its children parent → children map built once
perf(temporary-variables) temporary_variables.rs recursive naming at every node of a full visitor name each node once (like Angular)
perf(resolve_names) resolve_names.rs recursive resolve at every node of a full visitor resolve each node once
perf(i18n) resolve_i18n_element_placeholders.rs walk all ops per placeholder find each context once
perf(defer) defer_resolver.rs linear find per attribute IndexMap
perf(lexer) lexer.rs line/column from byte 0 per escape error one pass
perf(pipe_creation) pipe_creation.rs walk from the head per pipe one pass

Speed

Whole compile, release build, best of 5. 4N/N is about 4 when the time grows linearly.

Template (N = 2000) main t(4N) this PR t(4N) main 4N/N this PR 4N/N
<span>x</span> × N 2610 ms 76 ms 11.3 4.5
<i (click)> × N 3805 ms 105 ms 17.9 4.7
<br> × N 2767 ms 34 ms 25.7 4.7
<i [title]> × N 3060 ms 79 ms 19.2 4.5
nested <div> × N 1019 ms 37 ms 15.9 4.3
@if × N 511 ms 70 ms 15.4 4.2
@for × N 612 ms 161 ms 11.6 4.8
x <!--c--> × N 272 ms 17 ms 24.4 4.7
@let × N 282 ms 49 ms 19.9 6.1
{{ a + a + … }} 540 ms 6.6 ms 14.1 4.4
1 element, N attributes 61 ms 10 ms 12.6 4.0
{{ a | json | … }} 853 ms 148 ms 18.9 14.5*

* The output for this one is itself N² in size (255 MB at 4N), the same as Angular.

The whitespace pass alone is 3.6x faster on the #734 input (179 → 50 µs). Unchanged text no longer allocates.

Output

1198 files (Angular compliance cases + compare fixtures + generated shapes)
  × 4 option sets (default, preserveWhitespaces, legacy i18n ids, advanced optimizations)
  → same output as main, except 1 intended change below
+ ~450 extra templates written to break each fix → same output as main

One intended change: nested safe calls such as f1()?.[f2()?.f3()?.[f4()?.f5()]] now get the same temp names as Angular. The old code counted each read once per ancestor, so a temp could be freed early and reused while still live (wrong code at runtime). The names now match Angular's own expected file safe_access_temporaries_template_use_null.js. Against real ngc 22.3.0 on 36 nested safe-call templates: main matched 26, this PR matches 33. The other 3 are the safe-read item in the list below, not this phase.

The JS trim rules (trim U+FEFF, keep U+0085) are also in place. No emitted output can change from this today, because the compile path always preserves leading and trailing whitespace, like Angular's parseTemplate. Unit tests cover it.

Checks

  • cargo test --all-features: 4247 passed
  • conformance snapshot: no change
  • vp run compare: 100% in every group

Found, not fixed here (no perf link, needs its own decision)

  • Safe-read guard: Angular emits (tmp = tmp) in the cloned branch, but we emit the call again ((tmp = ctx.b())), so a side effect can run twice. It's in expand_safe_reads.
  • collect_unique_names sorts temp names as strings, so tmp_0_10 comes before tmp_0_2. This only shows with 11+ temps in one op.
  • transform/boundaries.rs keeps its own copy of is_js_whitespace.

🤖 Generated with Claude Code

Brooooooklyn and others added 10 commits November 25, 2026 19:47
The whitespace pass now does NGSP replacement and run collapsing in one
byte pass. A 256-entry table finds the 6 ASCII whitespace bytes and the
UTF-8 lead bytes of the wide whitespace chars and NGSP. Unchanged parts
are copied as slices, and when nothing changes the input is borrowed.

The callers trim the borrowed text and reuse the original Ident when the
text did not change (every change makes it shorter). They only allocate
when it changed.

Trimming now follows JS trimStart/trimEnd like Angular: U+FEFF is
trimmed and U+0085 is kept. The JS whitespace check moves to util::chars
and is shared with the legacy animation code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
process_create_ops_with_child_recursion called create_ops.iter_mut().nth(index)
for every create op (and again for each @for with track ops). The create list
is a linked list, so this was O(n^2) in the number of sibling ops.

Now the view's create list is taken out of the job for one walk and put back
at the end. Naming a child view only touches that child's own lists and the
name pool, so nothing reads the parent list meanwhile. The order is the same:
op, then its child views, then @for track ops, as in Angular's
nameFunctionsAndVariables. Output is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
collapseEmptyInstructions walked the create list from the head twice
for every empty element: once to reach the End op and once to find the
matching Start op. With many empty siblings this was quadratic.

Now the pairs keep both the start and end index, and a single forward
pass turns each Start op into its merged form and removes its End op.
Which ops collapse and their order are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Emitting child views scanned every view of the job once per view to find
its children, so emit was quadratic in the number of views. Build the
parent -> children lists once, in job.views order, and walk them depth
first. The order matches Angular's emitChildViews, so output is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The naming callback ran inside transform_expressions_in_*, which already
calls it on every node, children first. But it also walked the whole
subtree again, so the cost was nodes x depth.

It also counted the same read once per ancestor. A name was then freed
before its real last read, and a nested safe call could reuse a name that
was still in use, e.g. `a()?.b?.c?.d?.[g()?.h]` got `tmp_0_0 = ctx.g()`
inside `tmp_0_0.b.c.d[...]`.

Now the callback handles only the current node, like the visitor in
Angular's generateTemporaries. Names now match ngc 22.3 in these cases,
including the compliance case safe_access_temporaries
(`tmp_3_2 = tmp_3_1.f3()`, `tmp_3_3 = ctx.f4()`).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The post-order visitor called the recursive resolve_expression at every
node, so each node was resolved once per level above it (nodes x depth).
A long chain like `a + a + ... + a` was quadratic.

The visitor callback now resolves only the node itself. The one extra
pass the old code gave to subtrees built from Ast/ExpressionRef leaves
(under a parent that resolve_expression descends into) is kept: the
parent runs it once for those child slots. Further passes changed
nothing, so the result is the same tree.

Calls to the resolver at N=2000 / 8000:
  expr_long  6.0M / 96M  ->  4.0K / 16K
  expr_safe 16.1M / 256M -> 28.0K / 112K

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Each element or template placeholder looked up its i18n context op by
walking the root create list and then every view's create list. With
many placeholders and many views this was placeholders x ops.

Now the params are collected per context xref while the views are
walked, and written to the context ops in one pass at the end, root
first, in the same order as before. Angular builds a context map once
in the same phase. The output is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
element_selector built the attribute map for directive matching with a
linear name search per attribute, so one element with N attributes cost
O(N^2). It runs on the default path (DomOnly check). Use an
insertion-ordered IndexMap instead: same order, same "later value
replaces, first position kept" rule as getAttrsForDirectiveMatching.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
In escaped-string mode, each bad escape error walked the input from
byte 0 to find its line and column. With E errors that is n x E.

Now the errors are collected first, and their positions are found in
one walk over the input (offsets sorted, results put back in order).
Line and column values are the same as before: every \n and \r starts
a line, and columns count characters.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
In compatibility mode each pipe goes after its target op and after the
pipes already placed there. We rescanned the create list from the head
for every pipe, so a view with many pipes cost O(pipes x ops).

Now we walk the create list once per view and record, for each xref,
the first non-pipe op after it. Only pipe ops are inserted, always just
before that op, so the recorded spots stay valid and each pipe lands
exactly where the old scan put it. Output is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Brooooooklyn

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-25T12:02:21.225996Z 3774065 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: 37740652b8

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Brooooooklyn
Brooooooklyn merged commit e3b3ca7 into main Nov 25, 2026
10 checks passed
@Brooooooklyn
Brooooooklyn deleted the perf/linear-hotspots branch November 25, 2026 12:44
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.

1 participant