Skip to content

feat(minifier): inline single use function declaration to assignment - #28917

Merged
Dunqing merged 14 commits into
oxc-project:mainfrom
oddharsh:feat/inline-single-use-fn-assignment
Nov 16, 2026
Merged

Dunqing merged 14 commits into
oxc-project:mainfrom
oddharsh:feat/inline-single-use-fn-assignment

Conversation

@oddharsh

@oddharsh oddharsh commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Carries on #28121 by @armano2, who handed it off. 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:

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.

@oddharsh
oddharsh requested a review from Dunqing as a code owner November 6, 2026 15:00
@github-actions github-actions Bot added the A-minifier Area - Minification, compression and name mangling, oxc_minifier / oxc_mangler label Nov 6, 2026
@codspeed

codspeed Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 81 untouched benchmarks
⏩ 29 skipped benchmarks1


Comparing oddharsh:feat/inline-single-use-fn-assignment (30ba68a) with main (d742f8b)2

Open in CodSpeed

Footnotes

  1. 29 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

  2. No successful run was found on main (e61ca3a) during the generation of this report, so d742f8b was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@Dunqing

Dunqing commented Nov 9, 2026

Copy link
Copy Markdown
Member

/monitor-oxc run

@oxc-guard

oxc-guard Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

@oxc-guard

oxc-guard Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Minify Size

feat/inline-single-use-fn-assignment vs main

1,560 of 109,516 files changed.

files changed raw gzip -9 brotli q5
normal 108,351 1,423
1,422 smaller, 1 larger
-44,417 B (-0.009%)
501.84 MB → 501.80 MB
-24,887 B (-0.017%)
148.08 MB → 148.06 MB
-27,710 B (-0.020%)
136.16 MB → 136.13 MB
already minified (*.min.js) 1,165 137
137 smaller, 0 larger
-5,308 B (-0.010%)
54.31 MB → 54.30 MB
-4,343 B (-0.031%)
14.16 MB → 14.16 MB
-4,025 B (-0.030%)
13.23 MB → 13.23 MB

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 node_modules corpus; gzip and brotli compress each file on its own and are summed. Sizes only: correctness is in the Monitor Oxc comment. Identical pnpm peer-variant copies were counted once (384 collapsed).

@oddharsh

oddharsh commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for kicking off monitor-oxc. I went through the red rows against main's scheduled run from an hour later (42592052954), comparing failing files rather than just counts.

Test262. Both runs move from 17417 to 17415, and every new failure in both is timeout: global. They land on different files each run: main times out on 2 transform tests (asi/S7.9_A6.1_T11.js, class/method-static/dflt-params-ref-self.js), and this PR on 2 minify tests (comments/S7.4_A5.js, class/dstr/async-private-gen-meth-ary-ptrn-elem-obj-id.js). Locally, the head build minifies those two in 0.19s and 0.45s, and both pass in Node. The second one has no function declarations, so this change never touches it.

node_modules suites. compressor (60), formatter (89), and codegen, dce, mangler, transformer, whitespace (1 each) fail on the same files with the same counts on main. The diffs are comment placement (/* istanbul ignore else */, JSDoc @type casts) and show up in transformer too, which doesn't run the minifier.

Uglify Corpus, Runtime Bundles, and the 5 runtime suites are green, and Minify Size shows 1,422 files smaller and 1 larger (-44,447 B raw, -24,977 B gzip).

I used Claude Code to pull and diff the logs; I checked the results myself.

armano2 and others added 13 commits November 16, 2026 02:49
…arlier in the list

Search the whole statement list for the declaration instead of only the
previous statement, and treat each assignment in a sequence or chain as a
target. The assignment holds the only reference, so no statement in between
can observe the move.

Looking only at the previous statement (or a run of declarations) was not
idempotent on three.js: merging expression statements into a sequence and
inlining a single-use `var` do not request another pass, so the move landed
on the second minify instead of the first.
Keep sloppy block declarations whose Annex B writes to enclosing var
bindings are not represented by resolved references. Apply the guard even
when unused-code removal is disabled.

