Skip to content

fix(minifier): keep a string method call when its result is longer - #29311

Open
ashokram-si wants to merge 5 commits into
oxc-project:mainfrom
ashokram-si:fix/minifier-replace-all-size-29269
Open

ashokram-si wants to merge 5 commits into
oxc-project:mainfrom
ashokram-si:fix/minifier-replace-all-size-29269

Conversation

@ashokram-si

@ashokram-si ashokram-si commented Nov 11, 2026 •

Copy link
Copy Markdown

Fixes #29269

A known string method call was replaced by its constant result whatever the size, so 'aaaaaaa'.replaceAll('a', 'aaaaaaa') became a 49-character literal, longer than the call.

Example

Input (playground for this example):

x = 'aaaaaaa'.replaceAll('a', 'aaaaaaa');

cargo run -p oxc_minifier --example minifier 29311.js. The playground runs the released version of Oxc, so it shows the output before this change.

Before output (main at d59aa8e):

x = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";

After output (this branch at 8dd635d):

x = "aaaaaaa".replaceAll("a", "aaaaaaa");

The call is kept: the 51-character literal it folded to is longer than the call.

replace_known_global_methods now keeps the call when the string result is longer than the call, and only where the result would be printed as it is: the right side of an assignment, an argument, a returned value, an array element, an object property's value, or the initializer of a binding that stays (the compressor's own rule for dropping an unused declarator decides). Anywhere else the folded literal lets the parent fold further, so the call folds as before: a comparison (C === 'b' is !1), !, typeof, == null, void, an if or ?: test, &&, a sequence, + (through try_fold_add), a member access ([0], .charAt(0), .length), a statement whose value is unused, and a binding the compressor drops.

Both sizes count string literals as they are printed, with their quotes and escapes (backslashes, line terminators, and the least common quote character); other operands use string_expression_size_lower_bound, the helper string + folding already uses. A computed key such as ['replaceAll'] is a static member by the time the call is visited, so it prints, and counts, as .replaceAll.

It still folds as before when an operand can't be sized, such as /abc/.toString() or (-1).toString().

Global function calls such as encodeURI('测试') are unchanged. Their size depends on the output charset: 19 vs 20 bytes as UTF-8, while ASCII-only output prints the characters as \u6d4b\u8bd5 and makes the literal longer. Happy to extend the rule to them if you want it general.

Tests: test_fold_string_replace_all_size covers kept calls, the equal-size boundary (folds) and one character over it (kept), escapes, chained member access, unsized operands, and the parents where the call folds as before (comparison, == null, !, typeof, void, an if or ?: test, &&, a sequence, an unused statement, +, a binding dropped as unused) or is kept (an argument, a return, a binding that stays). It fails on main. cargo test -p oxc_minifier passes, cargo minsize output is byte-identical, and cargo fmt --check and cargo clippy -p oxc_minifier --all-targets -- -D warnings are clean.

AI disclosure: written with Claude Code (model: Claude Opus 5.5, claude-opus-5-5; the reasoning effort level was not recorded); I reviewed the change and ran the tests.

…onger

Folding a known string method call replaced it with its constant
result whatever the size, so `'aaaaaaa'.replaceAll('a', 'aaaaaaa')`
became a 49-character literal. The call is now kept when the result
is longer than the call, estimated with
`string_expression_size_lower_bound`.

The call still folds when an operand can't be sized, as before, and
when it is the object of a member expression (`[0]`, `.charAt(0)`),
which folds only on a literal. Global function calls such as
`encodeURI` are unchanged.

Fixes oxc-project#29269
@ashokram-si
ashokram-si requested a review from Dunqing as a code owner November 11, 2026 18:25

@baltasarblanco baltasarblanco left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested this locally: the head (0092093) against the main it merges (8cc6101), minifying with CompressOptions::smallest(), mangling and no whitespace, and running the original and both outputs under Node 24.

test_fold_string_replace_all_size fails on 8cc6101 and passes on the head, and cargo test -p oxc_minifier passes. On 21,718 files (2,969 minifier test inputs, mostly the SWC minifier fixtures, and 18,749 npm package files), the output changes in 1 file, which gets 82 bytes smaller: propCode.trim() in a test file stays a call, so the output has one copy of the template text instead of two.

One pattern where the output gets larger: when the parent would fold or drop the folded string, the kept call now stays. The PR already makes an exception for member access ([0], .charAt(0), .length); these are other parents where it shows up. C is the issue's call, 'aaaaaaa'.replaceAll('a', 'aaaaaaa'), and the sizes are bytes of the minified statement:

input main this PR
x = C === 'b' 5 (x=!1;) 44
x = !C 5 (x=!1;) 39
x = typeof C 11 44
if (C) x = 1; else x = 2 4 (x=1;) 40 (`aaaaaaa`.replaceAll(`a`,`aaaaaaa`),x=1;)
C; x = 1 4 (x=1;) 40

It's the same with == null, void, a ?: or && test, a sequence and an unused const: 11 cases in all, each 33 to 39 bytes larger, and Node prints the same for the original and both outputs. The call stays even when its value is unused because a method call on a literal counts as having side effects (crates/oxc_ecmascript/src/side_effects/expressions.rs:624). None of these showed up in the files above, so they may not matter much in practice.

Smaller things:

  • The description says C + 'b' still folds through try_fold_add. On the head it keeps the call (x = 'aaaaaaa'.replaceAll('a', 'aaaaaaa') + 'b', 42 bytes vs 55 on main), I think for the same side-effect reason, since try_fold_add checks may_have_side_effects first.
  • The size estimate doesn't count escapes (the comment says so), and that can also keep a call that is longer than the literal: x = '\\\\\\\\\\\\\\'.replaceAll('\\', 'aaaaa') stays as a 44-byte call, where main printed a 40-byte literal.
  • I tried 8 mutations of the new check, and the tests catch 6. One that survives is dropping the + 2. A case where the literal is one byte longer than the call catches it: test_same("x = 'aaaa'.replaceAll('a', 'aaaaaaaa')") passes on the head and fails without the + 2. The other is removing the ComputedMemberExpression arm of method_call_size_lower_bound, which doesn't make the ['replaceAll'] test fail. My guess is that the key has already become .replaceAll by the time the call is visited.

The cases give the same table on GitHub's merge ref with a newer main (7053128).

Drafted with AI assistance (Claude Opus 5.5 and Claude Fable 5.2, by Anthropic). I re-ran the script that produces these results and checked the outputs myself.

…is printed

The call was kept wherever its folded string was longer, so a parent
that folds or drops the value lost that folding: `C === 'b'`, `!C`,
`typeof C`, an `if` test, an unused statement or binding all came out
larger than on main. The call is now kept only where the result is
printed as it is: an assignment's right side, an argument, a returned
value, an array element, an object property's value, or the initializer
of a binding that stays (the compressor's own rule for dropping an
unused declarator decides). Elsewhere it folds as before.

String literals are sized as printed, with quotes and escapes, on both
sides. The `ComputedMemberExpression` arm is dropped: the key is a
static member by the time the call is visited.
@ashokram-si

Copy link
Copy Markdown
Author

Thanks for the thorough test, the corpus run and the mutation checks.

You're right about the parents: keeping the call is only a win where its value is printed as it is. Pushed 8dd635d: the call is now kept only on the right side of an assignment, as an argument, a returned value, an array element, an object property's value, or the initializer of a binding that stays. Whether a binding stays is the compressor's own rule for dropping an unused declarator, so a binding it keeps (unused bindings kept, a script's top level, using) keeps the call and one it drops folds and can still be dropped. Everywhere else it folds as on main, so your 11 cases go back to main's output: x = C === 'b' and x = !C are x = !1, typeof C is 'string', the if and the unused statement leave x = 1, and C + 'b' folds through try_fold_add again, which makes the description's sentence about it true. I also added five parents that fold the value the same way: == null, void, a ?: test, && and a sequence.

The other points:

  • Escapes: string literals are now counted as printed, with quotes, backslashes, line terminators and the least common quote character, on both sides. Your '\\\\\\\\\\\\\\'.replaceAll('\\', 'aaaaa') folds to the 35-character literal again.
  • The + 2 boundary: added your case, x = 'aaaa'.replaceAll('a', 'aaaaaaaa') (one character over, kept), next to the equal-size one (folds).
  • The ComputedMemberExpression arm: as you guessed, the key is already .replaceAll when the call is visited, so the arm was dead; it's removed, and the ['replaceAll'] test still covers the computed form.

All of these are cases in test_fold_string_replace_all_size. cargo test -p oxc_minifier passes, cargo minsize output is unchanged, and fmt and clippy are clean. I haven't re-run your 21,718-file corpus.

Drafted with Claude Code (Claude Opus 5.5, claude-opus-5-5; reasoning effort level not recorded); I reviewed the change and ran the tests.

@baltasarblanco

Copy link
Copy Markdown
Contributor

