Repository navigation
fix(minifier): keep a string method call when its result is longer - #29311
ashokram-si wants to merge 5 commits into
Conversation
…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
baltasarblanco
left a comment
There was a problem hiding this comment.
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 throughtry_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, sincetry_fold_addchecksmay_have_side_effectsfirst. - 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 theComputedMemberExpressionarm ofmethod_call_size_lower_bound, which doesn't make the['replaceAll']test fail. My guess is that the key has already become.replaceAllby 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.
|
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, The other points:
All of these are cases in Drafted with Claude Code (Claude Opus 5.5, |
|
Re-ran the comparison on
These still get larger than on main:
The first one is new in this commit: the minified output prints The tests still pass if 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.
|
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:
Drafted with Claude Code (Claude Opus 5.5, |
|
Re-ran both on
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. |
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):
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):
After output (this branch at 8dd635d):
The call is kept: the 51-character literal it folded to is longer than the call.
replace_known_global_methodsnow 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, anifor?:test,&&, a sequence,+(throughtry_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\u8bd5and makes the literal longer. Happy to extend the rule to them if you want it general.Tests:
test_fold_string_replace_all_sizecovers 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, anifor?: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_minifierpasses,cargo minsizeoutput is byte-identical, andcargo fmt --checkandcargo clippy -p oxc_minifier --all-targets -- -D warningsare 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.