Skip to content

Commit 864547a

Browse files
perf(javascript): release webpack's tree as the printer converts it (#22368)
The parse phase builds webpack's own tree and terser's, and held the first whole while converting. A node's children are dead once it is converted, so drop them there: peak RSS falls 1436 -> 1148 MiB on typescript.js (9.1 MiB) and 2266 -> 2175 MiB on webpack's own bundle, byte-identical output. The HTML-comment guard also read a template's text as source, so any bundle holding `<!--` or `-->` in a template declined after paying for the parse; it now runs after the template text is taken.
1 parent f321baf commit 864547a

3 files changed

Lines changed: 48 additions & 9 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+
Cut the peak memory of minifying through webpack's printer.

‎lib/javascript/syntax-printer.js‎

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8051,14 +8051,6 @@ const createTerserTree = ({ ast, parse }) => {
80518051
const { type } = token;
80528052
const { label } = type;
80538053
if (label === "eof") return;
8054-
// What terser reads as an HTML comment, whatever this parser made of it.
8055-
const first = source.charCodeAt(token.start);
8056-
if (
8057-
(first === 60 && source.startsWith("<!--", token.start)) ||
8058-
(first === 45 && source.startsWith("-->", token.start))
8059-
) {
8060-
throw DECLINE;
8061-
}
80628054
// terser reads a template's text from its opening backtick, or the
80638055
// `}` closing a substitution, through the next `${` or backtick.
80648056
if (inTemplateText) {
@@ -8075,6 +8067,16 @@ const createTerserTree = ({ ast, parse }) => {
80758067
}
80768068
return;
80778069
}
8070+
// What terser reads as an HTML comment, whatever this parser made of
8071+
// it. Template text returned above: read as text, it holds no
8072+
// comment, and only its token can start where one would.
8073+
const first = source.charCodeAt(token.start);
8074+
if (
8075+
(first === 60 && source.startsWith("<!--", token.start)) ||
8076+
(first === 45 && source.startsWith("-->", token.start))
8077+
) {
8078+
throw DECLINE;
8079+
}
80788080
if (label === "`") {
80798081
inTemplateText = true;
80808082
} else if (label === "{" || label === "${") {
@@ -9618,11 +9620,39 @@ const createTerserTree = ({ ast, parse }) => {
96189620
return new Type({ name: token.value, start: token, end: token });
96199621
};
96209622

9623+
/**
9624+
* Drops a converted node's links to its children, which nothing reads
9625+
* again: terser's tree is built bottom-up, and holding webpack's own whole
9626+
* behind the walk costs 83 MiB of peak on a 9 MiB asset.
9627+
* @param {EstreeNode} node a node whose conversion is done
9628+
* @returns {void}
9629+
*/
9630+
const release = (node) => {
9631+
const fields = /** @type {Record<string, unknown>} */ (
9632+
/** @type {unknown} */ (node)
9633+
);
9634+
for (const key in fields) {
9635+
if (fields[key] !== null && typeof fields[key] === "object") {
9636+
fields[key] = null;
9637+
}
9638+
}
9639+
};
9640+
96219641
/**
96229642
* @param {EstreeNode} node a node
96239643
* @returns {Node} terser's node for it
96249644
*/
96259645
const from = (node) => {
9646+
const converted = convert(node);
9647+
release(node);
9648+
return converted;
9649+
};
9650+
9651+
/**
9652+
* @param {EstreeNode} node a node
9653+
* @returns {Node} terser's node for it
9654+
*/
9655+
const convert = (node) => {
96269656
switch (node.type) {
96279657
case "Identifier":
96289658
// terser reads `let` as a keyword wherever it stands, and `await`

‎test/unitCases/syntax-printer-parse.unittest.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,11 @@ const CASES = [
187187
["import.meta and dynamic imports", "x = import.meta.url; y = import('z'); w = import('v', { with: {} });", true],
188188
["a default export ending at a line break", "export default function () {}\nfoo();", true],
189189
["a default export ending the source", "export default function () {}", true],
190-
["BigInt keys, which terser keys by their digits", "x = { 0b1n: 1, 1_0n: 2, 0x1Fn: 3 }; class A { 0o7n() {} }"]
190+
["BigInt keys, which terser keys by their digits", "x = { 0b1n: 1, 1_0n: 2, 0x1Fn: 3 }; class A { 0o7n() {} }"],
191+
[
192+
"HTML comment sequences a literal holds as text",
193+
'x = `<!-- a ${b} -->`; y = `${c}-->`; z = "<!--"; w = /-->/;'
194+
]
191195
];
192196

193197
/**

0 commit comments

Comments
 (0)