Skip to content

Commit 12b414a

Browse files
perf(javascript): push and pop only the stack in collapse_vars' scanners (#22554)
Every node `collapse_vars` scans went through the walker's general push and pop, which track directives and classes. Its scanner and its multi-read replacer read no directive, only the parents `parent` and `find_scope` look up, so they now push and pop nothing but the stack. Minifying lodash.js runs 0.71% fewer instructions under CodSpeed's simulation flags.
1 parent 6e6a464 commit 12b414a

1 file changed

Lines changed: 34 additions & 6 deletions

File tree

‎lib/javascript/syntax-printer.js‎

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25999,6 +25999,29 @@ const createTightenBody = (modules) => {
2599925999
/** @type {Node[]} */
2600026000
const EMPTY_PATH = [];
2600126001

26002+
/**
26003+
* Pushes a node a scan enters, tracking no directive.
26004+
* @this {Walker} the scan
26005+
* @param {Node} node the node
26006+
* @returns {void}
26007+
*/
26008+
function pushScanned(node) {
26009+
this.stack.push(node);
26010+
}
26011+
26012+
/**
26013+
* Pops the node a scan leaves.
26014+
* @this {Walker} the scan
26015+
* @returns {void}
26016+
*/
26017+
function popScanned() {
26018+
this.stack.pop();
26019+
}
26020+
26021+
// The scanners read no directive, only the parents `parent` and
26022+
// `find_scope` read, so they push and pop nothing but the stack.
26023+
const SCANNED_STACK = { push: pushScanned, pop: popScanned };
26024+
2600226025
/**
2600326026
* What terser's `collapse` keeps in function-scoped `var`s its closures share
2600426027
* across candidates, held once per nesting depth so a run allocates none of
@@ -26059,13 +26082,18 @@ const createTightenBody = (modules) => {
2605926082
/** @type {Node | null} */
2606026083
this.removing = null;
2606126084
this.found = false;
26062-
// The scanners read nothing of their directives, so none is reset.
26063-
this.scanner = createTransformer(
26064-
(/** @type {Node} */ node) => scanForCandidate(this, node),
26065-
(/** @type {Node} */ node) => leaveScanned(this, node)
26085+
this.scanner = Object.assign(
26086+
createTransformer(
26087+
(/** @type {Node} */ node) => scanForCandidate(this, node),
26088+
(/** @type {Node} */ node) => leaveScanned(this, node)
26089+
),
26090+
SCANNED_STACK
2606626091
);
26067-
this.multiReplacer = createTransformer((/** @type {Node} */ node) =>
26068-
replaceRead(this, node)
26092+
this.multiReplacer = Object.assign(
26093+
createTransformer((/** @type {Node} */ node) =>
26094+
replaceRead(this, node)
26095+
),
26096+
SCANNED_STACK
2606926097
);
2607026098
// A left value reads no directive, so its walk keeps only the parents
2607126099
// `isModified` reads, not the directives every other walk tracks.

0 commit comments

Comments
 (0)