Add Script/CommonJS and runtime regressions with mangling enabled and
disabled, and document the retained function-name metadata invariant.

AI assistance: Codex.
Two sloppy-mode cases let `inline_function_declaration_into_assignment`
inline a function declaration whose binding is still observable.

A block function that semantic leaves block-scoped, because the var scope
already binds its name, still overwrites that var binding when its
declaration is evaluated (Annex B.3.2.1). The write has no resolved
reference, so the outer declaration looked single-use:

    function f() { return 1 } { function f() { return 2 } } r.f = f;

was rewritten so that `r.f()` returned 1 instead of 2. In sloppy mode,
skip the move when a statement already in the list nests a plain function
declaration of the same name below a block, `if`, `switch`, loop, `try`
or `with`. The walk stays at statement level and never enters expressions
or function bodies, which are separate var scopes.

The direct-eval guard only checked the current scope. A hoisted block
function is bound in the enclosing var scope, where an eval outside the
block can read it. The binding scope's `DirectEval` flag is now checked
too.

minsize and allocation snapshots are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RRFyUtahqHbo7h2kPiWc98
Main now removes labels that no break needs, so the labeled Annex B
test case kept its label only by accident. Break to the label from a
nested loop so the label stays required and the case still exercises
the labeled-statement path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RRFyUsa2NjhY4WBxKQtWsC
@Dunqing
Dunqing force-pushed the feat/inline-single-use-fn-assignment branch from f8f59a1 to 6452484 Compare November 16, 2026 02:53
@Dunqing

Dunqing commented Nov 16, 2026

Copy link
Copy Markdown
Member

/monitor-oxc run

Merge main at e61ca3a into the PR head 6452484. The only conflict
was tasks/minsize/minsize.snap, after oxc-project#29614 (mangle labels) landed;
regenerated it with `cargo minsize`. `cargo allocs` reports no change and
`cargo test -p oxc_minifier` passes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RRFyUsa2NjhY4WBxKQtWsC
@Dunqing
Dunqing force-pushed the feat/inline-single-use-fn-assignment branch from 6307205 to 30ba68a Compare November 16, 2026 07:41

@Dunqing Dunqing left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Great job

@Dunqing
Dunqing merged commit 63b8158 into oxc-project:main Nov 16, 2026
39 checks passed
graphite-app Bot pushed a commit that referenced this pull request Nov 16, 2026
### 💥 BREAKING CHANGES