Re-ran the comparison on 8dd635dff against the main it merges (8cc6101), with the same setup as before (CompressOptions::smallest(), mangling, no whitespace, Node 24). C is the issue's call again.

  • The 11 cases, C + 'b' and the 7-backslash case now print exactly what 8cc6101 prints, and the issue's case is still 16 bytes smaller.
  • On the 21,718 files, the output changes in 1 file, 82 bytes smaller: the same file and the same output as on 0092093. None gets larger.

These still get larger than on main:

input 8cc6101 8dd635dff
x = 'aaaaaaaa'.replaceAll('a', '\n\n\n') 29 35
x = Number(C) 6 (x=NaN;) 46
function f() { let s; s = C; return s.length; } x = f(); 29 79

The first one is new in this commit: the minified output prints \n raw inside a template literal (oxc-minify 0.153.0 does too), so it costs no escape. Counting \r, U+2028 and U+2029 but not \n changes only the 5 cases with line feeds among the 92 I ran: 3 go back to main's size and 2 get smaller than main. In the other two the call is an argument or an assigned value whose use folds on main; parseInt(C) and isNaN(C) grow the same way.

The tests still pass if NewExpressionArguments, ArrayExpressionElements or ObjectPropertyValue is removed from the list; test_same on x = new Foo(C), x = [C] and x = { a: C } catches each one.

Drafted with AI assistance (Claude Opus 5.5 and Claude Fable 5.2, by Anthropic); I re-ran the script that produces these results and checked the outputs myself.

…lds its value

Three cases still grew compared with main:
- an argument of a call the compressor folds: `Number(C)`, `parseInt(C)`,
  `isNaN(C)`, a `Math` or `String` method, or a method of a string literal;
- the value assigned to a local binding, which is propagated to its uses
  (`s = C; return s.length`);
- a result with line feeds, which a template literal prints as they are.

These now fold as on main. The folded literal is sized with a lower bound
(the cheapest quote, a template literal's line feeds free), and the call's own
literals with an upper bound (every character that may be escaped, at its
longest escape). So the call is kept only when the literal is surely longer.
An operand built with `+` isn't sized, since its literals would be counted
low, so such a call folds as on main.
@ashokram-si

Copy link
Copy Markdown
Author

Thanks for re-running it. All three cases came from the same gap: the call was kept wherever its value is passed on, even where the consumer would fold that value. Pushed be4e90d, merged with current main as c00e154:

  • \n: the folded literal is now sized by the quote that costs least. ' and " count the escaped line feeds; a backtick counts ` and ${ but prints a line feed as it is, as codegen does. x = 'aaaaaaaa'.replaceAll('a', '\n\n\n') folds again.

  • The call's own literals are now sized as an upper bound, the folded result as a lower bound. So the call is kept only when the result is surely longer. This fixes a case from a second look at that change: in x = '````````````\n\n\n\n\n\n\n\n\n\n'.replaceAll('`', 'yyy'), the receiver prints ten \n. The old estimate made the call look 10 characters shorter than it is, so it was kept although folding is 8 bytes smaller. It folds now. A call whose receiver or argument is built with + isn't sized any more, since its literals would be counted low. Such a call folds as on main.

  • Call arguments: the call is kept as an argument only when the compressor can't fold the callee with a literal argument. These now fold as on main:

    • the global functions that try_fold_global_functions or substitute_simple_function_call fold: Number(C), parseInt(C), isNaN(C), String(C), encodeURIComponent(C) and the rest;
    • a method of Math or String;
    • a method of a string literal ('abc'.indexOf(C) is -1 again).

    A local function named Number is any other call, and its argument is kept.

  • Assignments: an assignment to a local binding now folds as on main, so s = C; return s.length is return 49 again. An assignment to a global or a member is still kept.

  • Tests: test_same now covers new Foo(C), [C] and { a: C }, one for each of those parents in the list. There are cases for each item above.

cargo test -p oxc_minifier passes. cargo minsize output is unchanged, and fmt and clippy are clean. I haven't re-run your corpus or your 92-case script.

Drafted with Claude Code (Claude Opus 5.5, claude-opus-5-5; reasoning effort level not recorded); I reviewed the change and ran the tests.

@baltasarblanco

Copy link
Copy Markdown
Contributor

Re-ran both on c00e1545f against the main it merges (777d2d1b5), same setup as before.

  • All 92 cases print the same under Node and none is larger than on main. The three from my last comment print exactly main's output; the issue's case is still 16 bytes smaller.
  • On the 21,718 files, 1 output changes, 82 bytes smaller: the same file and output as on 8dd635dff.

Drafted with AI assistance (Claude Opus 5.5 and Claude Fable 5.2, by Anthropic); I re-ran the script that produces these results and checked the outputs myself.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

minifier: folding replaceAll can increase output size

2 participants