Skip to content

Commit c6098e7

Browse files
fix(javascript): bound only the string methods an argument sizes
The size guard skipped any string method with a large number, so `"xy".charCodeAt(1e99)` stopped folding; it now covers `repeat`, `padStart` and `padEnd`, listed by the generator. Record the test262 sources whose folded calls gzip worse beside the text they repeat. Claude-Session: https://claude.ai/code/session_01ViD41i2KdwwGxrDk7yrjeD
1 parent 93cf40b commit c6098e7

5 files changed

Lines changed: 46 additions & 13 deletions

File tree

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2477,6 +2477,7 @@ const domProperties = () =>
24772477
* @property {Record<string, string[]>} statics the functions of each global object
24782478
* @property {Record<string, string[]>} methods the prototype methods of each constructor
24792479
* @property {string[]} unicodeDependent the string methods folded on ASCII only
2480+
* @property {string[]} sizedByArgument the string methods a count or length argument sizes
24802481
*/
24812482

24822483
// The built-ins the `improve` phase evaluates on literals, by owner, as
@@ -2554,7 +2555,8 @@ const foldedBuiltIns = () => ({
25542555
"trim",
25552556
"trimEnd",
25562557
"trimStart"
2557-
]
2558+
],
2559+
sizedByArgument: ["padEnd", "padStart", "repeat"]
25582560
});
25592561

25602562
module.exports.domProperties = domProperties;

