Repository navigation
Conversation
Merging this PR will not alter performance
Comparing Footnotes
|
Dunqing
left a comment
There was a problem hiding this comment.
Looks good. It seems we can implement this in substitute_single_use_symbol_in_statement, which could reduce some repeated checks, but I haven't verified it yet.
…use_function_declaration
…use_function_declaration
thats a good idea, i'm going to check it latter on |
|
Hey, I saw you're planning to look at moving this into What it does: A function declaration with exactly one reference moves into that reference as a function expression. The reference could be an argument, a callee, a property value, or the right side of an assignment as in this PR: function c() { function f() { g() } h('x', f) }
// => function c() { h('x', function() { g() }) }
function c() { function f() { g() } return function() { f() } }
// => function c() { return function() { g() } }Inside a closure it'd move only as a callee, where the function object never escapes, so creating it once per call can't be observed. It'd never move into a loop. A second, smaller change would turn a strict Size. minsize against this branch's head:
Iterations would stay the same, save for three.js and echarts.js (2 to 3). On my personal site's 26 scripts, oxc trails SWC by 1,132 B brotli, and this closes the gap by 495 B. SWC makes the same move through Speed. Local only: Traps I ran into, each caught by minsize's idempotency check or a test:
State. About 520 lines in two files, using How would you like me to go from here? I can definitely push the branch so you can read it, you can fold whatever's useful into this PR, or I can open it as a follow-up once this lands. Disclosure: I built the prototype and ran these measurements with Claude Code, and I've reviewed the code and numbers before posting. |
|
/monitor-oxc run |
Monitor OxcCommit:
|
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Something went wrong. Try again later by commenting “@codex review”. ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
@codex review |
|
/monitor-oxc run |
Minify Size
965 of 109,516 files changed.
Full report (Test Minify Size summary): top files and packages each way, and before/after diffs of the files that changed most. Output bytes of oxc's full minify pipeline (compress + mangle + whitespace) over monitor-oxc's |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 597df54817
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| ) { | ||
| if ctx.is_tree_shake_only() | ||
| || Self::is_script_root_scope(ctx) | ||
| || ctx.current_scope_flags().contains_direct_eval() |
There was a problem hiding this comment.
Check the declaration's binding scope for direct eval
In sloppy Script/CommonJS, an Annex B block function can be hoisted into the enclosing function scope, while DirectEval propagates upward rather than into child blocks. Consequently, function outer(){ { function f(){} globalThis.saved=f; } return eval('typeof f'); } passes this guard: the block has no direct eval, and the assignment is the only resolved reference to f. Inlining removes the implicit outer binding, changing outer() from returning "function" to "undefined". Check the declaration symbol's binding scope for direct eval as well before removing it.
Useful? React with 👍 / 👎.
| let symbol_id = id.symbol_id(); | ||
| // check if symbol: is referenced once, is implicitly observable, as function is dead, has redeclarations | ||
| if ctx.scoping().get_resolved_reference_ids(symbol_id) != [reference.reference_id()] | ||
| || ctx.state.symbols.is_implicitly_observable(symbol_id) |
There was a problem hiding this comment.
Seed Annex B observability when unused removal is disabled
With unused: Keep, including CompressOptions::safest(), symbol_liveness::register_function skips the registration path that marks Annex B aliases implicitly observable. In sloppy Script/CommonJS, { function f(){return 1} } { function f(){return 2} globalThis.saved=f; } console.log(f()); therefore allows the second declaration through this check: its block-local symbol has one resolved reference and no redeclarations. Inlining removes its implicit assignment to the outer f, so the program prints 1 instead of 2. Record Annex B observability independently of recursive-function removal, or explicitly exclude these declarations here.
Useful? React with 👍 / 👎.
|
@oddharsh i'm not going to have time for now to follow up on this, feel free to follow up on this change if you want |
|
Thanks @armano2, happy to pick it up. Here's the plan, so it stays reviewable:
On the monitor-oxc run from 2026-11-02: I went through the logs, and I don't think the red rows come from this change. The Test262 diff only removes 9 known failures and adds none, which looks like the branch's snapshot falling behind |
…28917) Carries on #28121 by @armano2, who [handed it off](#28121 (comment)). Their 8 commits are unchanged and still theirs. On top: one commit that widens where the declaration can come from, two Annex B fixes from review, and a snapshot refresh. The branch is rebased onto current `main`. Moves a single-use function declaration into the assignment that uses it: ```js function a() {} b.a = a; // => b.a = function a() {}; function a() { g() } function b() { h() } exports.a = a; exports.b = b; // => exports.a = function() { g() }, exports.b = function() { h() }; ``` **Where it applies.** The declaration can sit anywhere earlier in the same statement list, and the target can be any assignment in a sequence or a chain (`r.a = r.b = f`). The assignment holds the only reference, so no statement in between can observe the move; creating a function has no side effects, and a statement that exits early just means the function is never created. It skips `with`, compound operators (`+=`), declarations that come after the use, redeclared names, direct `eval` in the binding's scope, the script root scope, and the sloppy-mode Annex B cases below, each with a test. **Why the whole list (94c6291).** I first tried looking back only through the run of preceding declarations, which covers the common CommonJS export block. minsize's idempotency check failed on three.js: merging expression statements into a sequence and inlining a single-use `var` don't request another pass, so the move landed on the second minify instead of the first. Searching the whole list doesn't depend on what an earlier pass left in between. **Annex B (added in review).** In sloppy mode, evaluating a block-level function declaration also writes the enclosing var binding (Annex B.3.2.1), and semantic doesn't record that write as a reference. The move is now skipped when: - a sloppy block declaration would lose its write to an enclosing var binding (75756ea); - a same-named block function in an earlier statement overwrites the outer function before the assignment reads it, e.g. `function f() { return 1 } if (c) { function f() { return 2 } } exports.f = f` (bd25837); - a direct `eval` in the enclosing var scope can read a hoisted block function. The eval check now uses the binding's scope, not only the current block (bd25837). Neither fix changes any minsize or allocation number. **Size** (minsize, against `main` e4703b1): | file | minified | gzip | |---|--:|--:| | typescript.js | -6,500 | -4,429 | | lodash.js | -965 | -626 | | d3.js | -916 | +342 | | moment.js | -604 | -408 | | antd.js | -489 | -424 | | victory.js | -474 | -286 | | three.js | -246 | -237 | | react.development.js | -75 | -51 | | echarts.js | -59 | +33 | | bundle.min.js | -10 | +1 | | vue.js | -9 | +4 | | **total** | **-10,347** | **-6,081** | d3 gets worse under gzip. I think moving helper bodies into the export block at the end splits up code gzip was matching, but I haven't confirmed it. three.js goes from 2 to 3 iterations; the rest are unchanged. **Rebase onto `main`.** The minsize and allocation snapshots were regenerated after the rebase (6452484). One Annex B test input was adjusted there so `main`'s new unused-label removal doesn't rewrite it. **Tests.** `cargo test -p oxc_minifier` passes and clippy is clean. CI is green at 6452484, including Conformance, Minification Size and Allocations. Monitor Oxc last ran on an earlier head (45c8273; results and analysis are in the comments) and still needs a rerun on this one. The general version (any single reference, such as call arguments and `return f`) follows as a separate PR once this one settles. Disclosure: I wrote the extension, did the merge, and wrote this description with Claude Code, and I've reviewed all three. The Annex B fixes were written with AI assistance (Codex for 75756ea, Claude Code for bd25837) and checked by running original and minified output side by side. --------- Co-authored-by: armano <armano2@users.noreply.github.com> Co-authored-by: Aadharsh Pannirselvam <19518661+oddharsh@users.noreply.github.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: qing deng <dengqing0821@gmail.com> Co-authored-by: Claude <noreply@anthropic.com>
Move an immediately preceding single-use function declaration into an assignment expression.
function a() {} b.a = a;->b.a = function a() {};