perf(minifier): use ReplaceWith instead of TakeIn#24017
Conversation
Merging this PR will not alter performance
Comparing Footnotes
|
There was a problem hiding this comment.
Pull request overview
Updates the minifier’s peephole normalization to use the new ReplaceWith API (instead of take_in) where the original node is immediately overwritten, reducing unnecessary arena allocations and updating the tracked allocation snapshot accordingly.
Changes:
- Refactor
convert_while_to_forto useStatement::replace_with(...)when rewritingwhileintofor. - Update
allocs_minifier.snapto reflect reduced arena allocations from the change.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| tasks/track_memory_allocations/allocs_minifier.snap | Updates expected allocation counts after the minifier change. |
| crates/oxc_minifier/src/peephole/normalize.rs | Uses ReplaceWith to avoid allocating dummy nodes during while→for rewrites. |
4b6ee85 to
c1fc2ce
Compare
1d5370a to
0b6374a
Compare
0b6374a to
6c6b14d
Compare
c1fc2ce to
5c8f03c
Compare
6c6b14d to
e1ad60b
Compare
5c8f03c to
865115a
Compare
@Dunqing Just to draw your attention to this.
|
Sounds good! Let me do it after I clean up my PRs in hand. |
Merge activity
|
865115a to
815a575
Compare
e1ad60b to
40f769d
Compare
…24292) ## Issue The `Allocations` CI job can fail on PRs with a 1-allocation `Sys allocs` difference that local `cargo allocs` cannot reproduce. #22621 fixed one instance of this in the transformer stage. #24112 and #24017 hit the same failure again in the minifier stage: `kitchen-sink.tsx` measures 2309 Sys allocs on macOS/arm64 but 2310 on Linux/x64, so whichever platform regenerates the snapshot last breaks CI for the other. ## Why The tracker counts every call to the system allocator. That includes the chunks bump arenas request. Whether an arena needs one more chunk depends on the total number of bytes in the arena, and byte totals are not the same on every target. For example, hashbrown tables use 16-byte control groups on x86_64 but 8-byte groups on aarch64. When an arena's content size sits close to a chunk boundary, one target crosses it and the other does not. In the observed failure, the minifier's mangler-phase semantic build creates a fresh `Scoping`. Its arena holds symbol names, resolved-reference vectors, and the per-scope `bindings` hash tables, which are slightly larger on x86_64. The traced Linux-only allocation happened under `Scoping::add_resolved_reference -> ArenaVec::push -> Arena::new_chunk`, when the push overflowed the current chunk and requested a new 720880-byte chunk. On aarch64 the same content fit in the existing chunk, so the count stayed one lower. Every other allocation class in the measurement grows on element counts, which are identical across platforms. Logging every allocation size during the minifier stage on macOS/arm64, Linux/arm64, and Linux/x64 showed the three sequences match one-to-one except for arena chunk requests. Any PR that shifts arena content near a chunk boundary can trip this again in any stage, so fixing one stage at a time does not close the class. ## Fix Count chunk allocations in `oxc_allocator` under the tracker-only `track_allocations` feature, and subtract them from the reported Sys allocs in the tracker. The counter is global rather than per-arena because short-lived arenas (like the one backing `Scoping`) are created and dropped inside the measured operation. Arena usage is still measured by the `Arena allocs` / `Arena reallocs` columns; only the platform-sensitive chunk-request count is excluded from `Sys allocs`. Parser and transformer rows are unchanged (the main arena is pre-warmed, and #22621 already removed transformer scoping growth). Semantic, minifier, and formatter rows drop by their per-stage chunk counts. ## Validation `cargo allocs` produces byte-identical snapshots on macOS/arm64, Linux/arm64 (Docker), and Linux/x64 (Docker), and is idempotent across reruns. `cargo test -p oxc_allocator` passes; clippy is clean with `track_allocations` both on and off, and the counter compiles out entirely without the feature. AI usage: AI assistance was used to investigate, implement, and validate this change.
|
@copilot resolve conflicts |
Replace `take_in` with `replace_with` in as many places as possible in minifier. See #24012 for explanation of the advantage of `replace_with`. Likely we could use `replace_with` in many more places, if we adapted the methods through which most replacement operations flow (which also track "dirty" state). I've left this for now, as I don't really understand the minifier.
3e70b22 to
b227a06
Compare
### 🚀 Features - 616bfa2 minifier: Remove unreachable code after terminating statements (#24441) (Dunqing) - ddab89a data_structures: Add `likely` and `unlikely` functions (#24368) (overlookmotel) - a3a39f9 react_compiler: Implement enableEmitHookGuards codegen (#24329) (Boshen) - b79eef7 minifier: Apply De Morgan's law to negated comparison chains in jump guards and loop tests (#24279) (Dunqing) - 34ff7b4 minifier: Drop write-only property assignments to unused local bindings by default (#24112) (Dunqing) - 1b829d8 semantic: Record const enums in EnumData (#24268) (Dunqing) - ba0944c semantic: Add `Scoping::set_symbol_span` (#24221) (camc314) ### 🐛 Bug Fixes - 7d33363 minifier: Preserve guaranteed throws from class heritage evaluation (#24349) (Dunqing) - 058a62f semantic: Track ambient contexts in `SemanticBuilder` (#24327) (camc314) - 721eb0b transformer/decorator: Scope accessor class binding (#24330) (camc314) - 1ebdce3 semantic: Allow reserved keywords in ambient declaration types (#24325) (camc314) - 460176a track-memory-allocations: Exclude arena chunks from Sys allocs (#24292) (Dunqing) - af4922b transformer: Clear lowered namespace redeclarations (#24300) (camc314) - ffd2765 semantic: Mark declared computed `MethodDefinition`s as type references (#24296) (camc314) - f17514b isolated-declarations: Emit const readonly fields as types (#24288) (camc314) - 40f769d minifier: Make `__proto__` write tracking execution-order independent (#24280) (Dunqing) - 6371fed transformer: Remove stale enum member bindings (#24272) (camc314) - f05dfab transformer: Correct symbol flags for lowered namespaces (#24271) (Dunqing) - 84eeb55 transformer: Correct symbol flags for lowered enums (#24269) (Dunqing) - c3057da transformer: Preserve generated class binding spans (#24220) (camc314) - 8260096 transformer: Correct span for lowered namespace symbol (#24222) (camc314) - 42d00d3 semantic: Mark declared class heritage as type references (#24237) (camc314) - 588d997 semantic: Mark TS `PropertyDefinition`s computed fields as type references (#24233) (camc314) - 9b95632 semantic: Mark computed method keys in `TSMethodSignature`s as type references (#24232) (camc314) ### ⚡ Performance - 5b26643 transformer_plugins: Dispatch global defines by trailing name (#23666) (Boshen) - dce0f29 react_compiler: Replace all compiled functions in a single AST walk (#24403) (Boshen) - f85f0d8 ast: Delegate inherited enum variants in clone_in and estree derives (#23555) (Boshen) - 3ff0234 allocator: Remove `unwrap` from `ReplaceWith` (#24365) (overlookmotel) - ab22e80 transformer: Fix Rust 1.97 performance regression (#24354) (camc314) - b47585c parser: Use `ReplaceWith` instead of `TakeIn` (#24018) (overlookmotel) - b227a06 minifier: Use `ReplaceWith` instead of `TakeIn` (#24017) (overlookmotel) Co-authored-by: Boshen <[email protected]>
…ue moves (#10285) ## What Converts the `take_in` → build → write-back patterns in the module finalizer, scan pre-processor, HMR finalizer, and lazy-export generation to `oxc_allocator::ReplaceWith` (available since oxc 0.139) or plain by-value moves. ## Why Each `take_in` writes a `Dummy` node into the vacated slot; for AST enums that dummy is a real arena allocation (16 bytes boxed) that is overwritten an instant later but stays in the pooled module arena until the generate stage ends. It advances the bump pointer (raising the pool's retained high-water mark) and lowers live-node density for every later walk over the same arena — scanner, DCE, finalizer, codegen. oxc's own parser (oxc-project/oxc#24018) and minifier (oxc-project/oxc#24017) made the same move in 0.140.0. ## How - **Slot-level `replace_with`**: keep-name wraps (finalizer ×3), both dynamic-`import()`-in-CJS rewrites, the `.then((m) => __toESM(...))` interop wrapper, `replace_first_expr_stmt`, and the PreProcessor's `require(a?b:c)` / `import(a?b:c)` transposes (those destructure the conditional by value — 3 dummies each, now 0). - **By-value with box reuse**: the finalizer's export-default block and the HMR finalizer's `handle_top_level_stmt` now match the owned statement directly, so `export default function/class` reuses the existing `Box<Function>`/`Box<Class>` instead of take-dummy-rebox (also saves the ~200-byte struct copy). `get_transformed_class_decl` takes the class box by value and returns `Result<Declaration, Box<Class>>`, giving the box back when no transform applies. - **Plain moves where no trait is needed**: var-hoisting drains `declarations` by value (`Vec`'s dummy is an empty vec — no allocation), JSON prop inlining uses `Option::take`, and paren unwrapping in lazy export works by value. Left as-is: the `Vec` drain-and-rebuild `take_in`s (free dummy, structurally required) and the `take_in_box` in the dynamic-import-to-other-chunk path, whose `Option`-returning protocol tolerates a partially consumed node on the warn paths. One behavior note: the finalizer's export-default fallback arm is now `unreachable!()` for `TSInterfaceDeclaration` (TS is stripped before finalization), matching the convention already used by `visit_declaration` and the HMR finalizer for TS variants. Output is byte-identical: the snapshot suite passes with zero snapshot updates.

Replace
take_inwithreplace_within as many places as possible in minifier. See #24012 for explanation of the advantage ofreplace_with.Likely we could use
replace_within many more places, if we adapted the methods through which most replacement operations flow (which also track "dirty" state). I've left this for now, as I don't really understand the minifier.