‎lib/javascript/syntax-printer.js‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19337,6 +19337,7 @@ const installImprove = (modules) => {
1933719337
const foldedStringMethods = new Set(foldedTables.methods.String);
1933819338
const foldedArrayMethods = new Set(foldedTables.methods.Array);
1933919339
const unicodeDependentMethods = new Set(foldedTables.unicodeDependent);
19340+
const sizedByArgumentMethods = new Set(foldedTables.sizedByArgument);
1934019341
// Every name a folded call can end in, for the check every call meets.
1934119342
const foldedNames = new Set([
1934219343
...foldedTables.globals,
@@ -19382,15 +19383,16 @@ const installImprove = (modules) => {
1938219383
* The built-in a call names and the value it is called on, when both are
1938319384
* known: a method of a literal, or a function of an undeclared global.
1938419385
* @param {Node} call a call
19385-
* @returns {{ fn: (...args: EXPECTED_ANY[]) => unknown, receiver: unknown } | undefined} the built-in
19386+
* @returns {{ fn: (...args: EXPECTED_ANY[]) => unknown, receiver: unknown, sizedByArgument: boolean } | undefined} the built-in
1938619387
*/
1938719388
const builtInOf = (call) => {
1938819389
const callee = call.expression;
1938919390
if (callee instanceof A.AST_SymbolRef) {
1939019391
return isUndeclaredRef(callee) && foldedGlobals.has(callee.name)
1939119392
? {
1939219393
fn: /** @type {EXPECTED_ANY} */ (global)[callee.name],
19393-
receiver: undefined
19394+
receiver: undefined,
19395+
sizedByArgument: false
1939419396
}
1939519397
: undefined;
1939619398
}
@@ -19404,7 +19406,8 @@ const installImprove = (modules) => {
1940419406
return methods && methods.has(name)
1940519407
? {
1940619408
fn: /** @type {EXPECTED_ANY} */ (global)[object.name][name],
19407-
receiver: undefined
19409+
receiver: undefined,
19410+
sizedByArgument: false
1940819411
}
1940919412
: undefined;
1941019413
}
@@ -19416,7 +19419,8 @@ const installImprove = (modules) => {
1941619419
return foldedStringMethods.has(name)
1941719420
? {
1941819421
fn: /** @type {EXPECTED_ANY} */ (String.prototype)[name],
19419-
receiver: object.value
19422+
receiver: object.value,
19423+
sizedByArgument: sizedByArgumentMethods.has(name)
1942019424
}
1942119425
: undefined;
1942219426
}
@@ -19431,7 +19435,8 @@ const installImprove = (modules) => {
1943119435
}
1943219436
return {
1943319437
fn: /** @type {EXPECTED_ANY} */ (Array.prototype)[name],
19434-
receiver: elements
19438+
receiver: elements,
19439+
sizedByArgument: false
1943519440
};
1943619441
}
1943719442
return undefined;
@@ -19468,10 +19473,10 @@ const installImprove = (modules) => {
1946819473
for (const arg of call.args) {
1946919474
const value = constantOf(arg);
1947019475
if (value === NOT_CONSTANT) return call;
19471-
// A string method's count or length past the call's own size, as in
19472-
// `"a".repeat(1e9)`, answers nothing shorter, so it is never run.
19476+
// A count or length past the call's own size, as in `"a".repeat(1e9)`,
19477+
// answers nothing shorter, so the method is never run.
1947319478
if (
19474-
typeof builtIn.receiver === "string" &&
19479+
builtIn.sizedByArgument &&
1947519480
(typeof value === "number" || typeof value === "string") &&
1947619481
Number(value) > limit
1947719482
) {

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1033,7 +1033,19 @@ const IMPROVED_YET_BIGGER = {
10331033
"fixture/issues/12215/input.js (its own options)":
10341034
"20 bytes fewer, 5 more gzipped: the folded global call matched a shadowed one beside it",
10351035
"issue_8864_1 (its own options)":
1036-
"19 bytes fewer, 1 more gzipped: the folded `toUpperCase()` matched the one left in the template"
1036+
"19 bytes fewer, 1 more gzipped: the folded `toUpperCase()` matched the one left in the template",
1037+
"returns-undefined-on-position-less-than-zero.js (the default minimizer's options)":
1038+
"15 bytes fewer, 1 more gzipped: the folded `codePointAt(-1)` matched the call beside it",
1039+
"S15.1.3.3_A4_T2.js (the default minimizer's options)":
1040+
"51 bytes fewer, 19 more gzipped: each folded lower-cased URL no longer repeats the upper-cased one",
1041+
"S15.1.3.4_A4_T2.js (the default minimizer's options)":
1042+
"51 bytes fewer, 19 more gzipped: each folded lower-cased URL no longer repeats the upper-cased one",
1043+
"S15.1.2.3_A1_T6.js (the default minimizer's options)":
1044+
"52 bytes fewer, 2 more gzipped: each folded `parseFloat` call matched the one in its message",
1045+
"S15.1.2.2_A7.2_T3.js (the default minimizer's options)":
1046+
"323 bytes fewer, 2 more gzipped: each folded `parseInt` call matched the one in its message",
1047+
"parseInt-default-to-decimal.js (the default minimizer's options)":
1048+
"60 bytes fewer, 1 more gzipped: each folded `parseInt` call matched the one in the strict function"
10371049
};
10381050

10391051
/**

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,11 @@ const IMPROVED_CASES = [
233233
"console.log(Math.max(1, 5, 2), Math.abs(-3), Math.round(-0.4), Math.imul(3, 4));",
234234
{ compress: {}, mangle: false }
235235
],
236+
[
237+
"a string method given an index past the string",
238+
'console.log("xy".charCodeAt(1e99), "abcde".charAt(4294967295));',
239+
{ compress: {}, mangle: false }
240+
],
236241
[
237242
"global functions called on literals",
238243
'console.log(parseInt("ff", 16), encodeURIComponent("a b"), isNaN("x"));',

‎tooling/generate-js-data.js‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,6 +1329,10 @@ const UNICODE_DEPENDENT_METHODS = [
13291329
"trimStart"
13301330
];
13311331

1332+
// String methods whose count or length argument sets their result's size,
1333+
// which the printer refuses past the call's own size before running them.
1334+
const SIZED_BY_ARGUMENT_METHODS = ["padEnd", "padStart", "repeat"];
1335+
13321336
// Every other member @mdn/browser-compat-data lists of `Math`, `Number`,
13331337
// `String`, `Array` and the global functions, by why it is not folded; one it
13341338
// starts listing fails generation until it is placed here or above.
@@ -1539,7 +1543,7 @@ const isNoLaterThan = (version, limit) => {
15391543
/**
15401544
* The built-ins `improve` folds, checked against BCD: each one present, on the
15411545
* owner it is spelled under, and in every Node webpack builds on.
1542-
* @returns {{ globals: string[], statics: Record<string, string[]>, methods: Record<string, string[]>, unicodeDependent: string[] }} the tables
1546+
* @returns {{ globals: string[], statics: Record<string, string[]>, methods: Record<string, string[]>, unicodeDependent: string[], sizedByArgument: string[] }} the tables
15431547
*/
15441548
const collectFoldedBuiltIns = () => {
15451549
const members = collectBuiltInMembers();
@@ -1600,7 +1604,10 @@ const collectFoldedBuiltIns = () => {
16001604
)}`
16011605
);
16021606
}
1603-
for (const name of UNICODE_DEPENDENT_METHODS) {
1607+
for (const name of [
1608+
...UNICODE_DEPENDENT_METHODS,
1609+
...SIZED_BY_ARGUMENT_METHODS
1610+
]) {
16041611
if (!FOLDED_BUILT_INS["String.prototype"].includes(name)) {
16051612
throw new Error(`${name} is not a folded string method`);
16061613
}
@@ -1609,7 +1616,8 @@ const collectFoldedBuiltIns = () => {
16091616
globals,
16101617
statics,
16111618
methods,
1612-
unicodeDependent: UNICODE_DEPENDENT_METHODS
1619+
unicodeDependent: UNICODE_DEPENDENT_METHODS,
1620+
sizedByArgument: SIZED_BY_ARGUMENT_METHODS
16131621
};
16141622
};
16151623

@@ -1624,6 +1632,7 @@ const renderFoldedBuiltIns = () => `
16241632
* @property {Record<string, string[]>} statics the functions of each global object
16251633
* @property {Record<string, string[]>} methods the prototype methods of each constructor
16261634
* @property {string[]} unicodeDependent the string methods folded on ASCII only
1635+
* @property {string[]} sizedByArgument the string methods a count or length argument sizes
16271636
*/
16281637
16291638
// The built-ins the \`improve\` phase evaluates on literals, by owner, as

0 commit comments

Comments
 (0)