Skip to content

Commit 0102e2b

Browse files
perf(javascript): release the rendered module sources after emit
Reading a chunk asset — what a minimizer or an asset plugin does — flattens every module factory source the chunk holds, and each memoizes its copy of that text. The factory sources live in a compiler-lifetime `WeakMap` keyed by the code generation source, so those copies stayed reachable for as long as the compilation did. Each compilation now collects the factory sources it rendered, including the ones it took from the cache, and releases their flattened copies once its assets are emitted. Clearing is only a hint, so a later read re-renders. Retained heap on a 2243-module production build: 149.47 -> 125.87 MiB, and 230.69 -> 181.44 MiB after six watch builds with a memory cache. Emitted output is byte-identical and no source had to be flattened again.
1 parent f089267 commit 0102e2b

5 files changed

Lines changed: 134 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Release the rendered module sources after emitting the chunks.

‎lib/javascript/JavascriptModulesPlugin.js‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,10 @@ const renameInlinedModuleVariable = (
430430

431431
const PLUGIN_NAME = "JavascriptModulesPlugin";
432432

433+
// Drops the redundant string/buffer copy of a source, keeping its map: a
434+
// parsed map is far more expensive to rebuild than a `toString`.
435+
const CLEAR_SOURCE_COPIES = { maps: false, source: true };
436+
433437
/** @typedef {{ header: string[], beforeStartup: string[], startup: string[], afterStartup: string[], allowInlineStartup: boolean }} Bootstrap */
434438
/** @typedef {{ source: Source, needModule: boolean, needExports: boolean, needRequire: boolean, needThisAsExports: boolean, needStrict: boolean | undefined, renderShorthand: boolean, next: ModuleFactoryCacheEntry | undefined }} ModuleFactoryCacheEntry */
435439

@@ -446,6 +450,8 @@ class JavascriptModulesPlugin {
446450
this._moduleFactoryCache = new WeakMap();
447451
/** @type {WeakMap<Source, Set<string>>} */
448452
this._renderedFreeNamesCache = new WeakMap();
453+
/** @type {WeakMap<CodeGenerationResults, Set<Source>>} */
454+
this._renderedModuleFactorySources = new WeakMap();
449455
}
450456

451457
/**
@@ -458,6 +464,9 @@ class JavascriptModulesPlugin {
458464
// drop them when the build ends so an idle process retains no parser state
459465
compiler.hooks.done.tap(PLUGIN_NAME, releaseParserCaches);
460466
compiler.hooks.failed.tap(PLUGIN_NAME, releaseParserCaches);
467+
compiler.hooks.afterEmit.tap(PLUGIN_NAME, (compilation) => {
468+
this._releaseRenderedModuleFactorySources(compilation);
469+
});
461470
compiler.hooks.compilation.tap(
462471
PLUGIN_NAME,
463472
(compilation, { normalModuleFactory }) => {
@@ -880,6 +889,33 @@ class JavascriptModulesPlugin {
880889
return outputOptions.chunkFilename;
881890
}
882891

892+
/**
893+
* Releases the flattened text the module factory sources memoized while the
894+
* chunk assets were read. Clearing is only a hint, so a later read re-renders.
895+
* @param {Compilation} compilation the compilation that emitted its assets
896+
* @returns {void}
897+
*/
898+
_releaseRenderedModuleFactorySources(compilation) {
899+
const { codeGenerationResults } = compilation;
900+
if (codeGenerationResults === undefined) return;
901+
const renderedSources = this._renderedModuleFactorySources.get(
902+
codeGenerationResults
903+
);
904+
if (renderedSources === undefined) return;
905+
this._renderedModuleFactorySources.delete(codeGenerationResults);
906+
// WHY: the factory sources wrap the code generation ones, so one walk with
907+
// a shared `visited` clears both layers and reaches a source shared by
908+
// several chunks once. Feature-detected, since `clearCache` landed in
909+
// webpack-sources 3.5 and a plugin may hand us an older `Source`.
910+
/** @type {WeakSet<Source>} */
911+
const visited = new WeakSet();
912+
for (const source of renderedSources) {
913+
if (typeof source.clearCache === "function") {
914+
source.clearCache(CLEAR_SOURCE_COPIES, visited);
915+
}
916+
}
917+
}
918+
883919
/**
884920
* Renders the newly generated source from rendering.
885921
* @param {Module} module the rendered module
@@ -985,6 +1021,19 @@ class JavascriptModulesPlugin {
9851021
next: cacheEntry
9861022
});
9871023
}
1024+
// Also on a cache hit: this compilation reads the assets again, so it
1025+
// memoizes into a source an earlier one already released.
1026+
let renderedSources = this._renderedModuleFactorySources.get(
1027+
codeGenerationResults
1028+
);
1029+
if (renderedSources === undefined) {
1030+
renderedSources = new Set();
1031+
this._renderedModuleFactorySources.set(
1032+
codeGenerationResults,
1033+
renderedSources
1034+
);
1035+
}
1036+
renderedSources.add(source);
9881037
moduleSourcePostContainer = tryRunOrWebpackError(
9891038
() => hooks.renderModuleContainer.call(source, module, renderContext),
9901039
"JavascriptModulesPlugin.getCompilationHooks().renderModuleContainer"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function value() {
2+
return 42;
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { value } from "./dep";
2+
3+
it("should render a working bundle after releasing the rendered sources", () => {
4+
expect(value()).toBe(42);
5+
});
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
"use strict";
2+
3+
/**
4+
* Reads the emitted assets so the rendered module sources memoize their
5+
* flattened text, then checks that emitting released it again.
6+
* @type {import("../../../../").WebpackPluginInstance}
7+
*/
8+
const observeRenderedSourceRelease = {
9+
apply(compiler) {
10+
const { Compilation } = compiler.webpack;
11+
const { JavascriptModulesPlugin } = compiler.webpack.javascript;
12+
compiler.hooks.compilation.tap("Observe", (compilation) => {
13+
/** @type {Map<import("webpack-sources").Source, string>} */
14+
const textBefore = new Map();
15+
/** @type {Set<import("webpack-sources").Source>} */
16+
const cleared = new Set();
17+
JavascriptModulesPlugin.getCompilationHooks(
18+
compilation
19+
).renderModuleContainer.tap("Observe", (source) => {
20+
if (textBefore.has(source)) return source;
21+
textBefore.set(source, "");
22+
const { clearCache } = source;
23+
if (typeof clearCache === "function") {
24+
source.clearCache = (...args) => {
25+
cleared.add(source);
26+
return clearCache.apply(source, args);
27+
};
28+
}
29+
return source;
30+
});
31+
compilation.hooks.processAssets.tap(
32+
{
33+
name: "Observe",
34+
stage: Compilation.PROCESS_ASSETS_STAGE_REPORT
35+
},
36+
(assets) => {
37+
for (const name of Object.keys(assets)) String(assets[name].source());
38+
for (const source of textBefore.keys()) {
39+
textBefore.set(source, String(source.source()));
40+
}
41+
}
42+
);
43+
compiler.hooks.done.tap("Observe", () => {
44+
// A run restoring its assets from the persistent cache renders
45+
// nothing, so there is nothing to assert; the uncached run asserts it.
46+
if (textBefore.size === 0) return;
47+
if (cleared.size !== textBefore.size) {
48+
compilation.errors.push(
49+
new Error(
50+
`released ${cleared.size} of ${textBefore.size} rendered module sources`
51+
)
52+
);
53+
}
54+
for (const [source, before] of textBefore) {
55+
if (String(source.source()) !== before) {
56+
compilation.errors.push(
57+
new Error("a released source rendered different text")
58+
);
59+
}
60+
}
61+
});
62+
});
63+
}
64+
};
65+
66+
/** @type {import("../../../../").Configuration} */
67+
module.exports = {
68+
// Keeps the two modules apart, so the chunk renders module factories rather
69+
// than inlining one module into its startup.
70+
optimization: { concatenateModules: false },
71+
plugins: [observeRenderedSourceRelease]
72+
};

0 commit comments

Comments
 (0)