Skip to content

Commit 0da03bd

Browse files
perf(javascript): print a simple function's call as its value in the one pass
terser's `inline` of simple functions: a function written in place, or moved to its one read, whose body only returns an expression and which is passed nothing, or for each parameter a name never written, prints as that expression, each parameter read as the name passed for it. Claude-Session: https://claude.ai/code/session_01Tgxgyb4N35ndP6cC4KcrXC
1 parent c494697 commit 0da03bd

3 files changed

Lines changed: 281 additions & 4 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+
Print a function called once with no or unchanged names passed as its returned value in the one-pass JavaScript minifier.

‎lib/javascript/syntax-printer.js‎

Lines changed: 259 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60596,6 +60596,10 @@ const createNodePrinter = (modules) => {
6059660596
const nameOf = (node) => {
6059760597
if (tree.type(node) === NodeType.Literal) return "Infinity";
6059860598
const definition = mangledNames === null ? null : mangledNames[node];
60599+
if (substituted.size !== 0 && definition) {
60600+
const passed = substituted.get(definition);
60601+
if (passed !== undefined) return nameOf(passed);
60602+
}
6059960603
return definition
6060060604
? definition.mangled_name || definition.name
6060160605
: tree.name(node);
@@ -60895,7 +60899,8 @@ const createNodePrinter = (modules) => {
6089560899
}
6089660900
case NodeType.ReturnStatement:
6089760901
return (
60898-
tree.type(grandparent) === NodeType.ArrowFunctionExpression &&
60902+
(tree.type(grandparent) === NodeType.ArrowFunctionExpression ||
60903+
(reducing.size !== 0 && reducing.has(grandparent))) &&
6089960904
tree.fieldCount(Field.body, parent) === 1
6090060905
);
6090160906
case NodeType.FunctionExpression: {
@@ -62679,6 +62684,20 @@ const createNodePrinter = (modules) => {
6267962684
// By assignment, 1 where nothing reads the name it sets: its value prints.
6268062685
/** @type {Uint8Array} */
6268162686
let dropsWrite = new Uint8Array(0);
62687+
// The functions called once with nothing passed that print as the value
62688+
// they return, and those calls, terser's `inline` of simple functions.
62689+
/** @type {Map<number, number>} */
62690+
const reducing = new Map();
62691+
/** @type {Map<number, number>} */
62692+
const reducedCalls = new Map();
62693+
// By parameter of such a function, the name passed for it, read instead.
62694+
/** @type {Map<TreeDefinition, number>} */
62695+
const substituted = new Map();
62696+
// By read of a function moved there, the call with nothing passed it is.
62697+
/** @type {Map<number, number>} */
62698+
const callReads = new Map();
62699+
/** @type {Set<number>} */
62700+
const moveReads = new Set();
6268262701
// By declarator, 1 for an alias whose reads read the name it is set to,
6268362702
// 2 for a function its one read prints.
6268462703
/** @type {Uint8Array} */
@@ -63219,6 +63238,138 @@ const createNodePrinter = (modules) => {
6321963238
);
6322063239
};
6322163240

63241+
/**
63242+
* @param {number} fn a function
63243+
* @param {number} call a call of it
63244+
* @returns {number} the one expression it returns, which the call may print as, 0 where none: it waits, yields, holds comments, reads its own `this`, `arguments` or name, or is passed other than a name never written for each parameter, which a function or class it holds could not read
63245+
*/
63246+
const reducedValue = (fn, call) => {
63247+
const count = tree.fieldCount(Field.params, fn);
63248+
if (
63249+
count !== tree.fieldCount(Field.arguments, call) ||
63250+
tree.flag(Flag.async, fn) ||
63251+
tree.flag(Flag.generator, fn) ||
63252+
holdsComments(fn)
63253+
) {
63254+
return 0;
63255+
}
63256+
for (let i = 0; i < count; i++) {
63257+
const param = tree.field(i, Field.params, fn);
63258+
const argument = itemOf(i, Field.arguments, call);
63259+
if (
63260+
typeOf(param) !== NodeType.Identifier ||
63261+
typeOf(argument) !== NodeType.Identifier ||
63262+
!isFixedName(param) ||
63263+
!isFixedName(argument)
63264+
) {
63265+
return 0;
63266+
}
63267+
}
63268+
let value = childOf(Field.body, fn);
63269+
if (typeOf(value) === NodeType.BlockStatement) {
63270+
if (tree.fieldCount(Field.body, value) !== 1) return 0;
63271+
const statement = tree.field(0, Field.body, value);
63272+
if (typeOf(statement) !== NodeType.ReturnStatement) return 0;
63273+
value = childOf(Field.argument, statement);
63274+
if (value === 0) return 0;
63275+
}
63276+
const id = tree.field(0, Field.id, fn);
63277+
const name = id === 0 ? "" : tree.name(id);
63278+
return findNode(value, (node) => {
63279+
switch (tree.type(node)) {
63280+
case NodeType.ThisExpression:
63281+
case NodeType.Super:
63282+
case NodeType.MetaProperty:
63283+
return true;
63284+
case NodeType.Identifier:
63285+
return tree.name(node) === "arguments" || tree.name(node) === name;
63286+
case NodeType.FunctionExpression:
63287+
case NodeType.FunctionDeclaration:
63288+
case NodeType.ArrowFunctionExpression:
63289+
case NodeType.ClassExpression:
63290+
case NodeType.ClassDeclaration:
63291+
// What it reads there is its own, but for a name.
63292+
return Boolean(
63293+
count !== 0 ||
63294+
(name !== "" &&
63295+
findNode(node, (inner) =>
63296+
tree.type(inner) === NodeType.Identifier &&
63297+
tree.name(inner) === name
63298+
? true
63299+
: undefined
63300+
))
63301+
);
63302+
default:
63303+
return undefined;
63304+
}
63305+
})
63306+
? 0
63307+
: value;
63308+
};
63309+
63310+
/**
63311+
* @param {number} name a name
63312+
* @returns {boolean} whether it is a variable of this source nothing ever writes but its declaration
63313+
*/
63314+
const isFixedName = (name) => {
63315+
const definition = /** @type {TreeDefinition[]} */ (mangledNames)[name];
63316+
return (
63317+
isMinifierVariable(definition) &&
63318+
!definition.undeclared &&
63319+
!definition.written
63320+
);
63321+
};
63322+
63323+
/**
63324+
* Notes a function a call prints as the value of, each parameter read
63325+
* printing as the name passed for it.
63326+
* @param {number} fn the function
63327+
* @param {number} call the call
63328+
* @param {number} value what it returns
63329+
* @returns {void}
63330+
*/
63331+
const noteReduced = (fn, call, value) => {
63332+
reducing.set(fn, value);
63333+
reducedCalls.set(call, fn);
63334+
const count = tree.fieldCount(Field.params, fn);
63335+
for (let i = 0; i < count; i++) {
63336+
substituted.set(
63337+
/** @type {TreeDefinition[]} */ (mangledNames)[
63338+
tree.field(i, Field.params, fn)
63339+
],
63340+
itemOf(i, Field.arguments, call)
63341+
);
63342+
}
63343+
};
63344+
63345+
/**
63346+
* @param {number} call a node
63347+
* @returns {boolean} whether it is a call that may print as what it calls returns: no `?.`, no comment on it
63348+
*/
63349+
const passesNothing = (call) =>
63350+
typeOf(call) === NodeType.CallExpression &&
63351+
!tree.flag(Flag.optional, call) &&
63352+
!hasEdgeComments(call);
63353+
63354+
/**
63355+
* Notes a call of a function written in place that prints as its value.
63356+
* @param {number} call a call
63357+
* @returns {void}
63358+
*/
63359+
const noteReducedCall = (call) => {
63360+
const callee = childOf(Field.callee, call);
63361+
const type = typeOf(callee);
63362+
if (
63363+
(type !== NodeType.FunctionExpression &&
63364+
type !== NodeType.ArrowFunctionExpression) ||
63365+
!passesNothing(call)
63366+
) {
63367+
return;
63368+
}
63369+
const value = reducedValue(callee, call);
63370+
if (value !== 0) noteReduced(callee, call, value);
63371+
};
63372+
6322263373
/**
6322363374
* Notes a statement only setting a name with `=`, terser's `unused` of an
6322463375
* assignment: the name it sets counts as unread there, so a name only set
@@ -63318,6 +63469,15 @@ const createNodePrinter = (modules) => {
6331863469
} else if (type === NodeType.Identifier) {
6331963470
noteRead(node, parent);
6332063471
if (probingTry !== 0) probedReads.add(node);
63472+
if (
63473+
moveReads.has(node) &&
63474+
passesNothing(parent) &&
63475+
childOf(Field.callee, parent) === node
63476+
) {
63477+
callReads.set(node, parent);
63478+
}
63479+
} else if (type === NodeType.CallExpression) {
63480+
noteReducedCall(node);
6332163481
} else if (type === NodeType.WithStatement) {
6332263482
answersWith = true;
6332363483
}
@@ -63990,6 +64150,14 @@ const createNodePrinter = (modules) => {
6399064150
readingFunction = program;
6399164151
functionStack.length = 0;
6399264152
functionStack.push(program);
64153+
reducing.clear();
64154+
reducedCalls.clear();
64155+
substituted.clear();
64156+
callReads.clear();
64157+
moveReads.clear();
64158+
if (treeMoves !== null) {
64159+
for (let i = 1; i < treeMoves.length; i += 2) moveReads.add(treeMoves[i]);
64160+
}
6399364161
/** @type {number[]} */
6399464162
const candidates = [];
6399564163
collectDroppable(program, 0, candidates);
@@ -64051,12 +64219,19 @@ const createNodePrinter = (modules) => {
6405164219
}
6405264220
for (let i = 0; i < moves.length; i += 2) {
6405364221
if (dropped[moves[i]] !== 1) continue;
64054-
if (typeOf(moves[i]) === NodeType.FunctionDeclaration) {
64222+
const fn =
64223+
typeOf(moves[i]) === NodeType.FunctionDeclaration
64224+
? moves[i]
64225+
: childOf(Field.init, moves[i]);
64226+
if (fn === moves[i]) {
6405564227
defunReads.set(moves[i + 1], moves[i]);
6405664228
movedDefuns.add(moves[i]);
6405764229
} else {
64058-
movedReads.set(childOf(Field.init, moves[i]), moves[i + 1]);
64230+
movedReads.set(fn, moves[i + 1]);
6405964231
}
64232+
const call = callReads.get(moves[i + 1]);
64233+
const value = call === undefined ? 0 : reducedValue(fn, call);
64234+
if (value !== 0) noteReduced(fn, /** @type {number} */ (call), value);
6406064235
}
6406164236
};
6406264237

@@ -65015,12 +65190,85 @@ const createNodePrinter = (modules) => {
6501565190
printNode(childOf(Field.right, node));
6501665191
};
6501765192

65193+
/**
65194+
* Prints a function called with nothing passed as the value it returns.
65195+
* @param {number} node the function
65196+
* @returns {void}
65197+
*/
65198+
const printReduced = (node) => {
65199+
const id = tree.field(0, Field.id, node);
65200+
if (id !== 0) skipNode(id);
65201+
const count = tree.fieldCount(Field.params, node);
65202+
for (let i = 0; i < count; i++) {
65203+
skipNode(tree.field(i, Field.params, node));
65204+
}
65205+
printNode(/** @type {number} */ (reducing.get(node)));
65206+
};
65207+
65208+
/**
65209+
* @param {number} call a call printing as the value its function returns
65210+
* @param {number} value that value
65211+
* @returns {boolean} whether the value needs parentheses where the call is
65212+
*/
65213+
const reducedParens = (call, value) => {
65214+
const type = typeOf(value);
65215+
if (
65216+
type === NodeType.Identifier ||
65217+
type === NodeType.ThisExpression ||
65218+
type === NodeType.MemberExpression ||
65219+
type === NodeType.CallExpression ||
65220+
type === NodeType.ArrayExpression ||
65221+
type === NodeType.TemplateLiteral ||
65222+
(type === NodeType.Literal && typeof tree.value(value) === "string")
65223+
) {
65224+
return false;
65225+
}
65226+
if (type === NodeType.SequenceExpression) return true;
65227+
const parent = parentAt(0);
65228+
switch (typeOf(parent)) {
65229+
case NodeType.VariableDeclarator:
65230+
case NodeType.ReturnStatement:
65231+
case NodeType.ThrowStatement:
65232+
case NodeType.ArrayExpression:
65233+
case NodeType.SpreadElement:
65234+
case NodeType.TemplateLiteral:
65235+
return false;
65236+
case NodeType.ExpressionStatement:
65237+
return opensAmbiguously(value);
65238+
case NodeType.CallExpression:
65239+
case NodeType.NewExpression:
65240+
return childOf(Field.callee, parent) === call;
65241+
case NodeType.AssignmentExpression:
65242+
return childOf(Field.right, parent) !== call;
65243+
case NodeType.Property:
65244+
return childOf(Field.value, parent) !== call;
65245+
default:
65246+
return true;
65247+
}
65248+
};
65249+
6501865250
/**
6501965251
* terser's `Call` printer.
6502065252
* @param {number} node a call or `new`
6502165253
* @returns {void}
6502265254
*/
6502365255
const printCall = (node) => {
65256+
const fn = reducedCalls.size === 0 ? undefined : reducedCalls.get(node);
65257+
if (fn !== undefined) {
65258+
// The function called prints as the value it returns.
65259+
const parens = reducedParens(
65260+
node,
65261+
/** @type {number} */ (reducing.get(fn))
65262+
);
65263+
if (parens) output.print("(");
65264+
printNode(childOf(Field.callee, node));
65265+
if (parens) output.print(")");
65266+
const count = tree.fieldCount(Field.arguments, node);
65267+
for (let i = 0; i < count; i++) {
65268+
skipNode(itemOf(i, Field.arguments, node));
65269+
}
65270+
return;
65271+
}
6502465272
printNode(childOf(Field.callee, node));
6502565273
if (
6502665274
typeOf(node) === NodeType.NewExpression &&
@@ -66433,9 +66681,17 @@ const createNodePrinter = (modules) => {
6643366681
generators[NodeType.FunctionExpression] = generators[
6643466682
NodeType.FunctionDeclaration
6643566683
] = (node) => {
66684+
if (reducing.size !== 0 && reducing.has(node)) {
66685+
printReduced(node);
66686+
return;
66687+
}
6643666688
printLambda(node, false);
6643766689
};
6643866690
generators[NodeType.ArrowFunctionExpression] = (node) => {
66691+
if (reducing.size !== 0 && reducing.has(node)) {
66692+
printReduced(node);
66693+
return;
66694+
}
6643966695
if (!isPrivateInRight(node, 0)) {
6644066696
printArrow(node);
6644166697
return;

‎test/unitCases/javascript/printer.unittest.js‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14755,7 +14755,7 @@ describe("JavaScript minifier printing a SyntaxTree", () => {
1475514755
[
1475614756
'function u(){ g(); var z = "z1", w = "w2", v = "v3"; function k(){ return w } h(function(){ return z }, function(){ function inner(){ return v } return inner }); return k } function f(o){ function a(e){ return e + o } const b = function(){ return this }, c = () => this; return [function(x){ return a(x) }, function(){ return b() }, function(){ return c() }] }',
1475714757
{},
14758-
'function u(){g();h(function(){return"z1"},function(){return function(){return"v3"}});return function(){return"w2"}}function f(n){let r=()=>this;return[function(r){return function(r){return r+n}(r)},function(){return function(){return this}()},function(){return r()}]}'
14758+
'function u(){g();h(function(){return"z1"},function(){return function(){return"v3"}});return function(){return"w2"}}function f(n){let t=()=>this;return[function(t){return t+n},function(){return function(){return this}()},function(){return t()}]}'
1475914759
],
1476014760
// What a statement sets folds into the next where that reads it first,
1476114761
// but not past a compound assignment's read nor a name it may write.
@@ -15228,6 +15228,22 @@ describe("JavaScript minifier printing a SyntaxTree", () => {
1522815228
{},
1522915229
"function f(c){switch(c){case-1:c=1;break;case 0:c=2}switch(c){case 1:x()}switch(c){default:}switch(c){case 1:y()}c:switch(c){case 1:z();break c}switch(c){case 1:y();/*! c */break}return import(r(c))}"
1523015230
],
15231+
// A function called with nothing but names never written passed prints as what it returns.
15232+
[
15233+
"function f(g) { var h = function () { return g.a }; function k() { return g.b } return [h(), k(), (() => ({ a: 1 }))(), (function () { return g.c + 1 })() * 2] }",
15234+
{},
15235+
"function f(n){return[n.a,n.b,{a:1},(n.c+1)*2]}"
15236+
],
15237+
[
15238+
"function f(s, e) { (() => ({ a: 1 }))(); return [((x, y) => x + y)(s, e), ((x) => (x, 1))(s)] }",
15239+
{},
15240+
"function f(e,n){return[e+n,(e,1)]}"
15241+
],
15242+
[
15243+
"function f(a, b) { a = 2; return [((x) => x + 1)(a), ((x) => () => x)(b), (function () { return this })(), (function n() { return n })(), (async () => 1)(), ((x) => x)(b, b), (() => 1)?.()] }",
15244+
{},
15245+
"function f(n,t){n=2;return[(n=>n+1)(n),(n=>()=>n)(t),function(){return this}(),function n(){return n}(),(async()=>1)(),(n=>n)(t,t),(()=>1)?.()]}"
15246+
],
1523115247
[
1523215248
"function f(a){ if (0) x(); Math.PI }",
1523315249
{ compress: {toplevel: false,builtins_ecma: 2020},format: {comments: false} },

0 commit comments

Comments
 (0)