Skip to content

Commit 8953192

Browse files
feat(javascript): give the names read most the one-character names in the minifier (#22573)
terser hands out names in the order it declares them, so in a scope with more locals than one-character names the helpers declared late get two characters however often they are read. The improve phase now counts each name's reads and hands each length's names to the busiest, keeping their order within a length, where that saves at least 200 bytes. Claude-Session: https://claude.ai/code/session_01ViD41i2KdwwGxrDk7yrjeD
1 parent ead32d3 commit 8953192

3 files changed

Lines changed: 182 additions & 0 deletions

File tree

‎lib/javascript/ScopeAnalyzer.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,6 +1333,8 @@ class MinifierVariable extends Variable {
13331333
/** @type {string | null} the name mangling gives it */
13341334
// eslint-disable-next-line camelcase -- terser's name, which the printer reads
13351335
this.mangled_name = null;
1336+
/** @type {number} how many names read or write it, which the mangler gives the short names by */
1337+
this.referenceCount = 0;
13361338
}
13371339
}
13381340

@@ -1946,6 +1948,7 @@ class MinifierScopeAnalyzer extends ScopeAnalyzer {
19461948
* @returns {void}
19471949
*/
19481950
_referenceVariable(identifier, from, variable) {
1951+
variable.referenceCount++;
19491952
if (variable.references !== NO_REFERENCES) {
19501953
variable.references.push(
19511954
new Reference(/** @type {Identifier} */ (identifier), from)

‎lib/javascript/syntax-printer.js‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,10 @@ const FORMAT_DEFAULTS = {
517517
// from the end never flattens more than this many characters.
518518
const COMMIT_AFTER = 8000;
519519

520+
// The bytes giving the busiest names the shortest must save before the
521+
// mangler does: below it, renaming costs gzip more than it saves.
522+
const SHORTER_NAMES_MIN_SAVING = 200;
523+
520524
const ANNOTATION = /[@#]__(PURE|INLINE|NOINLINE)__/;
521525

522526
// The characters `[$0-9A-Z_a-z]`, the ASCII part of what terser reads as an
@@ -38605,6 +38609,11 @@ const installEstreeMangle = (modules) => {
3860538609
return taken;
3860638610
};
3860738611

38612+
/** @type {MinifierVariable[]} */
38613+
const renamed = [];
38614+
// Where each name handed out stands in the order `identifiers` gives them.
38615+
/** @type {Map<string, number>} */
38616+
const nameIndexes = new Map();
3860838617
for (let i = 0; i < toMangle.length; i++) {
3860938618
const definition = toMangle[i];
3861038619
if (
@@ -38617,6 +38626,7 @@ const installEstreeMangle = (modules) => {
3861738626
if (redefinition) {
3861838627
definition.mangled_name =
3861938628
redefinition.mangled_name || redefinition.name;
38629+
renamed.push(definition);
3862038630
continue;
3862138631
}
3862238632
const owner = /** @type {MinifierScope} */ (definition.scope);
@@ -38647,6 +38657,105 @@ const installEstreeMangle = (modules) => {
3864738657
break;
3864838658
}
3864938659
definition.mangled_name = name;
38660+
renamed.push(definition);
38661+
nameIndexes.set(name, counting.cname);
38662+
}
38663+
const { improvements } = modules;
38664+
if (improvements !== undefined && improvements.enabled) {
38665+
shortenBusiestNames(toMangle, renamed, nameIndexes, analysis, [
38666+
unmangleableNames,
38667+
reserved
38668+
]);
38669+
}
38670+
};
38671+
38672+
/**
38673+
* Hands the names of the shortest length to the names referenced most,
38674+
* keeping each length's names in the order they were handed out.
38675+
* Done only where it saves enough bytes to outweigh what reordering costs gzip.
38676+
* @param {MinifierVariable[]} toMangle the definitions the mangler considered
38677+
* @param {MinifierVariable[]} named the definitions it named, each once
38678+
* @param {Map<string, number>} nameIndexes each name handed out, by its place in the order names are handed out
38679+
* @param {MinifierScopeAnalysis} analysis the scopes
38680+
* @param {Set<string>[]} keptNames names written as they are, which no name may become
38681+
* @returns {void}
38682+
*/
38683+
const shortenBusiestNames = (
38684+
toMangle,
38685+
named,
38686+
nameIndexes,
38687+
analysis,
38688+
keptNames
38689+
) => {
38690+
let longer = false;
38691+
for (const name of nameIndexes.keys()) {
38692+
if (name.length > 1) {
38693+
longer = true;
38694+
break;
38695+
}
38696+
}
38697+
// Every name one character long: there is no shorter name to hand out.
38698+
if (!longer) return;
38699+
/** @type {Map<string, number>} */
38700+
const references = new Map();
38701+
for (let i = 0; i < named.length; i++) {
38702+
const definition = named[i];
38703+
const name = /** @type {string} */ (definition.mangled_name);
38704+
references.set(
38705+
name,
38706+
(references.get(name) || 0) + definition.referenceCount
38707+
);
38708+
}
38709+
/** @type {Set<string>} */
38710+
const kept = new Set();
38711+
for (let i = 0; i < toMangle.length; i++) {
38712+
if (toMangle[i].mangled_name === null) kept.add(toMangle[i].name);
38713+
}
38714+
for (const name of analysis.globals.keys()) kept.add(name);
38715+
/** @type {string[]} */
38716+
const names = [];
38717+
for (const name of nameIndexes.keys()) {
38718+
if (!kept.has(name) && !keptNames.some((set) => set.has(name))) {
38719+
names.push(name);
38720+
}
38721+
}
38722+
/**
38723+
* @param {string} a a name
38724+
* @param {string} b another
38725+
* @returns {number} which is handed out first
38726+
*/
38727+
const byIndex = (a, b) =>
38728+
/** @type {number} */ (nameIndexes.get(a)) -
38729+
/** @type {number} */ (nameIndexes.get(b));
38730+
names.sort(byIndex);
38731+
const busiest = [...names].sort(
38732+
(a, b) =>
38733+
/** @type {number} */ (references.get(b)) -
38734+
/** @type {number} */ (references.get(a)) || byIndex(a, b)
38735+
);
38736+
/** @type {Map<string, string>} */
38737+
const renames = new Map();
38738+
let saved = 0;
38739+
for (let start = 0; start < names.length;) {
38740+
const { length } = names[start];
38741+
let end = start;
38742+
while (end < names.length && names[end].length === length) end++;
38743+
const chosen = busiest.slice(start, end).sort(byIndex);
38744+
for (let i = 0; i < chosen.length; i++) {
38745+
const name = chosen[i];
38746+
renames.set(name, names[start + i]);
38747+
saved +=
38748+
(name.length - length) * /** @type {number} */ (references.get(name));
38749+
}
38750+
start = end;
38751+
}
38752+
if (saved < SHORTER_NAMES_MIN_SAVING) return;
38753+
for (let i = 0; i < named.length; i++) {
38754+
const definition = named[i];
38755+
const shorter = renames.get(
38756+
/** @type {string} */ (definition.mangled_name)
38757+
);
38758+
if (shorter !== undefined) definition.mangled_name = shorter;
3865038759
}
3865138760
};
3865238761

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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2830,6 +2830,76 @@ describe("syntax-printer", () => {
28302830
expect(ourCache).toEqual(theirCache);
28312831
});
28322832

2833+
describe("the shortest names handed to the names read most", () => {
2834+
/**
2835+
* A function with more locals than one-character names, the last read often.
2836+
* @param {number} reads how often each of the last twenty is read
2837+
* @param {string=} globals what else the function logs
2838+
* @returns {string} the program
2839+
*/
2840+
const crowded = (reads, globals = "") => {
2841+
const names = Array.from({ length: 80 }, (_, i) => `variable${i}`);
2842+
const busy = names
2843+
.slice(60)
2844+
.map((name) => Array.from({ length: reads }, () => name).join("+"));
2845+
return `(function (input) {
2846+
${names.map((name, i) => `var ${name} = input + ${i};`).join(" ")}
2847+
console.log(${names.join("+")}, ${busy.join(", ")}${globals});
2848+
})(1);
2849+
(function (first, second) { console.log(first, second); })(2, 3);`;
2850+
};
2851+
/** @type {import("terser").MinifyOptions} */
2852+
const options = { compress: false, mangle: true };
2853+
2854+
it("should give the busiest names one character where that saves enough", async () => {
2855+
const { minify, improvements } = await load();
2856+
const reference = terserReference();
2857+
const source = crowded(15);
2858+
const { code } = await minify(source, options);
2859+
const theirs = await reference.minify(source, options);
2860+
expect(/** @type {string} */ (code).length).toBeLessThan(
2861+
/** @type {string} */ (theirs.code).length - 200
2862+
);
2863+
expect(runProgram(/** @type {string} */ (code))).toBe(
2864+
runProgram(source)
2865+
);
2866+
const phase = /** @type {{ enabled: boolean }} */ (improvements);
2867+
phase.enabled = false;
2868+
try {
2869+
expect((await minify(source, options)).code).toBe(theirs.code);
2870+
} finally {
2871+
phase.enabled = true;
2872+
}
2873+
});
2874+
2875+
it("should keep the names as handed out where that saves too little", async () => {
2876+
const { minify } = await load();
2877+
const source = crowded(5);
2878+
const { code } = await minify(source, options);
2879+
expect(code).toBe((await terserReference().minify(source, options)).code);
2880+
});
2881+
2882+
it("should never hand a local a name it reads as a global", async () => {
2883+
const { minify } = await load();
2884+
// Every one-character name is a global here, and handed out next door.
2885+
const letters = [
2886+
..."abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_"
2887+
];
2888+
const parameters = letters.slice(0, 20).map((_, i) => `parameter${i}`);
2889+
const source = `${crowded(15, letters.map((name) => `, typeof ${name}`).join(""))}
2890+
(function (${parameters.join(", ")}) { console.log(${parameters.join(", ")}); })();`;
2891+
const { code } = await minify(source, options);
2892+
expect(runProgram(/** @type {string} */ (code))).toBe(
2893+
runProgram(source)
2894+
);
2895+
// Declared at the top level, which is not mangled, they stay as written too.
2896+
const declared = `var ${letters.map((name) => `${name} = "${name}"`).join(", ")};\n${source}`;
2897+
expect(
2898+
runProgram(/** @type {string} */ ((await minify(declared, options)).code))
2899+
).toBe(runProgram(declared));
2900+
});
2901+
});
2902+
28332903
it("should minify a source too large to keep its buffers as terser does", async () => {
28342904
const { minify } = await load();
28352905
const reference = terserReference();

0 commit comments

Comments
 (0)