Skip to content

Commit 9d7138e

Browse files
committed
perf(@angular/build): read rendered module length once per module in chunk optimizer
When converting the chunk optimizer output into an esbuild-compatible metafile, the rendered length of each module was read inside the loop over the module's original inputs. The `renderedLength` property of a rolldown rendered module is a lazy getter that transfers the full module code from native memory on every access. Since each module in this pass is an entire esbuild output chunk, a main chunk of 17 MB with 3,588 inputs resulted in roughly 62 GB of string copies and around 150 seconds spent in `bundleOutputToEsbuildMetafile`, while the rolldown bundling itself took 3 seconds. Read the rendered length once per module before iterating its inputs. The generated metafile is unchanged. On the affected application the `OPTIMIZE_CHUNKS` phase drops from 179 seconds to 4 seconds and the total production build from 234 to 55 seconds. Fixes #34044
1 parent 6b20983 commit 9d7138e

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

packages/angular/build/src/builders/application/chunk-optimizer.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,16 @@ function bundleOutputToEsbuildMetafile(
113113
continue;
114114
}
115115

116+
// Read the rendered length once per module. The value is a lazy getter on the bundler's
117+
// rendered module object that transfers the entire module code from native memory on each
118+
// access, which is prohibitively expensive when repeated for every input of a large chunk.
119+
const { renderedLength } = renderedModule;
120+
116121
for (const [originalInputPath, originalInputInfo] of Object.entries(
117122
originalOutputEntry.inputs,
118123
)) {
119124
const proportion = originalInputInfo.bytesInOutput / totalOriginalBytesInModule;
120-
const newBytesInOutput = Math.floor(renderedModule.renderedLength * proportion);
125+
const newBytesInOutput = Math.floor(renderedLength * proportion);
121126

122127
const existing = newOutputInputs[originalInputPath];
123128
if (existing) {

0 commit comments

Comments
 (0)