Skip to content

Commit afca4d8

Browse files
fix(javascript): keep minified output from growing where inlining or mangling copied names (#22717)
* fix(javascript): read the argument in place of a flattened call's parameter A function called in place copied each argument into a `var` unless the parameter had the argument's own name, so `/*#__INLINE__*/` output grew past its input (terser/terser#1179: 262 bytes from 202). The parameter now reads the argument where nothing writes either and the function declares no name like it. A `return` split from `a, void b` is also dropped where only declarations follow it in the function body. * fix(javascript): keep calls of a function a value read keeps A function only returning a literal had the literal copied into every call even where another read kept the function, so the output grew past its input (terser/terser#1328: 76 bytes from 69). A function passed on as a value now keeps its calls wherever the literal is longer. * fix(javascript): leave a scope's counted names to the keys its names want A destructured name the mangler would name after its key lost that name to one counted out earlier in the same scope, so `{ a, b, c }` printed as `{ a, b, c: n }` and terser/terser#1217 kept output longer than its input. Counting now skips the names a scope's definitions want spelled. * chore: add changeset for the minifier output growth fixes * fix(javascript): keep the `arguments.length` test before reading an index An index at or past `arguments.length` reads `Object.prototype`, where a getter may sit, so dropping Babel's `arguments.length > n` test before `arguments[n]` could run it. oxc and terser both keep the test. * fix(javascript): check an aliased parameter for closures in a loop A parameter read as its argument skipped the list of names the loop check walks, so a closure made in a loop could have read a later iteration's value. It now joins that list like every other parameter. * test: retire the outputs no longer bigger than swc's or oxc's Three outputs no longer trail swc and two no longer trail oxc. Six test262 getters are 13 to 17 bytes shorter but gzip 4 to 10 bytes worse, as the kept getter is called where terser repeats its string.
1 parent b6ea6e5 commit afca4d8

6 files changed

Lines changed: 122 additions & 147 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+
Keep minified JavaScript from growing where inlining or mangling copied names.

‎lib/javascript/syntax-printer.js‎

Lines changed: 58 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -21685,7 +21685,20 @@ const installImprove = (modules) => {
2168521685
}
2168621686
};
2168721687

21688-
// `function(a){…}(a)` flattens where `a` is taken, its body reading the `a`
21688+
/**
21689+
* Whether a function declares a name anywhere in it, its parameters included.
21690+
* @param {Node} lambda the function
21691+
* @param {string} name the name
21692+
* @returns {boolean} true when it does
21693+
*/
21694+
const declaresName = (lambda, name) =>
21695+
A.walk(lambda, (/** @type {Node} */ node) => {
21696+
if (A.isSymbolDeclarationNode(node) && node.name === name) {
21697+
return A.walk_abort;
21698+
}
21699+
});
21700+
21701+
// `function(a){…}(b)` flattens where `a` is taken, its body reading the `b`
2168921702
// around it, which nothing writes, so it holds the argument's value.
2169021703
inlineHooks.aliasesParameter = (call, lambda, index, compressor) => {
2169121704
if (!improvements.enabled) return false;
@@ -21704,7 +21717,8 @@ const installImprove = (modules) => {
2170421717
) ||
2170521718
!A.isSymbolFunargNode(parameter) ||
2170621719
!A.isSymbolRefNode(value) ||
21707-
value.name !== parameter.name
21720+
// Read under the argument's name, the body must declare none like it.
21721+
(value.name !== parameter.name && declaresName(lambda, value.name))
2170821722
) {
2170921723
return false;
2171021724
}
@@ -22871,133 +22885,6 @@ const installImprove = (modules) => {
2287122885
}
2287222886
);
2287322887

22874-
/** @type {WeakMap<Node, boolean>} */
22875-
const argumentsReadOnly = new WeakMap();
22876-
22877-
/**
22878-
* Whether a function only reads its `arguments` object's `length` and
22879-
* indices, so an index at or past `length` reads `undefined`.
22880-
* @param {Node} lambda a function
22881-
* @returns {boolean} true when nothing writes or passes the object on
22882-
*/
22883-
const readsArgumentsOnly = (lambda) => {
22884-
const known = argumentsReadOnly.get(lambda);
22885-
if (known !== undefined) return known;
22886-
let readOnly = true;
22887-
const walker = A.createWalker((/** @type {Node} */ node) => {
22888-
if (!readOnly) return true;
22889-
if (!A.isSymbolRefNode(node) || node.name !== "arguments") return;
22890-
const access = walker.parent();
22891-
const outer = walker.parent(1);
22892-
if (
22893-
!(
22894-
(A.isDotNode(access) && access.property.name === "length") ||
22895-
(A.isSubNode(access) && A.isNumberNode(access.property))
22896-
) ||
22897-
access.object !== node ||
22898-
(A.isAssignNode(outer) && outer.left === access) ||
22899-
(A.isUnaryNode(outer) &&
22900-
(outer.operator === "++" ||
22901-
outer.operator === "--" ||
22902-
outer.operator === "delete"))
22903-
) {
22904-
readOnly = false;
22905-
}
22906-
});
22907-
walkNode(lambda, walker);
22908-
argumentsReadOnly.set(lambda, readOnly);
22909-
return readOnly;
22910-
};
22911-
22912-
/**
22913-
* The index `arguments.length > n` tests for, where the function's own
22914-
* `arguments` object is only ever read.
22915-
* @param {Node} node a test
22916-
* @returns {number | undefined} the index n, or undefined
22917-
*/
22918-
const testedArgumentIndex = (node) => {
22919-
if (!A.isBinaryNode(node) || A.isAssignNode(node)) return;
22920-
let length;
22921-
let count;
22922-
if (node.operator === ">") [length, count] = [node.left, node.right];
22923-
else if (node.operator === "<") [count, length] = [node.left, node.right];
22924-
else return;
22925-
if (
22926-
!A.isDotNode(length) ||
22927-
length.property.name !== "length" ||
22928-
!A.isArgumentsObject(length.object) ||
22929-
!A.isNumberNode(count) ||
22930-
!readsArgumentsOnly(length.object.definition.scope.node)
22931-
) {
22932-
return;
22933-
}
22934-
return count.value;
22935-
};
22936-
22937-
/**
22938-
* @param {Node} node an expression
22939-
* @returns {boolean} whether it is `undefined` or `void` of a constant
22940-
*/
22941-
const isUndefinedValue = (node) =>
22942-
A.isUndefinedNode(node) ||
22943-
(A.isUnaryPrefixNode(node) &&
22944-
node.operator === "void" &&
22945-
A.isConstantNode(node.argument));
22946-
22947-
/**
22948-
* Whether a node reads `arguments[n]` off the same object a test read.
22949-
* @param {Node} node an expression
22950-
* @param {number} index the index n
22951-
* @returns {boolean} true for that read
22952-
*/
22953-
const readsArgument = (node, index) =>
22954-
A.isSubNode(node) &&
22955-
A.isArgumentsObject(node.object) &&
22956-
A.isNumberNode(node.property) &&
22957-
node.property.value === index;
22958-
22959-
// Babel's default parameters test `arguments.length > n` before reading
22960-
// `arguments[n]`, which reads `undefined` past the length anyway.
22961-
A.wrapOptimizer("Binary", (optimizeBinary) => (self, compressor) => {
22962-
const optimized = optimizeBinary(self, compressor);
22963-
if (
22964-
!improvements.enabled ||
22965-
!A.isBinaryNode(optimized) ||
22966-
optimized.operator !== "&&"
22967-
) {
22968-
return optimized;
22969-
}
22970-
const index = testedArgumentIndex(optimized.left);
22971-
const { right } = optimized;
22972-
if (
22973-
index === undefined ||
22974-
!A.isBinaryNode(right) ||
22975-
right.operator !== "!==" ||
22976-
!(
22977-
(isUndefinedValue(right.left) && readsArgument(right.right, index)) ||
22978-
(isUndefinedValue(right.right) && readsArgument(right.left, index))
22979-
)
22980-
) {
22981-
return optimized;
22982-
}
22983-
return right;
22984-
});
22985-
A.wrapOptimizer(
22986-
"Conditional",
22987-
(optimizeConditional) => (self, compressor) => {
22988-
const optimized = optimizeConditional(self, compressor);
22989-
if (!improvements.enabled || !A.isConditionalNode(optimized)) {
22990-
return optimized;
22991-
}
22992-
const index = testedArgumentIndex(optimized.test);
22993-
return index !== undefined &&
22994-
readsArgument(optimized.consequent, index) &&
22995-
isUndefinedValue(optimized.alternate)
22996-
? optimized.consequent
22997-
: optimized;
22998-
}
22999-
);
23000-
2300122888
/**
2300222889
* Whether the value of the node visited goes on as it is, where no test or
2300322890
* operator around it could fold a constant written in its place.
@@ -23063,7 +22950,10 @@ const installImprove = (modules) => {
2306322950
A.nodeSize(written, compressor) - A.nodeSize(call, compressor);
2306422951
// The function stays for the calls kept, so two of them must repay it: a
2306522952
// call read by a test or an operator still inlines, and may be the rest.
23066-
return compressor.option("unused") && !compressor.exposed(definition)
22953+
// Passed on as a value, it stays whatever its calls become.
22954+
return compressor.option("unused") &&
22955+
!compressor.exposed(definition) &&
22956+
!definition.escaped
2306722957
? Math.min(definition.references.length, 2) * saved >
2306822958
A.nodeSize(fn, compressor)
2306922959
: saved > 0;
@@ -25591,6 +25481,11 @@ const installImprove = (modules) => {
2559125481
for (let i = 0; i < body.length; i++) {
2559225482
const effects = splitEffectsOf(body[i], walker);
2559325483
if (effects === undefined) continue;
25484+
// A function's own body ending in what only declares falls off there.
25485+
if (A.isLambdaNode(node) && body.slice(i + 1).every(onlyDeclares)) {
25486+
body[i] = effects;
25487+
continue;
25488+
}
2559425489
body.splice(
2559525490
i,
2559625491
1,
@@ -25601,6 +25496,19 @@ const installImprove = (modules) => {
2560125496
}
2560225497
};
2560325498

25499+
/**
25500+
* @param {Node} statement a statement
25501+
* @returns {boolean} whether running it does nothing: a function declaration or `var` without values
25502+
*/
25503+
const onlyDeclares = (statement) =>
25504+
A.isDefunNode(statement) ||
25505+
A.isEmptyStatementNode(statement) ||
25506+
(A.isVarNode(statement) &&
25507+
statement.declarations.every(
25508+
(/** @type {Node} */ declaration) =>
25509+
!declaration.init && A.isSymbolDeclarationNode(declaration.id)
25510+
));
25511+
2560425512
// The unary operators that evaluate their operand before anything else.
2560525513
const OPERAND_FIRST_UNARY = new Set(["!", "-", "+", "~", "void", "typeof"]);
2560625514

@@ -33210,6 +33118,13 @@ const createInline = (modules) => {
3321033118
if (variable === undefined) return false;
3321133119
(state.reuses || (state.reuses = new Map())).set(arg, variable);
3321233120
}
33121+
} else if (
33122+
state.self.callee === lambda &&
33123+
inlineHooks.aliasesParameter !== null &&
33124+
inlineHooks.aliasesParameter(state.self, lambda, i, state.compressor)
33125+
) {
33126+
// Read in place, the argument needs no `var` holding a copy.
33127+
(state.aliases || (state.aliases = [])).push(arg);
3321333128
}
3321433129
if (inLoop) inLoop.push(arg.definition);
3321533130
}
@@ -33391,6 +33306,7 @@ const createInline = (modules) => {
3339133306
walk(lambda.body, (/** @type {Node} */ node) => {
3339233307
if (isSymbolRefNode(node) && node.definition === parameter) {
3339333308
node.definition = variable;
33309+
node.name = variable.name;
3339433310
variable.references.push(node);
3339533311
}
3339633312
});
@@ -46119,10 +46035,18 @@ const installEstreeMangle = (modules) => {
4611946035
let givenIn = null;
4612046036
/** @type {Set<string>} */
4612146037
const spelled = new Set();
46038+
// The names a scope's definitions want spelled, which counting skips there.
46039+
/** @type {Map<MinifierScope, Set<string>> | null} */
46040+
let wantedIn = null;
4612246041
if (wanted.size !== 0) {
4612346042
givenIn = new Map();
46124-
for (const definition of wanted.keys()) {
46125-
givenIn.set(countingScopeOf(definition, blockDefunScopes), new Set());
46043+
wantedIn = new Map();
46044+
for (const [definition, spelling] of wanted) {
46045+
const counting = countingScopeOf(definition, blockDefunScopes);
46046+
givenIn.set(counting, new Set());
46047+
const names = wantedIn.get(counting);
46048+
if (names === undefined) wantedIn.set(counting, new Set([spelling]));
46049+
else names.add(spelling);
4612646050
}
4612746051
}
4612846052
for (let i = 0; i < toMangle.length; i++) {
@@ -46151,6 +46075,7 @@ const installEstreeMangle = (modules) => {
4615146075
const counting = countingScopeOf(definition, blockDefunScopes);
4615246076
const taken = takenIn(counting);
4615346077
const given = givenIn === null ? undefined : givenIn.get(counting);
46078+
const wantedHere = wantedIn === null ? undefined : wantedIn.get(counting);
4615446079
if (given !== undefined) {
4615546080
const spelling = wanted.get(definition);
4615646081
if (
@@ -46178,6 +46103,7 @@ const installEstreeMangle = (modules) => {
4617846103
if (taken.has(name)) continue;
4617946104
if (shadowed !== null && shadowed === name) continue;
4618046105
if (given !== undefined && given.has(name)) continue;
46106+
if (wantedHere !== undefined && wantedHere.has(name)) continue;
4618146107
break;
4618246108
}
4618346109
if (given !== undefined) given.add(name);

‎test/helpers/oxcSmaller.js‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -607,11 +607,9 @@ const OXC_SMALLER_BY_REASON = [
607607
"swc minifier: benches-full/jquery.js",
608608
"swc minifier: benches-full/three.js",
609609
"swc minifier: benches-full/vue.js",
610-
"swc minifier: fixture/issues/5682/input.js",
611610
"swc minifier: fixture/issues/d3-color/1/input.js",
612611
"swc minifier: fixture/issues/firebase/dist/1/input.js",
613612
"swc minifier: fixture/issues/react/hooks/3/input.js",
614-
"swc minifier: fixture/issues/react-instancesearch/004/input.js",
615613
"swc minifier: fixture/next/36127/2/3/input.js",
616614
"swc minifier: fixture/next/41527/1/input.js",
617615
"swc minifier: fixture/next/47005/input.js",

‎test/helpers/swcSmaller.js‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,6 @@ const SWC_SMALLER_BY_REASON = [
257257
"terser compress: reduce_vars/defun_inline_2",
258258
"terser compress: reduce_vars/issue_1606",
259259
"terser compress: reduce_vars/issue_1850_4",
260-
"terser compress: reduce_vars/issue_294",
261260
"terser compress: reduce_vars/issue_3110_3",
262261
"terser compress: reduce_vars/regex_loop",
263262
"terser compress: reduce_vars/unsafe_evaluate_array_1",
@@ -345,8 +344,7 @@ const SWC_SMALLER_BY_REASON = [
345344
"terser compress: identity/inline_identity_higher_order",
346345
"terser compress: inline/issue_308",
347346
"terser compress: issue-t292/no_flatten_with_arg_colliding_with_arg_value_inner_scope",
348-
"terser compress: issue-t292/no_flatten_with_var_colliding_with_arg_value_inner_scope",
349-
"terser compress: reduce_vars/variables_collision_in_immediately_invoked_func"
347+
"terser compress: issue-t292/no_flatten_with_var_colliding_with_arg_value_inner_scope"
350348
]
351349
],
352350
[
@@ -682,7 +680,6 @@ const SWC_SMALLER_BY_REASON = [
682680
"swc minifier: fixture/next/66237/input.js",
683681
"swc minifier: fixture/next/66378/input.js",
684682
"swc minifier: fixture/next/brotli-decompress/static/chunks/545-ff8f898b276d138f/input.js",
685-
"swc minifier: fixture/next/chakra/input.js",
686683
"swc minifier: fixture/next/cryptojs/input.js",
687684
"swc minifier: fixture/next/feedback-3/579-dcac359116b2707c/input.js",
688685
"swc minifier: fixture/next/octokit/static/chunks/357-72bd409f1472b1b8/input.js",

‎test/specCases/minify-corpora.spectest.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,6 +1444,18 @@ const IMPROVED_YET_BIGGER = {
14441444
"11 to 14 bytes fewer, 2 to 3 more gzipped: each folded call repeats the string the test asserts it equals",
14451445
"return-values.js (the default minimizer's options)":
14461446
"392 bytes fewer, 18 more gzipped: each folded call repeats the string the test asserts it equals",
1447+
"15.2.3.6-4-314.js (the default minimizer's options)":
1448+
"13 to 17 bytes fewer, 4 to 10 more gzipped: the getter kept for `get` is called where terser repeats the string it returns, which gzip reused",
1449+
"15.2.3.6-4-314-1.js (the default minimizer's options)":
1450+
"13 to 17 bytes fewer, 4 to 10 more gzipped: the getter kept for `get` is called where terser repeats the string it returns, which gzip reused",
1451+
"15.2.3.6-4-315.js (the default minimizer's options)":
1452+
"13 to 17 bytes fewer, 4 to 10 more gzipped: the getter kept for `get` is called where terser repeats the string it returns, which gzip reused",
1453+
"15.2.3.6-4-315-1.js (the default minimizer's options)":
1454+
"13 to 17 bytes fewer, 4 to 10 more gzipped: the getter kept for `get` is called where terser repeats the string it returns, which gzip reused",
1455+
"15.2.3.6-4-321.js (the default minimizer's options)":
1456+
"13 to 17 bytes fewer, 4 to 10 more gzipped: the getter kept for `get` is called where terser repeats the string it returns, which gzip reused",
1457+
"15.2.3.6-4-321-1.js (the default minimizer's options)":
1458+
"13 to 17 bytes fewer, 4 to 10 more gzipped: the getter kept for `get` is called where terser repeats the string it returns, which gzip reused",
14471459
};
14481460

14491461
// Sources whose shorter spelling no longer repeats text gzip matched, by the

0 commit comments

Comments
 (0)