- d742f8b ast: [**BREAKING**] Add `LabelId` to `LabelIdentifier` (#29611) (camc314)

### 🚀 Features

- 63b8158 minifier: Inline single use function declaration to assignment (#28917) (Aadharsh  Pannirselvam)
- eb1833e mangler: Mangle labels (#29614) (camc314)
- 702e734 codegen: Print label names from scoping (#29613) (camc314)
- 750033a semantic: Resolve labels to label IDs (#29612) (camc314)
- ccca379 minifier: Remove unused labels (#29607) (camc314)
- e375dff lexer: Complete NEON support for aarch64 (#29390) (Shanon Jackson)
- bfe9a81 data_structures: Add buffered vector batch reservations (#29365) (Boshen)
- fa492d6 minifier: Merge adjacent switch cases with identical bodies (#29316) (camc314)
- 8cc6101 minifier: Inline member reads into callees as indirect calls (#29305) (camc314)
- e3d7d91 minifier: Inline member reads into `new` expressions (#29304) (camc314)
- 57211d0 minifier: Inline member reads into call arguments (#29303) (camc314)
- 523a063 lexer: Publish lexer and SIMD crates (#29264) (Boshen)
- 2a5d710 data_structures: Add `unreachable_unchecked!` macro (#29131) (overlookmotel)
- 061becc react-compiler: Support logical assignment operators (#29137) (Yagiz Nizipli)

### 🐛 Bug Fixes

- d520358 transformer: Retain decorated fields with define semantics (#29444) (Dunqing)
- 83fb644 lexer: Combine surrogate pairs written with any escape form (#29658) (Shanon Jackson)
- f8e329c transformer: Preserve optional calls on private field chains (#29443) (Dunqing)
- 4b86eba react-compiler: Do not report hoisted function declaration initializer as access before declaration (#29641) (Boshen)
- 7e41638 parser: Keep whole parenthesized `TSInstantiationExpression` with `preserve_parens: false` (#29359) (leaysgur)
- 5aa6c17 napi/parser: Release native parse result eagerly (#29569) (Boshen)
- 1dc0ee9 semantic: Preserve catch and var binding identities (#28739) (Dunqing)
- 11dbaf4 ecmascript: Prefer hex integers when lengths tie (#29436) (camc314)
- e64155e transformer/react-refresh: Recognize TypeScript wrappers in HOC arguments (#29290) (camc314)
- 9085be1 react-compiler: Only report zero-argument new Date in render body (#29419) (Boshen)
- 9f465fd react-compiler: Preserve ref mutation hints (#28553) (Nash Aschenbath)
- 075b74f lexer: Do not drop UTF-8 validation errors in TSX type arguments (#29409) (overlookmotel)
- 59cbbbf lexer: Fix source length assertion (#29408) (overlookmotel)
- f37f271 react-compiler: Resolve computed dependencies in merged memo scopes (#29376) (camc314)
- ab73590 parser: Preserve re-exported default import names in raw transfer (#29234) (camc314)
- 6e6bd61 react-compiler: Avoid invariants for catch phis and ref-free callbacks (#29018) (camc314)
- 19b1691 isolated-declarations: Retain types used by exported ambient variables (#28996) (camc314)
- b372b04 transformer/optional-chaining: Preserve parenthesized chain boundaries (#29218) (camc314)
- efcdf06 transformer: Isolate logical assignment temporaries in parameters (#29182) (Dunqing)
- 177fdd7 parser: Classify re-exported namespaces as indirect exports (#29196) (Boshen)
- 053cc40 parser: Preserve type-only flags on re-exported imports (#29195) (Boshen)
- 34172e0 data_structures: Fix `assert_unchecked!` macro (#29130) (overlookmotel)
- b515929 transformer: Lower wrapped assignments and preserve computed receivers (#29113) (camc314)
- 59aed23 parser: Highlight invalid numeric separators (#29117) (camc314)
- 8f5e82c parser: Reject exponentiation operands hidden by assertions (#29115) (camc314)
- 42e790c parser: Require `declare` before `abstract` modifiers (#29111) (camc314)

### ⚡ Performance

- dd9af34 parser: Make parser and lexer checkpoints `Copy` (#29662) (Boshen)
- 4ebe523 parser: Keep the tokens read while classifying `()` and `(x)` arrows (#29560) (Boshen)
- 7a6b0a4 parser: Lex the token after `let` and `?.` once (#29559) (Boshen)
- bd9fa9b parser: Lex the token after a modifier keyword once (#29558) (Boshen)
- 2c6ee72 parser: Skip arrow speculation for array and object literals (#29557) (Boshen)
- 85c0269 parser: Rule out type arguments from tokens before speculating (#29556) (Boshen)
- b6edb78 parser: Avoid token loop restart after newlines (#29423) (Boshen)
- 762bbcf lexer: Cut NEON lexing cost in carve, coalesce and compress (#29414) (Shanon Jackson)
- f09af45 lexer: Keep source position in registers across byte handlers (#29401) (Boshen)
- ae1a375 lexer: Complete single-byte tokens without a byte handler (#29400) (Boshen)
- 11daf98 lexer: Build tokens once from pending state (#29399) (Boshen)
- c49e1bd str: Hash identifiers without branching on length (#29397) (Boshen)
- 8a6e0c1 lexer: Borrow contiguous escaped string values (#29372) (Boshen)
- 27baf0c lexer: Buffer more comments before arena allocation (#29371) (Boshen)
- baa84fc lexer: Size escaped strings before allocating (#29369) (Boshen)
- 4313dee parser: Buffer module record entries (#29368) (Boshen)
- 2df1f19 parser: Reduce regex, template, and comment allocations (#29361) (Boshen)
- f26d9d9 parser: Reduce arena allocations and reallocations (#29358) (Boshen)
- 846adbb mangler: Skip a liveness walk that never marks a scope (#29391) (Dunqing)
- 0d2e41a parser: Reduce identifier dispatch overhead (#29347) (Boshen)
- 1aaf3b0 parser: Reuse pure re-export storage (#29216) (Boshen)
- 9add80d parser: Buffer decorator and heritage lists (#29215) (Boshen)
- cb5cf55 parser: Collect prefixed import lists once (#29214) (Boshen)
- ec6b166 parser: Omit discarded recovered parentheses (#29213) (Boshen)
- 01e2b66 parser: Avoid discarded TypeScript instantiations (#29210) (Boshen)
- 727610a parser: Collect short named import lists (#29209) (Boshen)
- d1eaed0 parser: Collect short template literal lists (#29208) (Boshen)
- 0301b92 parser: Reserve module record batches (#29207) (Boshen)
- 9734804 parser: Collect short switch consequent lists (#29206) (Boshen)
- 6674b7a parser: Collect short JSX child lists (#29205) (Boshen)
- cc03246 parser: Collect short binding pattern lists (#29204) (Boshen)
- e36dd2d parser: Avoid discarded cover parameter allocations (#29203) (Boshen)
- 2d5864c parser: Collect short sequence expression lists (#29202) (Boshen)
- 6495e66 parser: Avoid discarded block allocations (#29199) (Boshen)
- 2601c45 parser: Collect short formal parameter lists (#29198) (Boshen)
- 10fddc3 parser: Collect short TypeScript type lists (#29197) (Boshen)
- 4140fb0 parser: Keep class heritage on the stack (#29190) (Boshen)
- e028d74 parser: Collect short statement lists before allocating (#29189) (Boshen)
- 69c7892 parser: Collect short declaration lists before allocating (#29188) (Boshen)
- aed0190 parser: Skip unused identifier hashes (#29168) (Boshen)
- 2c0a6c1 parser: Avoid allocating discarded directive statements (#29186) (Boshen)
- 48532bb parser: Buffer remaining parser lists (#29185) (Boshen)
- e59d413 parser: Reuse export entry storage (#29172) (Boshen)
- f815f54 parser: Reduce arena list reallocations (#29169) (Boshen)

### 📚 Documentation

- 9e5a2bb agents: Require runnable playground reproductions in PRs (#29450) (Dunqing)
- c4d1024 minifier: Require examples in fix and optimization PRs (#29439) (Dunqing)
- 43e0829 parser: Fix grammar context formatting (#29373) (camc314)
- 3e947d7 napi/parser: Correct TS-ESTree differences (#29268) (fisker Cheung)
- 521d194 transformer: Require reproductions in fix PR descriptions (#29224) (Dunqing)
- 24c4b78 simd: Improve comment (#29244) (overlookmotel)
- 306b96a lexer: Correct safety comment on `TokensView::new` (#29231) (overlookmotel)
- 44c65f0 lexer: Add comments (#29226) (overlookmotel)
- 796255d simd: Use consistent formatting for bullet points in doc comments (#29133) (overlookmotel)
- 794cf47 isolated-declarations: Add agent guide (#29125) (camc314)
- 768cc29 parser: Clarify syntax support and AST preservation (#29121) (camc314)
- 67ecd8c estree: Correct serialization capabilities (#29120) (camc314)
camc314 added a commit to camc314/rolldown that referenced this pull request Nov 16, 2026
Oxc inlines single-use function declarations into assignments. Regenerate the
minified CommonJS snapshots of mixed_reexport_dynamic_entry and
mixed_reexport_records. The exported readers become function expressions;
execution fixtures verify the same values and evaluation order. The immediate
parent leaves both groups unchanged on this Rolldown implementation.

First-bad-Oxc-Commit: 63b8158975aca717c4e88e0dc6bf0995a7e9e27c
Oxc-Revision: 63b8158975aca717c4e88e0dc6bf0995a7e9e27c
camc314 added a commit to camc314/rolldown that referenced this pull request Nov 16, 2026
Oxc inlines single-use function declarations into assignments. Regenerate the
minified CommonJS snapshots of mixed_reexport_dynamic_entry and
mixed_reexport_records. The exported readers become function expressions;
execution fixtures verify the same values and evaluation order. The immediate
parent leaves both groups unchanged on this Rolldown implementation.

First-bad-Oxc-Commit: 63b8158975aca717c4e88e0dc6bf0995a7e9e27c
Oxc-Revision: 63b8158975aca717c4e88e0dc6bf0995a7e9e27c
camc314 added a commit to camc314/rolldown that referenced this pull request Nov 16, 2026
Oxc inlines single-use function declarations into assignments. Regenerate the
minified CommonJS snapshots of mixed_reexport_dynamic_entry and
mixed_reexport_records. The exported readers become function expressions;
execution fixtures verify the same values and evaluation order. The immediate
parent leaves both groups unchanged on this Rolldown implementation.

First-bad-Oxc-Commit: 63b8158975aca717c4e88e0dc6bf0995a7e9e27c
Oxc-Revision: 63b8158975aca717c4e88e0dc6bf0995a7e9e27c
hyfdev pushed a commit to rolldown/rolldown that referenced this pull request Nov 17, 2026
## Summary

- Upgrade Oxc Rust dependencies to 0.159.0.
- Upgrade @oxc-project/runtime, @oxc-project/types, oxc-parser, and oxc-transform to 0.159.0.

## Root causes

### oxc-project/oxc#29238

Oxc adopts sourcemap v10, whose source entries may be null. Preserve null slots through map composition, path resolution, placeholder replacement, and the Rust/TypeScript boundary. Skip callbacks for unnamed entries while retaining their original indices, and keep single-source transform inference. Regenerate binding types and add execution coverage for nullable sources.

### oxc-project/oxc#29305

Oxc inlines single-use member callees as indirect calls, preserving the unbound receiver. Regenerate both wrapping variants of namespace_pure_reexport_barrel; their execution assertions retain the same values.

### oxc-project/oxc#29436

Oxc prefers hexadecimal integers when their spelling ties decimal. Regenerate the enum, advanced-chunk module-size, keep-names, and JSON large-integer expectations. Values and byte lengths remain unchanged, including chunk-size boundaries and JSON.parse precision loss.

### oxc-project/oxc#28739

Oxc separates catch parameters from the enclosing var bindings they redeclare. Scan implicit module vars, coordinate printed names without merging their values, and use normal wrapped-ESM declaration hoisting. Include same-named Annex B block functions in each naming group. Update the wrapped-ESM snapshot and add execution fixtures for imported-name conflicts, nested catches, loop/destructuring declarations, and sloppy CommonJS block functions.

### oxc-project/oxc#29607

Oxc removes unused labels. Regenerate minify_nested_labels_no_bundle: nested labels and blocks disappear while all 21 newline calls execute in the same order.

### oxc-project/oxc#28917

Oxc inlines single-use function declarations into assignments. Regenerate the minified CommonJS mixed_reexport_dynamic_entry and mixed_reexport_records snapshots: exported readers become function expressions, preserving values and evaluation order.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-minifier Area - Minification, compression and name mangling, oxc_minifier / oxc_mangler

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants