Skip to content

Commit b12ad4d

Browse files
perf(javascript): print without compressing as fast as before the one pass, and cover its last branches (#23037)
* test(javascript): cover the one pass's remaining branches, and keep comments from breaking folded exits A comment before an `if` test folded into `return t?a:b` now opens the test in parentheses, and one after a `new` printed as a call moves ahead of the `throw`, `return`, `yield` or `await` before it. Claude-Session: https://claude.ai/code/session_01Tgxgyb4N35ndP6cC4KcrXC * perf(javascript): read print-only tokens only for the comments it keeps The one pass read every node's tokens to place comments, even where a print keeps none or only those before the first token, as the deleted minify driver did not. Print-only lodash: 55.3 ms to 43.1 ms CPU. Claude-Session: https://claude.ai/code/session_01Tgxgyb4N35ndP6cC4KcrXC * refactor(javascript): type the comment filter's stream without `unknown` Claude-Session: https://claude.ai/code/session_01Tgxgyb4N35ndP6cC4KcrXC * chore: keep the comment-fold changeset to one short sentence Claude-Session: https://claude.ai/code/session_01Tgxgyb4N35ndP6cC4KcrXC
1 parent b895d3e commit b12ad4d

5 files changed

Lines changed: 421 additions & 17 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 a comment by a folded `if` test or dropped `new` from ending a `return`.

‎lib/javascript/syntax-printer.js‎

Lines changed: 90 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2145,6 +2145,38 @@ const installMinify = (modules) => {
21452145
}
21462146
};
21472147

2148+
/**
2149+
* Which comments a print keeps, where the node printer may test them
2150+
* before it prints: a callback or a `lastIndex` a test moves would see it.
2151+
* @param {DriverFormatOptions} format the format options, as given
2152+
* @returns {CommentFilter | false | undefined} the test, false for none, undefined where they are only tested as they print
2153+
*/
2154+
const commentFilterOf = (format) => {
2155+
const { comments } = format;
2156+
if (comments !== undefined && !comments && !format.preserve_annotations) {
2157+
return false;
2158+
}
2159+
/** @type {FormatSettings} */
2160+
let settings;
2161+
try {
2162+
settings = checkFormatOptions({ ...format });
2163+
} catch (_err) {
2164+
// A bad option throws once the source is read, as terser has it.
2165+
return undefined;
2166+
}
2167+
const kept = settings.comments;
2168+
if (
2169+
typeof kept === "function" ||
2170+
(kept instanceof RegExp && (kept.global || kept.sticky))
2171+
) {
2172+
return undefined;
2173+
}
2174+
return new modules.MinifiedOutput(
2175+
settings,
2176+
/** @type {NodeAccess} */ (/** @type {NodeAccess | null} */ (null))
2177+
).commentFilter;
2178+
};
2179+
21482180
modules.prepareNodePrint = (text, given) => {
21492181
const options = /** @type {DriverOptions} */ (
21502182
/** @type {Omit<DriverOptions, "format"> & { format: DriverFormatOptions | null }} */ (
@@ -2255,11 +2287,33 @@ const installMinify = (modules) => {
22552287
options.parse.toplevel = null;
22562288
delete options.parse.spidermonkey;
22572289
options.parse.filename = "0";
2258-
// Every token's type is read for the scopes, mangled or not.
2290+
// Where only comments before its first token print, that token.
2291+
let leadingToken = 0;
2292+
const keptBy =
2293+
options.mangle || options.compress
2294+
? undefined
2295+
: commentFilterOf(givenFormat);
2296+
// The scopes read every token's type; printing alone reads the tokens
2297+
// only for the comments it keeps, and only the first where it keeps
2298+
// those before it alone.
22592299
const parsed = parseModule.parseTree(
22602300
text,
22612301
options.parse,
2262-
modules.readTreeTokens
2302+
keptBy === undefined
2303+
? modules.readTreeTokens
2304+
: (tree, program, tokenAt, table) => {
2305+
const kept = keptBy === false ? 0 : table.keptComments(keptBy);
2306+
if (kept === 2) {
2307+
return modules.readTreeTokens(tree, program, tokenAt, table);
2308+
}
2309+
if (kept === 1) {
2310+
// Past every comment, the source's end is a token too.
2311+
leadingToken = /** @type {number} */ (
2312+
tokenAt(tree.start(program), false)
2313+
);
2314+
}
2315+
return null;
2316+
}
22632317
);
22642318
const compressOptions = /** @type {Partial<CompressOptions> | false} */ (
22652319
options.compress
@@ -2351,7 +2405,7 @@ const installMinify = (modules) => {
23512405
parsed,
23522406
text,
23532407
format,
2354-
leadingToken: 0,
2408+
leadingToken,
23552409
names,
23562410
compress: options.compress
23572411
? /** @type {Partial<CompressOptions>} */ (options.compress)
@@ -2760,7 +2814,7 @@ class SourceToken {
27602814
}
27612815
}
27622816

2763-
/** @typedef {{ base: number, token: (index: number) => SourceToken, commentsOf: (index: number, after: boolean) => Comment[], forEachCommentGap: (callback: (index: number, comments: Comment[]) => void) => void, commentFlags: () => Uint8Array, outerCounts: Map<number, number> }} TokenTable the tokens of one parse, read by their index past `base` */
2817+
/** @typedef {{ base: number, token: (index: number) => SourceToken, commentsOf: (index: number, after: boolean) => Comment[], forEachCommentGap: (callback: (index: number, comments: Comment[]) => void) => void, commentFlags: () => Uint8Array, keptComments: (test: (comment: Comment) => boolean) => number, outerCounts: Map<number, number> }} TokenTable the tokens of one parse, read by their index past `base` */
27642818

27652819
/**
27662820
* The tokens of the parse a tree was read from. Its nodes hold their tokens'
@@ -3392,6 +3446,22 @@ const installFusedParse = (modules) => {
33923446
for (let i = count; i < size; i++) flags[i] = flags[count - 1];
33933447
return flags;
33943448
},
3449+
// Read as the parse ends: 0 where it keeps none, 1 where only those
3450+
// before the first token, 2 where it keeps more.
3451+
keptComments: (/** @type {(comment: Comment) => boolean} */ test) => {
3452+
const gaps = /** @type {CommentGaps} */ (lists);
3453+
let kept = 0;
3454+
for (const gap of listed) {
3455+
const list = /** @type {Comment[]} */ (gaps[gap]);
3456+
for (let i = 0; i < list.length; i++) {
3457+
if (!test(list[i])) continue;
3458+
if (gap !== 0) return 2;
3459+
kept = 1;
3460+
break;
3461+
}
3462+
}
3463+
return kept;
3464+
},
33953465
outerCounts: new Map()
33963466
}
33973467
};
@@ -7649,6 +7719,11 @@ const createNodePrinter = (modules) => {
76497719
return childOf(Field.left, parent);
76507720
case NodeType.CallExpression:
76517721
return childOf(Field.callee, parent);
7722+
// A `new` printing as a call opens with its callee.
7723+
case NodeType.NewExpression:
7724+
return compresses && constructsWhenCalled(parent)
7725+
? childOf(Field.callee, parent)
7726+
: undefined;
76527727
case NodeType.ConditionalExpression:
76537728
return childOf(Field.test, parent);
76547729
case NodeType.MemberExpression:
@@ -12450,6 +12525,7 @@ const createNodePrinter = (modules) => {
1245012525
const test = childOf(Field.test, node);
1245112526
const branch = form === CONDITIONAL_FORM && branchForm() !== 0;
1245212527
if (form === CONDITIONAL_FORM) printHoistedHead(node);
12528+
let afterReturn = false;
1245312529
if (form === RETURN_FORM) {
1245412530
// One continuing the `?:` an `if` before it, or holding it as its
1245512531
// `else`, opens prints none.
@@ -12462,6 +12538,7 @@ const createNodePrinter = (modules) => {
1246212538
) {
1246312539
output.print("return");
1246412540
output.space();
12541+
afterReturn = true;
1246512542
}
1246612543
}
1246712544
const folded = form === RETURN_FORM ? foldedReturn(node) : 0;
@@ -12485,7 +12562,14 @@ const createNodePrinter = (modules) => {
1248512562
printNode(
1248612563
test,
1248712564
bindsLooser(test) ||
12488-
(form !== RETURN_FORM && !branch && opensAmbiguously(test))
12565+
(form !== RETURN_FORM && !branch && opensAmbiguously(test)) ||
12566+
// A comment before the test could break the line after `return`,
12567+
// as terser's `return` parenthesizes a value comments precede.
12568+
(afterReturn &&
12569+
hasComments(
12570+
/** @type {TreeTokens} */ (commentTokens).starts[test],
12571+
1
12572+
))
1248912573
);
1249012574
output.space();
1249112575
output.print("?");
@@ -14137,15 +14221,9 @@ const createNodePrinter = (modules) => {
1413714221
) {
1413814222
return undefined;
1413914223
}
14224+
// Its callers print no write target, which reads no value.
1414014225
const parent = parentAt(0);
1414114226
switch (typeOf(parent)) {
14142-
case NodeType.AssignmentExpression:
14143-
case NodeType.ForInStatement:
14144-
case NodeType.ForOfStatement:
14145-
if (childOf(Field.left, parent) === node) return undefined;
14146-
break;
14147-
case NodeType.UpdateExpression:
14148-
return undefined;
1414914227
case NodeType.UnaryExpression:
1415014228
if (tree.textOf(Part.operator, parent) === "delete") return undefined;
1415114229
break;

0 commit comments

Comments
 (0)