Skip to content

Commit 014480e

Browse files
feat(minify): also try names in oxc's letter order, where gzip writes it smaller
In a small program, re-run with mangled names taken in English letter order instead of by counted characters, and keep whichever gzips smaller. Claude-Session: https://claude.ai/code/session_01ViD41i2KdwwGxrDk7yrjeD
1 parent 56189de commit 014480e

3 files changed

Lines changed: 68 additions & 7 deletions

File tree

‎lib/javascript/syntax-printer.js‎

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const {
5555

5656
/* eslint-disable camelcase, new-cap -- terser's own API, which this speaks */
5757

58-
// cspell:ignore Mihai, Bazon, DEFNODE, endpos, endcol, sequencesize, lvalues, binop, loopcontrol, symbolref, funs, jridgewell, DEFMETHOD, Defun, defun, defuns, fnames, funarg, classnames, mangleable, unmangleable, NOINLINE, privatename, argnames, thedef, argname, bcatch, bfinally, MANGLEPROP, nlb, punc, Funarg, propmangle, preferrably, nondeferred, fargs, domprops, noin, Noin, DEFPRINT, defprint, dgimsuyv, nokeyword
58+
// cspell:ignore Mihai, Bazon, DEFNODE, endpos, endcol, sequencesize, lvalues, binop, loopcontrol, symbolref, funs, jridgewell, DEFMETHOD, Defun, defun, defuns, fnames, funarg, classnames, mangleable, unmangleable, NOINLINE, privatename, argnames, thedef, argname, bcatch, bfinally, MANGLEPROP, nlb, punc, Funarg, propmangle, preferrably, nondeferred, fargs, domprops, noin, Noin, DEFPRINT, defprint, dgimsuyv, nokeyword, etnriaoslucdfmhpgvybkwjxqz, ETNRIAOSLUCDFMHPGVYBKWJXQZ
5959

6060
/** @typedef {{ ast: Ast, [key: string]: EXPECTED_ANY }} MinifierModules terser's own modules */
6161
/**
@@ -307,7 +307,7 @@ const {
307307

308308
/** @typedef {(sources: MinifySources, options?: MinifyOptions) => Promise<MinifyOutput>} Minify what one call to the minifier takes and answers */
309309

310-
/** @typedef {{ minify: Minify, phases: string[], corrections?: { enabled: boolean }, improvements?: { enabled: boolean, lets: { enabled: boolean, applied: boolean }, literalsLast: { enabled: boolean, applied: boolean }, choosesBySize: { enabled: boolean } }, modules: MinifierModules }} Minifier what a caller minifies with, the switches of its corrections and improvements, and the modules it is built from */
310+
/** @typedef {{ minify: Minify, phases: string[], corrections?: { enabled: boolean }, improvements?: { enabled: boolean, lets: { enabled: boolean, applied: boolean }, literalsLast: { enabled: boolean, applied: boolean }, namesByCount: { enabled: boolean, applied: boolean }, choosesBySize: { enabled: boolean } }, modules: MinifierModules }} Minifier what a caller minifies with, the switches of its corrections and improvements, and the modules it is built from */
311311

312312
/**
313313
* One step building webpack's minifier, installing what it implements onto
@@ -17023,12 +17023,16 @@ const installFusedParse = (modules) => {
1702317023
const IDENTIFIER_LEADING =
1702417024
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_";
1702517025
const IDENTIFIER_DIGITS = "0123456789";
17026+
// The same letters by how often English text, and so code, uses them: oxc's
17027+
// order, whose names gzip smaller than counted ones in some small programs.
17028+
const IDENTIFIER_LEADING_BY_USE =
17029+
"etnriaoslucdfmhpgvybkwjxqzETNRIAOSLUCDFMHPGVYBKWJXQZ$_";
1702617030

1702717031
/**
1702817032
* webpack's character counter for mangled names. terser's keys a `Map` by
1702917033
* every character it is shown; this counts the ones a name can hold.
1703017034
* @param {(items: string[], compare: (a: string, b: string) => number) => string[]} mergeSort terser's stable sort
17031-
* @returns {{ reset: () => void, consider: (str: string, delta: number) => void, sort: () => void, get: (num: number) => string }} the counter
17035+
* @returns {{ reset: () => void, consider: (str: string, delta: number) => void, sort: () => void, get: (num: number) => string, ordered: { enabled: boolean, applied: boolean } }} the counter, and the switch of ordering its names by the counts
1703217036
*/
1703317037
const createFrequency = (mergeSort) => {
1703417038
const leading = [...IDENTIFIER_LEADING];
@@ -17058,6 +17062,11 @@ const createFrequency = (mergeSort) => {
1705817062
}
1705917063
},
1706017064
sort() {
17065+
if (!frequency.ordered.enabled) {
17066+
chars = [...IDENTIFIER_LEADING_BY_USE, ...digits];
17067+
return;
17068+
}
17069+
frequency.ordered.applied = true;
1706117070
chars = [...mergeSort(leading, compare), ...mergeSort(digits, compare)];
1706217071
},
1706317072
/**
@@ -17075,7 +17084,8 @@ const createFrequency = (mergeSort) => {
1707517084
base = 64;
1707617085
} while (num > 0);
1707717086
return name;
17078-
}
17087+
},
17088+
ordered: { enabled: true, applied: false }
1707917089
};
1708017090
frequency.reset();
1708117091
frequency.sort();
@@ -20220,7 +20230,7 @@ const unescapeWhitespace = (literal, tabs, lineBreaks) => {
2022020230
const LINE_BREAK_ESCAPES = 1000;
2022120231
const LINE_BREAK_SPACING = 500;
2022220232

20223-
/** @typedef {{ enabled: boolean, lets: { enabled: boolean, applied: boolean }, literalsLast: { enabled: boolean, applied: boolean }, choosesBySize: { enabled: boolean } }} Improvements the switch the improvements read, one for writing `const` as `let` alone, one for writing a literal after what an equality compares it with, and one for trying a small program without each */
20233+
/** @typedef {{ enabled: boolean, lets: { enabled: boolean, applied: boolean }, literalsLast: { enabled: boolean, applied: boolean }, namesByCount: { enabled: boolean, applied: boolean }, choosesBySize: { enabled: boolean } }} Improvements the switch the improvements read, one for writing `const` as `let` alone, one for writing a literal after what an equality compares it with, one for naming by counted characters, and one for trying a small program without each */
2022420234

2022520235
// Up to this many bytes, a program is minified again with each spelling the
2022620236
// improvements choose by measure switched off, which costs little there.
@@ -20295,6 +20305,7 @@ const installSizeChoice = (modules, improvements) => {
2029520305
modules.minify = async (files, options, fsModule) => {
2029620306
improvements.literalsLast.applied = false;
2029720307
improvements.lets.applied = false;
20308+
improvements.namesByCount.applied = false;
2029820309
// The caller's own options for the run it reads back, as a name cache;
2029920310
// a copy taken before it, which that run may fill in, for the others.
2030020311
const pristine = options && copyOptions(options);
@@ -20315,7 +20326,11 @@ const installSizeChoice = (modules, improvements) => {
2031520326
/** @type {{ enabled: boolean }[]} */
2031620327
const off = [];
2031720328
try {
20318-
for (const spelling of [improvements.literalsLast, improvements.lets]) {
20329+
for (const spelling of [
20330+
improvements.literalsLast,
20331+
improvements.lets,
20332+
improvements.namesByCount
20333+
]) {
2031920334
// A spelling the program never took gives the same output without.
2032020335
if (!spelling.enabled || !spelling.applied) continue;
2032120336
spelling.enabled = false;
@@ -20362,6 +20377,7 @@ const installImprove = (modules) => {
2036220377
enabled: true,
2036320378
lets: { enabled: true, applied: false },
2036420379
literalsLast: { enabled: true, applied: false },
20380+
namesByCount: modules.scope.base54.ordered,
2036520381
choosesBySize: { enabled: true }
2036620382
};
2036720383
modules.improvements = improvements;

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

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9061,7 +9061,8 @@ describe("syntax-printer", () => {
90619061
const input =
90629062
"function f(a, p) { a === undefined || a === null || p; a === undefined || a !== null || p; a !== undefined || a === null || p; a !== undefined || a !== null || p; a === undefined && a === null || p; a === undefined && a !== null || p; a !== undefined && a === null || p; a !== undefined && a !== null || p; } f(); const c = 1; console.log(c);";
90639063
/** @type {import("terser").MinifyOptions} */
9064-
const options = { compress: { passes: 2 }, mangle: true };
9064+
// Unmangled, so the names stay out of the choice this measures.
9065+
const options = { compress: { passes: 2 }, mangle: false };
90659066
/**
90669067
* @param {boolean} literalsLast whether the literal is written last
90679068
* @param {boolean} lets whether `const` is written as `let`
@@ -9100,6 +9101,49 @@ describe("syntax-printer", () => {
91009101
expect(runProgram(chosen)).toBe(runProgram(input));
91019102
});
91029103

9104+
it("should keep, of a small program's names ordered by counted characters and by their use in English, the one gzip writes smaller", async () => {
9105+
const { minify, improvements } = await load();
9106+
if (!improvements) throw new Error("the improve phase is not installed");
9107+
const zlib = require("zlib");
9108+
/**
9109+
* @param {string} code a program
9110+
* @returns {number} its gzipped size
9111+
*/
9112+
const gzipSize = (code) => zlib.gzipSync(code, { level: 9 }).length;
9113+
const input =
9114+
"function f1(x, y) { return [x + y]; } function f2(x, y) { var z = x + y; return [x, side_effect(), z]; } function f3(x, y) { return [[3], [f(x + y), x, y], [g()]]; }";
9115+
/**
9116+
* @param {boolean} counted whether names are ordered by counted characters
9117+
* @returns {Promise<string>} the program minified so, in one run
9118+
*/
9119+
const once = async (counted) => {
9120+
if (!improvements) throw new Error("the improve phase is not installed");
9121+
improvements.namesByCount.enabled = counted;
9122+
try {
9123+
return /** @type {string} */ ((await minify(input)).code);
9124+
} finally {
9125+
improvements.namesByCount.enabled = true;
9126+
}
9127+
};
9128+
const counted = await once(true);
9129+
const byUse = await once(false);
9130+
expect(counted).toBe(
9131+
"function f1(f,n){return[f+n]}function f2(f,n){var r=f+n;return[f,side_effect(),r]}function f3(n,r){return[[3],[f(n+r),n,r],[g()]]}"
9132+
);
9133+
expect(byUse).toBe(
9134+
"function f1(e,t){return[e+t]}function f2(e,t){var n=e+t;return[e,side_effect(),n]}function f3(e,t){return[[3],[f(e+t),e,t],[g()]]}"
9135+
);
9136+
improvements.choosesBySize.enabled = true;
9137+
let chosen;
9138+
try {
9139+
chosen = /** @type {string} */ ((await minify(input)).code);
9140+
} finally {
9141+
improvements.choosesBySize.enabled = false;
9142+
}
9143+
expect(chosen).toBe(gzipSize(byUse) < gzipSize(counted) ? byUse : counted);
9144+
expect(gzipSize(byUse)).toBeLessThan(gzipSize(counted));
9145+
});
9146+
91039147
it("should name a module's imports and exports by the names they spell, where that writes less", async () => {
91049148
const { minify, improvements } = await load();
91059149
/** @type {[string, string][]} */

‎types.d.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20524,6 +20524,7 @@ declare interface Minifier {
2052420524
enabled: boolean;
2052520525
lets: { enabled: boolean; applied: boolean };
2052620526
literalsLast: { enabled: boolean; applied: boolean };
20527+
namesByCount: { enabled: boolean; applied: boolean };
2052720528
choosesBySize: { enabled: boolean };
2052820529
};
2052920530
modules: MinifierModules;

0 commit comments

Comments
 (0)