Skip to content

Commit 9acc878

Browse files
perf(graph): stop holding empty per-module arrays (#22319)
A plain import specifier records no member ranges, so the two parser sites now hand the dependency the shared empty array instead of an empty slice: 1802 arrays on a 956-module build become one. Flushing a module's pending connections emptied its array and kept it; releasing it instead frees 818 arrays, and the creation counter shows it is built once per module either way, so nothing is re-allocated.
1 parent 52d94db commit 9acc878

3 files changed

Lines changed: 17 additions & 11 deletions

File tree

‎.changeset/043-graph-hot-path-allocations.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"webpack": patch
33
---
44

5-
Reduce allocations on module and chunk graph hot paths.
5+
Reduce allocations on module graph, chunk graph and ESM import hot paths.

‎lib/dependencies/esm/ESMImportDependencyParserPlugin.js‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -555,11 +555,14 @@ class HarmonyImportDependencyParserPlugin {
555555
members,
556556
membersOptionals
557557
);
558+
const rangeCount =
559+
memberRanges.length - (members.length - nonOptionalMembers.length);
560+
// Nothing left to record is the common case, so share one array.
558561
/** @type {Range[]} */
559-
const ranges = memberRanges.slice(
560-
0,
561-
memberRanges.length - (members.length - nonOptionalMembers.length)
562-
);
562+
const ranges =
563+
rangeCount > 0
564+
? memberRanges.slice(0, rangeCount)
565+
: EMPTY_ID_RANGES;
563566
const expr =
564567
nonOptionalMembers !== members
565568
? getNonOptionalMemberChain(
@@ -615,11 +618,14 @@ class HarmonyImportDependencyParserPlugin {
615618
members,
616619
membersOptionals
617620
);
621+
const rangeCount =
622+
memberRanges.length - (members.length - nonOptionalMembers.length);
623+
// Nothing left to record is the common case, so share one array.
618624
/** @type {Range[]} */
619-
const ranges = memberRanges.slice(
620-
0,
621-
memberRanges.length - (members.length - nonOptionalMembers.length)
622-
);
625+
const ranges =
626+
rangeCount > 0
627+
? memberRanges.slice(0, rangeCount)
628+
: EMPTY_ID_RANGES;
623629
const expr =
624630
nonOptionalMembers !== members
625631
? getNonOptionalMemberChain(

‎lib/graph/ModuleGraph.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ class ModuleGraph {
461461
connection
462462
);
463463
}
464-
pending.length = 0;
464+
mgm._unassignedConnections = undefined;
465465
}
466466

467467
/**
@@ -739,7 +739,7 @@ class ModuleGraph {
739739
foundConnection = connection;
740740
}
741741
}
742-
mgm._unassignedConnections.length = 0;
742+
mgm._unassignedConnections = undefined;
743743
if (foundConnection !== undefined) {
744744
return foundConnection;
745745
}

0 commit comments

Comments
 (0)