perf(@angular/build): traverse AST with iterative post-order walker in i18n inliner - #33845
perf(@angular/build): traverse AST with iterative post-order walker in i18n inliner#33845clydin wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Code Review
This pull request replaces the recursive Visitor from oxc-parser with a custom non-recursive post-order AST traversal function (walkAstPostOrder) in the i18n inliner worker. This bottom-up traversal ensures that nested $localize expressions are correctly processed before their outer containing templates. A unit test has also been added to verify this behavior. The feedback suggests optimizing the AST walker by flattening array children directly when pushing them onto the stack, which simplifies the loop logic and allows for stricter typing of the traversal stack.
f9c75b7 to
bc2a9dc
Compare
…n i18n inliner Replace the oxc-parser Visitor class in the i18n inliner worker with a lightweight, non-recursive post-order AST walker based on visitorKeys. oxc-parser's Visitor class caches visitor callback objects in a module-global array across invocations, which causes all per-request MagicString instances, source code buffers, and diagnostics closures to be retained for the lifetime of the worker thread. In multi-locale builds, this leads to continuous heap accumulation and out-of-memory errors on memory-constrained CI runners. The custom walker uses an iterative two-pass array traversal on the V8 heap to guarantee bottom-up evaluation without recursion or stack overflow risks. This ensures nested $localize template expressions are transformed and written to MagicString before outer templates evaluate their expressions, while eliminating all module-global caching and memory retention across file transformations.
bc2a9dc to
502ee51
Compare
alan-agius4
left a comment
There was a problem hiding this comment.
LGTM, just some NITs
| } | ||
| } | ||
|
|
||
| for (let i = postOrderNodes.length - 1; i >= 0; i--) { |
There was a problem hiding this comment.
NIT: in all these loops you can use for of
|
|
||
| for (let i = 0; i < keys.length; i++) { | ||
| const child = (current as unknown as Record<string, Node | Node[]>)[keys[i]]; | ||
| if (child) { |
There was a problem hiding this comment.
NIT: instead of nesting consider an early exit.
Replace the oxc-parser Visitor class in the i18n inliner worker with a lightweight, non-recursive post-order AST walker based on visitorKeys.
oxc-parser's Visitor class caches visitor callback objects in a module-global array across invocations, which causes all per-request MagicString instances, source code buffers, and diagnostics closures to be retained for the lifetime of the worker thread. In multi-locale builds, this leads to continuous heap accumulation and out-of-memory errors on memory-constrained CI runners.
The custom walker uses an iterative two-pass array traversal on the V8 heap to guarantee bottom-up evaluation without recursion or stack overflow risks. This ensures nested $localize template expressions are transformed and written to MagicString before outer templates evaluate their expressions, while eliminating all module-global caching and memory retention across file transformations.