Skip to content

Commit 68812db

Browse files
feat(javascript): write Number's safe-integer bounds and epsilon as powers of two in the minifier (#22522)
* feat(javascript): write `Number`'s safe-integer bounds and epsilon as powers of two in the minifier From ECMAScript 2016, `Number.MAX_SAFE_INTEGER` is written `2**53-1`, `Number.MIN_SAFE_INTEGER` `1-2**53` and `Number.EPSILON` `2**-52`. Claude-Session: https://claude.ai/code/session_01ViD41i2KdwwGxrDk7yrjeD * test(javascript): list jQuery Mobile under its own options as improved yet bigger Claude-Session: https://claude.ai/code/session_01ViD41i2KdwwGxrDk7yrjeD
1 parent 9d7ccd2 commit 68812db

5 files changed

Lines changed: 80 additions & 15 deletions

File tree

‎lib/javascript/syntax-printer-data.js‎

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/javascript/syntax-printer.js‎

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17863,9 +17863,40 @@ const installImprove = (modules) => {
1786317863
for (const owner of Object.keys(foldedTables.constants)) {
1786417864
foldedConstants.set(owner, new Set(foldedTables.constants[owner]));
1786517865
}
17866+
/** @type {Map<string, Set<string>>} */
17867+
const powerOfTwoConstants = new Map();
17868+
for (const owner of Object.keys(foldedTables.powerOfTwoConstants)) {
17869+
powerOfTwoConstants.set(
17870+
owner,
17871+
new Set(foldedTables.powerOfTwoConstants[owner])
17872+
);
17873+
}
1786617874
/** @type {(node: Node, parent: Node) => boolean} */
1786717875
const isLhs = modules.inference.is_lhs;
1786817876

17877+
/**
17878+
* A power of two, or one off it, written so from ECMAScript 2016:
17879+
* `Number.MAX_SAFE_INTEGER` as `2**53-1`, `Number.MIN_SAFE_INTEGER` as `1-2**53`.
17880+
* @param {number} value `2**k`, `2**k-1` or `1-2**k`, as the generator checks
17881+
* @param {Node} orig where its position comes from
17882+
* @returns {Node} the expression
17883+
*/
17884+
const powerOfTwoOf = (value, orig) => {
17885+
const exponent = Math.round(Math.log2(Math.abs(value)));
17886+
const power = makeNode(A.BinaryNode, orig, {
17887+
operator: "**",
17888+
left: makeNodeFromConstant(2, orig),
17889+
right: makeNodeFromConstant(exponent, orig)
17890+
});
17891+
if (2 ** exponent === value) return power;
17892+
const one = makeNodeFromConstant(1, orig);
17893+
return makeNode(A.BinaryNode, orig, {
17894+
operator: "-",
17895+
left: value > 0 ? power : one,
17896+
right: value > 0 ? one : power
17897+
});
17898+
};
17899+
1786917900
A.wrapOptimizer("Dot", (optimizeDot) => (self, compressor) => {
1787017901
const optimized = optimizeDot(self, compressor);
1787117902
if (
@@ -17877,21 +17908,27 @@ const installImprove = (modules) => {
1787717908
) {
1787817909
return optimized;
1787917910
}
17880-
const names = foldedConstants.get(optimized.object.name);
17911+
const owner = optimized.object.name;
1788117912
const name = optimized.property.name;
17913+
const names = foldedConstants.get(owner);
17914+
const powers =
17915+
compressor.option("ecma") >= 2016
17916+
? powerOfTwoConstants.get(owner)
17917+
: undefined;
17918+
const asPower = powers !== undefined && powers.has(name);
1788217919
if (
17883-
!names ||
17884-
!names.has(name) ||
17920+
!(asPower || (names !== undefined && names.has(name))) ||
1788517921
!isUndeclaredRef(optimized.object) ||
1788617922
isLhs(self, compressor.parent()) ||
1788717923
globalMayBeRebound(compressor)
1788817924
) {
1788917925
return optimized;
1789017926
}
17891-
const folded = makeNodeFromConstant(
17892-
/** @type {EXPECTED_ANY} */ (global)[optimized.object.name][name],
17893-
optimized
17894-
);
17927+
/** @type {number} */
17928+
const value = /** @type {EXPECTED_ANY} */ (global)[owner][name];
17929+
const folded = asPower
17930+
? powerOfTwoOf(value, optimized)
17931+
: makeNodeFromConstant(value, optimized);
1789517932
return modules.printToString(folded).length <
1789617933
modules.printToString(optimized).length
1789717934
? folded

‎test/specCases/minify-corpora.spectest.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,6 +1282,8 @@ const rivalSmallerSeen = new Set();
12821282
const IMPROVED_YET_BIGGER = {
12831283
"arrow/object_parens (its own options)":
12841284
"3 bytes fewer, 1 more gzipped: `+!!{}` breaks the `()=>(` runs of the arrows around it gzip reused",
1285+
"projects/files/jquery.mobile-1.4.2.js (its own options)":
1286+
"50 bytes fewer, 1 more gzipped: `if(null!=r)return r` in place of `return null==r?void 0:r` loses the `return null==` gzip reused",
12851287
"projects/files/jquery.mobile-1.4.2.js (the default minimizer's options)":
12861288
"30 bytes fewer, 2 more gzipped: `this.p=c?x:y` breaks the `c?this.p=x:this.p=y` runs gzip reused",
12871289
"fixture/next/regression-1/framework-798bab57daac3897/input.js (its own options)":

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,11 @@ const IMPROVED_CASES = [
439439
"function f(a, b) { return [Math.pow(a, 3), Math.pow(2, b), Math.pow(-a, 2), Math.pow(a + b, 0.5), Math.pow(-2, b)]; } console.log(f(2, 3).join());",
440440
{ compress: { ecma: 2016 }, ecma: 2016, mangle: false }
441441
],
442+
[
443+
"`Number`'s safe-integer bounds and epsilon, as powers of two from ECMAScript 2016",
444+
"console.log(Number.MAX_SAFE_INTEGER, Number.MIN_SAFE_INTEGER, Number.EPSILON, -Number.EPSILON, 1 / Number.EPSILON, Number.MAX_SAFE_INTEGER.toString(16));",
445+
{ compress: { ecma: 2016 }, ecma: 2016, mangle: false }
446+
],
442447
[
443448
"an arrow naming `yield` in an async function, which reserves only `await`",
444449
"var yield; async function f(n) { while (n--) { (() => { yield = 1; })(); } } f(2); console.log(yield);",
@@ -5910,6 +5915,10 @@ describe("syntax-printer", () => {
59105915
["function f(a, b) { return Math.pow(a, 3); } console.log(f(2));", target(2015)],
59115916
["function f(a, b) { return [Math.pow(a, b), Math.pow(2, 3)]; } console.log(f(2, 3));", target(2016)],
59125917
["function f(Math, a) { return Math.pow(a, 3); } console.log(f({ pow: (a, b) => a + b }, 2));", target(2016)],
5918+
["console.log(Number.MAX_SAFE_INTEGER, Number.MIN_SAFE_INTEGER, Number.EPSILON);", target(2015)],
5919+
["function f(Number) { return Number.MAX_SAFE_INTEGER; } console.log(f({ MAX_SAFE_INTEGER: 1 }));", target(2016)],
5920+
["function f(o) { with (o) return Number.EPSILON; } console.log(f({ Number: { EPSILON: 1 } }));", target(2016)],
5921+
["console.log(delete Number.MAX_SAFE_INTEGER, Number.MIN_SAFE_INTEGER++, Number?.EPSILON);", target(2020)],
59135922
["function f(c) { return [null == c.d ? void 0 : c.d.e]; } console.log(f({}), f({ d: { e: 1 } }));", target(2021)],
59145923
["function f(c) { return [null == c ? void 0 : g(c)]; } function g(c) { return c; } console.log(f(null), f(1), g(2));", target(2021)],
59155924
["function f(c) { return [null == c ? void 0 : (c || g).a]; } function g() {} console.log(f(null), f({ a: 1 }));", target(2021)],

‎tooling/generate-js-data.js‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,13 @@ const FOLDED_BUILT_INS = {
807807

808808
// Static values `improve` writes as the number they hold where that is shorter.
809809
const FOLDED_CONSTANTS = {
810-
Number: ["EPSILON", "NEGATIVE_INFINITY", "NaN", "POSITIVE_INFINITY"]
810+
Number: ["NEGATIVE_INFINITY", "NaN", "POSITIVE_INFINITY"]
811+
};
812+
813+
// Static values `improve` writes as a power of two, as `2**53-1`, from
814+
// ECMAScript 2016: their digits are longer, or gzip worse, than their name.
815+
const POWER_OF_TWO_CONSTANTS = {
816+
Number: ["EPSILON", "MAX_SAFE_INTEGER", "MIN_SAFE_INTEGER"]
811817
};
812818

813819
// Methods whose answer for a string outside ASCII follows the engine's Unicode
@@ -865,10 +871,6 @@ const UNFOLDED_BUILT_INS = {
865871
"Array.@@species",
866872
"Array.prototype.@@unscopables"
867873
],
868-
"its sixteen digits gzip worse than the name they replace": [
869-
"Number.MAX_SAFE_INTEGER",
870-
"Number.MIN_SAFE_INTEGER"
871-
],
872874
"it runs code, or answers differently on each call": ["eval", "Math.random"],
873875
"the spec lets each engine approximate its answer": [
874876
"Math.acos",
@@ -1041,7 +1043,7 @@ const isNoLaterThan = (version, limit) => {
10411043
/**
10421044
* The built-ins `improve` folds, checked against BCD: each one present, on the
10431045
* owner it is spelled under, and in every Node webpack builds on.
1044-
* @returns {{ globals: string[], statics: Record<string, string[]>, methods: Record<string, string[]>, constants: Record<string, string[]>, unicodeDependent: string[], sizedByArgument: string[], constructedWhenCalled: string[] }} the tables
1046+
* @returns {{ globals: string[], statics: Record<string, string[]>, methods: Record<string, string[]>, constants: Record<string, string[]>, powerOfTwoConstants: Record<string, string[]>, unicodeDependent: string[], sizedByArgument: string[], constructedWhenCalled: string[] }} the tables
10451047
*/
10461048
const collectFoldedBuiltIns = () => {
10471049
const members = collectBuiltInMembers();
@@ -1094,6 +1096,17 @@ const collectFoldedBuiltIns = () => {
10941096
for (const [owner, names] of Object.entries(FOLDED_CONSTANTS)) {
10951097
for (const name of names) place(`${owner}.${name}`, "FOLDED_CONSTANTS");
10961098
}
1099+
for (const [owner, names] of Object.entries(POWER_OF_TWO_CONSTANTS)) {
1100+
for (const name of names) {
1101+
place(`${owner}.${name}`, "POWER_OF_TWO_CONSTANTS");
1102+
// The printer writes 2**k, 2**k-1 or 1-2**k, k its magnitude's log rounded.
1103+
const value = /** @type {EXPECTED_ANY} */ (global)[owner][name];
1104+
const power = 2 ** Math.round(Math.log2(Math.abs(value)));
1105+
if (![power, power - 1, 1 - power].includes(value)) {
1106+
throw new Error(`${owner}.${name} is no power of two, nor one off it`);
1107+
}
1108+
}
1109+
}
10971110
for (const [reason, names] of Object.entries(UNFOLDED_BUILT_INS)) {
10981111
for (const name of names) place(name, `left out as "${reason}"`);
10991112
}
@@ -1123,6 +1136,7 @@ const collectFoldedBuiltIns = () => {
11231136
statics,
11241137
methods,
11251138
constants: FOLDED_CONSTANTS,
1139+
powerOfTwoConstants: POWER_OF_TWO_CONSTANTS,
11261140
unicodeDependent: UNICODE_DEPENDENT_METHODS,
11271141
sizedByArgument: SIZED_BY_ARGUMENT_METHODS,
11281142
constructedWhenCalled: CONSTRUCTED_WHEN_CALLED
@@ -1140,6 +1154,7 @@ const renderFoldedBuiltIns = () => `
11401154
* @property {Record<string, string[]>} statics the functions of each global object
11411155
* @property {Record<string, string[]>} methods the prototype methods of each constructor
11421156
* @property {Record<string, string[]>} constants the static values of each global object
1157+
* @property {Record<string, string[]>} powerOfTwoConstants the static values of each global object written as a power of two
11431158
* @property {string[]} unicodeDependent the string methods folded on ASCII only
11441159
* @property {string[]} sizedByArgument the string methods a count or length argument sizes
11451160
* @property {string[]} constructedWhenCalled the constructors that construct the same without \`new\`

0 commit comments

Comments
 (0)