Skip to content

perf(@angular/build): cache pre-declaration transformed SourceFiles during AoT emit - #34425

Open
alan-agius4 wants to merge 3 commits into
angular:mainfrom
alan-agius4:perf/aot-pre-declaration-cache
Open

alan-agius4 wants to merge 3 commits into
angular:mainfrom
alan-agius4:perf/aot-pre-declaration-cache

Conversation

@alan-agius4

@alan-agius4 alan-agius4 commented Oct 20, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • When TypeScript's BuilderProgram.emitNextAffectedFile emits .d.ts-only (emitOnly = Dts) for an affected transitive importer, TypeScript skips transformers.before and runs transformers.afterDeclarations. ensurePreDeclarationTransformers runs before transformers inside afterDeclarations to populate Angular's dtsTransforms, and caches the transformed ts.SourceFile in preTransformedSourceFiles only for those .d.ts-first files (deleting the entry immediately when consumed by a subsequent .js emit).
  • For normal .js-first / combined .js + .d.ts emits, tracks seenSourceFiles via a Set<ts.SourceFile> without retaining transformed SourceFile ASTs in memory across the emit loop.

Benchmark Results

Metric main Branch Delta
Runs (ms) 11814, 11582, 11874, 11927, 12264 11584, 11679, 11758, 11764, 11807
Median (ms) 11874 ms 11758 ms -0.98% (-116 ms)
Mean (ms) 11892 ms 11718 ms -1.46% (-174 ms)
Min (ms) 11582 ms 11584 ms +0.02% (+2 ms)

…uring AoT emit

Cache transformed `SourceFile` ASTs in a `Map` in `ensurePreDeclarationTransformers` instead of only tracking visited files in a `Set`. When `.d.ts`-only emit runs prior to `.js` emit (such as for transitive importers during incremental library builds), the subsequent `.js` emit now reuses the cached transformed `SourceFile` instead of executing `before` transformers a second time.
@angular-robot angular-robot Bot added area: performance Issues related to performance area: @angular/build labels Oct 20, 2026
@alan-agius4 alan-agius4 added the target: minor This PR is targeted for the next minor release label Oct 20, 2026
@alan-agius4
alan-agius4 requested a review from dgp1130 October 20, 2026 12:44
@alan-agius4 alan-agius4 added the action: review The PR is still awaiting reviews from at least one requested reviewer label Oct 20, 2026
@alan-agius4
alan-agius4 marked this pull request as ready for review October 20, 2026 12:44

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request exports and refactors the ensurePreDeclarationTransformers function in aot-compilation.ts to properly cache and reuse pre-transformed source files. The implementation now combines all before-transformers into a single transformer and utilizes a map to cache the transformed ASTs during the declaration emit phase, preventing redundant executions when subsequently emitting JavaScript. Comprehensive unit tests have also been added in angular-compilation_spec.ts to verify this behavior. There are no review comments, so I have no feedback to provide.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

action: review The PR is still awaiting reviews from at least one requested reviewer area: @angular/build area: performance Issues related to performance target: minor This PR is targeted for the next minor release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants