refactor(allocator): fixed size allocators handle their own deallocation#22085
Conversation
How to use the Graphite Merge QueueAdd either label to this PR to merge it via the merge queue:
You must have a Graphite account in order to use the merge queue. Sign up using this link. An organization admin has enabled the Graphite Merge Queue in this repository. Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue. This stack of pull requests is managed by Graphite. Learn more about stacking. |
Merging this PR will not alter performance
Comparing Footnotes
|
There was a problem hiding this comment.
Pull request overview
Refactors oxc_allocator fixed-size arena chunks to self-manage deallocation safely by recording which allocator (System vs global) produced the backing allocation, eliminating the need for external/manual System deallocation and allowing new_fixed_size to be safe.
Changes:
- Add an
is_fixed_sizeflag toChunkFooterand set it for arenas created viafrom_raw_parts/ fixed-size paths. - Centralize chunk deallocation through
dealloc_arena_chunk, selectingSystem.deallocvsalloc::deallocbased onis_fixed_size. - Make
Arena::new_fixed_size/Allocator::new_fixed_sizesafe and update fixed-size pool teardown to use the shared deallocation helper.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| crates/oxc_allocator/src/pool/fixed_size.rs | Switch fixed-size allocator teardown to dealloc_arena_chunk and remove manual System dealloc handling. |
| crates/oxc_allocator/src/fixed_size.rs | Make Allocator::new_fixed_size safe and rely on arena-managed lifecycle. |
| crates/oxc_allocator/src/arena/mod.rs | Extend ChunkFooter with is_fixed_size and re-export dealloc helper for fixed-size feature. |
| crates/oxc_allocator/src/arena/from_raw_parts.rs | Mark raw-parts arenas as fixed-size for System-backed deallocation. |
| crates/oxc_allocator/src/arena/fixed_size.rs | Make Arena::new_fixed_size safe and construct via from_raw_parts. |
| crates/oxc_allocator/src/arena/drop.rs | Add dealloc_arena_chunk and route chunk list deallocation through it with allocator selection. |
| crates/oxc_allocator/src/arena/create.rs | Initialize ChunkFooter.is_fixed_size to false for normal (global-alloc) chunks. |
b90bd7e to
bd7048a
Compare
Merge activity
|
…ion (#22085) `Arena`'s chunks already store the pointer and layout of their backing allocations in their `ChunkFooter`s. However, it still wasn't safe for them to handle `Drop` themselves, because fixed-size `Arena`s obtain backing memory direct from the `System` allocator, bypassing the usual global allocator (which can be switched to e.g. Mimalloc). Therefore, special handling was required to ensure the backing memory was also freed via `System` allocator - unlike normal `Arena`s. Fix that by storing an `is_fixed_size` flag in `ChunkFooter`. This flag is set in `Arena`s created by `Arena::from_raw_parts` (and so, by extension, by `Arena::new_fixed_size`). When `is_fixed_size` flag is set on a chunk, the `Arena` correctly frees the chunk's backing memory via `System` allocator. When it's not set (normal `Arena`s), global allocator is used as usual. `Arena`s created with `from_raw_parts` / `new_fixed_size` can now manage the entire lifecycle of the memory they own. `Arena::new_fixed_size` therefore doesn't need to be an unsafe method any more. All deallocation now goes through `dealloc_arena_chunk` which `Arena` provides, which correctly checks the `is_fixed_size `flag to determine which allocator to return the backing memory to.
1638b41 to
6de10fc
Compare
bd7048a to
2c044b5
Compare
Remove `can_grow` flag from `Arena`. Instead refuse to grow the `Arena` with a new chunk if `ChunkFooter::is_fixed_size` (introduced in #22085) is `true`. This flag is set for fixed-size allocators, so it performs the same purpose as the old `can_grow` flag.
…22093) Comments-only change. #22085 altered the docs for `Arena::from_raw_parts` to reflect that arena chunk created by this method will be deallocated via `System` allocator by default, but neglected to update docs for `Allocator::from_raw_parts`. Update the doc comment for this method to bring it into sync.

Arena's chunks already store the pointer and layout of their backing allocations in theirChunkFooters.However, it still wasn't safe for them to handle
Dropthemselves, because fixed-sizeArenas obtain backing memory direct from theSystemallocator, bypassing the usual global allocator (which can be switched to e.g. Mimalloc). Therefore, special handling was required to ensure the backing memory was also freed viaSystemallocator - unlike normalArenas.Fix that by storing an
is_fixed_sizeflag inChunkFooter. This flag is set inArenas created byArena::from_raw_parts(and so, by extension, byArena::new_fixed_size). Whenis_fixed_sizeflag is set on a chunk, theArenacorrectly frees the chunk's backing memory viaSystemallocator. When it's not set (normalArenas), global allocator is used as usual.Arenas created withfrom_raw_parts/new_fixed_sizecan now manage the entire lifecycle of the memory they own.Arena::new_fixed_sizetherefore doesn't need to be an unsafe method any more.All deallocation now goes through
dealloc_arena_chunkwhichArenaprovides, which correctly checks theis_fixed_sizeflag to determine which allocator to return the backing memory to.