From 9a2b4a04f391db2e27a1c9072641eee329fe1b6c Mon Sep 17 00:00:00 2001 From: Charles Lyding <19598772+clydin@users.noreply.github.com> Date: Wed, 30 Sep 2026 15:37:17 -0400 Subject: [PATCH 1/2] perf(@angular/build): avoid sharding intermediate files under worker surplus in i18n inliner When the number of uncached files in an inlining window meets or exceeds the worker pool concurrency, the thread pool already has sufficient file-level parallelism to keep all workers fully utilized. Previously, all intermediate files (files between 100 KB and 70% of the maximum file size) were sharded into batches of half their locales regardless of the total file count. In applications with many chunks, this doubled the number of worker tasks and forced multiple worker threads to independently parse the same JavaScript file and decode the same source map. Intermediate files are now processed with all window locales in a single batch whenever the file count is greater than or equal to the worker thread count. This halves the task count for intermediate chunks, eliminates redundant AST and sourcemap parsing across threads, and reduces IPC and scheduling overhead. --- packages/angular/build/src/tools/i18n/i18n-inliner.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/angular/build/src/tools/i18n/i18n-inliner.ts b/packages/angular/build/src/tools/i18n/i18n-inliner.ts index 881440f2ece1..d7d3ef6ea0a9 100644 --- a/packages/angular/build/src/tools/i18n/i18n-inliner.ts +++ b/packages/angular/build/src/tools/i18n/i18n-inliner.ts @@ -540,8 +540,12 @@ export class I18nInliner { // at least one worker to process smaller chunks in parallel without starvation. const targetWorkers = Math.max(1, Math.min(workerCount - 1, MAX_DOMINANT_WORKERS)); localesPerBatch = Math.max(1, Math.ceil(entries.length / targetWorkers)); + } else if (uncachedByFile.size >= workerCount) { + // Worker pool is already saturated by file-level concurrency; + // process all locales in a single batch to avoid duplicate parsing and IPC overhead. + localesPerBatch = entries.length; } else { - // Intermediate files: moderate sharding + // Intermediate files with idle workers: moderate sharding localesPerBatch = Math.max(1, Math.ceil(entries.length / 2)); } From 2afc7f53633dda7b06580a70d5cc61b4e5190a8b Mon Sep 17 00:00:00 2001 From: Charles Lyding <19598772+clydin@users.noreply.github.com> Date: Wed, 30 Sep 2026 17:49:51 -0400 Subject: [PATCH 2/2] perf(@angular/build): bound aggregate worker allocation across dominant files in i18n inliner Previously, MAX_DOMINANT_WORKERS (4) was applied independently per dominant file. When multiple large bundles qualified as dominant in the same window (e.g., 3 bundles of 3 MB each), each bundle claimed up to 4 worker threads, allocating up to 12 heavy tasks simultaneously. This could saturate the entire worker pool with large AST and sourcemap remappings, starving smaller chunks and spiking peak RSS. The target worker count per dominant file now divides MAX_DOMINANT_WORKERS by the number of dominant files in the window. This preserves the existing 4-worker allocation when a single dominant file is present, while bounding aggregate dominant concurrency across the pool when multiple large bundles exist. --- .../build/src/tools/i18n/i18n-inliner.ts | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/packages/angular/build/src/tools/i18n/i18n-inliner.ts b/packages/angular/build/src/tools/i18n/i18n-inliner.ts index d7d3ef6ea0a9..d4a4c4cd631f 100644 --- a/packages/angular/build/src/tools/i18n/i18n-inliner.ts +++ b/packages/angular/build/src/tools/i18n/i18n-inliner.ts @@ -66,12 +66,12 @@ const SMALL_FILE_FLOOR_BYTES = 100 * 1024; /** * Ratio of the maximum file size in a window to consider a file "dominant". - * Files within 70% of the largest file are sharded across all workers for maximum concurrency. + * Files within 70% of the largest file are sharded across bounded workers for concurrency. */ const DOMINANT_FILE_RATIO = 0.7; /** - * Maximum number of worker threads to allocate concurrently for a single dominant file. + * Maximum number of worker threads to allocate concurrently across dominant files. * Capping concurrency on dominant files (which hold large AST and sourcemap trees) * prevents V8 heap and RSS explosion on machines with high CPU core counts. */ @@ -520,6 +520,20 @@ export class I18nInliner { // while small files act as gap fillers near the window barrier to prevent tail stragglers. sortedFiles.sort((a, b) => b.fileSize - a.fileSize); + const dominantThreshold = Math.max(SMALL_FILE_FLOOR_BYTES, maxFileSize * DOMINANT_FILE_RATIO); + let dominantCount = 0; + for (const { fileSize } of sortedFiles) { + if (fileSize >= dominantThreshold) { + dominantCount++; + } else { + break; + } + } + const dominantTargetWorkers = + dominantCount > 0 + ? Math.max(1, Math.min(workerCount - 1, Math.floor(MAX_DOMINANT_WORKERS / dominantCount))) + : 1; + const workerTasks: Promise[] = []; for (const { filename, entries, codeFile, fileSize } of sortedFiles) { @@ -535,11 +549,10 @@ export class I18nInliner { } else if (fileSize < SMALL_FILE_FLOOR_BYTES) { // Small chunks (< 100 KB): process all locales in 1 batch to eliminate IPC overhead localesPerBatch = entries.length; - } else if (fileSize >= maxFileSize * DOMINANT_FILE_RATIO) { + } else if (fileSize >= dominantThreshold) { // Dominant file(s) with multiple files: shard across bounded workers while always reserving // at least one worker to process smaller chunks in parallel without starvation. - const targetWorkers = Math.max(1, Math.min(workerCount - 1, MAX_DOMINANT_WORKERS)); - localesPerBatch = Math.max(1, Math.ceil(entries.length / targetWorkers)); + localesPerBatch = Math.max(1, Math.ceil(entries.length / dominantTargetWorkers)); } else if (uncachedByFile.size >= workerCount) { // Worker pool is already saturated by file-level concurrency; // process all locales in a single batch to avoid duplicate parsing and IPC overhead.