Repository navigation
perf: make whitespace and quadratic pipeline phases linear - #737
Merged
Merged
Conversation
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>
Member
Author
|
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. Swish! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
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.
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
whitespace.rsCow, reuse theIdentwhen the text did not changenaming.rs.nth(index)on a linked list per opempty_elements.rsemit.rstemporary_variables.rsresolve_names.rsresolve_i18n_element_placeholders.rsdefer_resolver.rsIndexMaplexer.rspipe_creation.rsSpeed
Whole compile, release build, best of 5.
4N/Nis about 4 when the time grows linearly.<span>x</span>× N<i (click)>× N<br>× N<i [title]>× N<div>× N@if× N@for× Nx <!--c-->× N@let× N{{ a + a + … }}{{ a | json | … }}* 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
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 filesafe_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 passedvp run compare: 100% in every groupFound, not fixed here (no perf link, needs its own decision)
(tmp = tmp)in the cloned branch, but we emit the call again ((tmp = ctx.b())), so a side effect can run twice. It's inexpand_safe_reads.collect_unique_namessorts temp names as strings, sotmp_0_10comes beforetmp_0_2. This only shows with 11+ temps in one op.transform/boundaries.rskeeps its own copy ofis_js_whitespace.🤖 Generated with Claude Code