Skip to content

Commit 47ace05

Browse files
authored
fix(cache): invalidate unaffected caches when connection activity changes (#22431)
1 parent c1be4e2 commit 47ace05

11 files changed

Lines changed: 100 additions & 12 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+
Invalidate unaffected module caches when connection activity changes.

‎lib/Compilation.js‎

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ const reduceAffectType = (connections) => {
650650
return affected;
651651
};
652652

653-
/** @typedef {{ id: ModuleId, modules?: Map<Module, ModuleId>, blocks?: (ChunkId | null)[], sourceTypes?: SourceTypes }} References */
653+
/** @typedef {{ id: ModuleId, modules?: Map<Module, string>, blocks?: (ChunkId | null)[], sourceTypes?: SourceTypes }} References */
654654
/** @typedef {Map<Module, WeakTupleMap<EXPECTED_ANY[], EXPECTED_ANY>>} ModuleMemCaches */
655655
/** @typedef {{ chunk: Chunk, load: () => Promise<EXPECTED_ANY>, create: (loaded: EXPECTED_ANY) => RuntimeModule, chunkGraph: ChunkGraph }} PendingRuntimeModule */
656656

@@ -3126,23 +3126,40 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
31263126
let statUnchanged = 0;
31273127
let statChanged = 0;
31283128
let statNew = 0;
3129+
/**
3130+
* Include connection activity because optimizations can change it without changing module ids.
3131+
* @param {Module} module connected module
3132+
* @param {readonly ModuleGraphConnection[]} connections connections to it
3133+
* @returns {string} reference key
3134+
*/
3135+
const referenceKey = (module, connections) => {
3136+
let key = `${chunkGraph.getModuleId(module)}`;
3137+
for (const connection of connections) {
3138+
const state = connection.getActiveState(undefined);
3139+
key += state === true ? "T" : state === false ? "F" : "O";
3140+
}
3141+
return key;
3142+
};
31293143
/**
31303144
* Compute references.
31313145
* @param {Module} module module
31323146
* @returns {References} references
31333147
*/
31343148
const computeReferences = (module) => {
31353149
const id = /** @type {ModuleId} */ (chunkGraph.getModuleId(module));
3136-
/** @type {Map<Module, ModuleId> | undefined} */
3150+
/** @type {Map<Module, string> | undefined} */
31373151
let modules;
31383152
/** @type {(ChunkId | null)[] | undefined} */
31393153
let blocks;
31403154
const outgoing = moduleGraph.getOutgoingConnectionsByModule(module);
31413155
if (outgoing !== undefined) {
3142-
for (const m of outgoing.keys()) {
3143-
if (!m) continue;
3156+
for (const [referencedModule, connections] of outgoing) {
3157+
if (!referencedModule) continue;
31443158
if (modules === undefined) modules = new Map();
3145-
modules.set(m, /** @type {ModuleId} */ (chunkGraph.getModuleId(m)));
3159+
modules.set(
3160+
referencedModule,
3161+
referenceKey(referencedModule, connections)
3162+
);
31463163
}
31473164
}
31483165
if (module.blocks.length > 0) {
@@ -3173,7 +3190,7 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
31733190
* @param {Module} module module
31743191
* @param {object} references references
31753192
* @param {string | number} references.id id
3176-
* @param {Map<Module, string | number | undefined>=} references.modules modules
3193+
* @param {Map<Module, string>=} references.modules modules
31773194
* @param {(string | number | null)[]=} references.blocks blocks
31783195
* @param {SourceTypes=} references.sourceTypes connection-dependent source types
31793196
* @returns {boolean} ok?
@@ -3184,8 +3201,15 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
31843201
) => {
31853202
if (id !== chunkGraph.getModuleId(module)) return false;
31863203
if (modules !== undefined) {
3187-
for (const [module, id] of modules) {
3188-
if (chunkGraph.getModuleId(module) !== id) return false;
3204+
const outgoing = moduleGraph.getOutgoingConnectionsByModule(module);
3205+
for (const [referencedModule, key] of modules) {
3206+
const connections = outgoing && outgoing.get(referencedModule);
3207+
if (
3208+
connections === undefined ||
3209+
referenceKey(referencedModule, connections) !== key
3210+
) {
3211+
return false;
3212+
}
31893213
}
31903214
}
31913215
if (blocks !== undefined) {

‎lib/graph/buildChunkGraph.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,10 +358,21 @@ const visitModules = (
358358
const module = /** @type {Module} */ (block.getRootBlock());
359359
const memCache = moduleMemCaches && moduleMemCaches.get(module);
360360
if (memCache !== undefined) {
361+
// an optimization (e.g. side effects) can flip a connection's active
362+
// state without the module changing — key the cache by the states too
363+
let stateEtag = "";
364+
for (const connection of moduleGraph.getOutgoingConnections(module)) {
365+
if (!connection.dependency || !connection.module || connection.weak) {
366+
continue;
367+
}
368+
const state = connection.getActiveState(runtime);
369+
stateEtag += state === true ? "T" : state === false ? "F" : "O";
370+
}
361371
/** @type {BlockModulesMap} */
362372
const map = memCache.provide(
363373
"bundleChunkGraph.blockModules",
364374
runtime,
375+
stateEtag,
365376
() => {
366377
logger.time("visitModules: prepare");
367378
const map = new Map();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
STATE.effect = WATCH_STEP;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import "./effect.js";
2+
3+
it("should update cached code when a connection changes activity", () => {
4+
expect(STATE.effect).toBe(WATCH_STEP === "1" ? "0" : WATCH_STEP);
5+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import "./effect.js";
2+
export default 0;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import "./effect.js";
2+
export default 1;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import "./effect.js";
2+
export default 2;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"use strict";
2+
3+
module.exports = {
4+
/** @returns {string} bundle containing the runtime assertions */
5+
findBundle() {
6+
return "main.js";
7+
}
8+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"use strict";
2+
3+
const path = require("path");
4+
const currentWatchStep = require("../../../helpers/currentWatchStep");
5+
6+
/** @type {import("../../../..").Configuration} */
7+
module.exports = {
8+
entry: { main: "./index.js", trigger: "./tick.js" },
9+
experiments: { cacheUnaffected: true },
10+
output: { filename: "[name].js" },
11+
plugins: [
12+
(compiler) => {
13+
compiler.hooks.compilation.tap("ConnectionActivity", (compilation) => {
14+
compilation.hooks.optimizeDependencies.tap("ConnectionActivity", () => {
15+
for (const module of compilation.modules) {
16+
if (
17+
module.nameForCondition() !==
18+
path.join(compiler.context, "index.js")
19+
) {
20+
continue;
21+
}
22+
for (const connection of compilation.moduleGraph.getOutgoingConnections(
23+
module
24+
)) {
25+
connection.setActive(currentWatchStep.step !== "1");
26+
}
27+
}
28+
});
29+
});
30+
}
31+
]
32+
};

0 commit comments

Comments
 (0)