feat(allocator): add methods for boxed slices ArenaBox<[T]>#19968
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
Adds convenience APIs and trait impls to make oxc_allocator::Box<[T]> behave more like the arena Vec, especially for empty boxed slices and converting boxed slices into arena-lifetime slices.
Changes:
- Add
TakeIn+Dummyimpls forBox<'a, [T]>. - Add
Box::<[T]>::new_empty_boxed_sliceconstructor for empty boxed slices. - Add
Box<[T]>::into_bump_slice/into_bump_slice_mutfor arena-lifetime slice conversion.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| crates/oxc_allocator/src/take_in.rs | Enables TakeIn/Dummy usage with boxed slices (Box<[T]>). |
| crates/oxc_allocator/src/boxed.rs | Introduces empty boxed-slice constructor and conversion methods to &'alloc [T] / &'alloc mut [T]. |
Comments suppressed due to low confidence (1)
crates/oxc_allocator/src/boxed.rs:176
- The comment above
into_bump_slice/into_bump_slice_mutsays this is a no-op because "Box<[T]>and&[T]/&mut [T]have the same layout", but the code is not converting between those types—it’s extending a reference lifetime viamem::transmute. This is misleading and makes the safety rationale harder to audit.
Adjust the comment to describe the real invariant being relied upon (arena-backed storage + consuming self) rather than layout equivalence.
//
// `#[inline(always)]` because this is a no-op. `Box<[T]>` and `&[T]` have the same layout.
#[expect(clippy::inline_always)]
#[inline(always)]
pub fn into_bump_slice(self) -> &'a [T] {
let r = self.as_ref();
// Extend lifetime of reference to lifetime of the allocator.
// SAFETY: `self` is consumed by this method, so there cannot be any mutable references to it.
// The reference lives until the allocator is dropped or reset (`'a` lifetime).
// Don't need `mem::forget(self)` here, because `Box` does not implement `Drop`.
unsafe { mem::transmute::<&[T], &'a [T]>(r) }
}
d8c7299 to
64eee51
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (4)
crates/oxc_allocator/src/boxed.rs:198
- Same as
into_bump_slice: the comment citesBox<[T]>/&mut [T]layout equivalence, but the key operation is unsafe lifetime extension of the&mut [T]produced byas_mut(). Rewording would avoid implying the safety relies on a layout guarantee.
//
// `#[inline(always)]` because this is a no-op. `Box<[T]>` and `&mut [T]` have the same layout.
#[expect(clippy::inline_always)]
#[inline(always)]
pub fn into_bump_slice_mut(mut self) -> &'a mut [T] {
crates/oxc_allocator/src/boxed.rs:215
- The SAFETY comment says the pointer comes from "an empty slice allocated from
Box::new_empty_boxed_slice", butnew_empty_boxed_sliceintentionally does not allocate (it uses a dangling, aligned pointer with len=0). Consider rewording this to avoid implying an allocation happened and to clarify whyNonNull<[T]>::as_ref()is still sound for the empty-slice representation.
// SAFETY: `self.0` is always a unique reference allocated from a `Bump` in `Box::new_in`,
// or an empty slice allocated from `Box::new_empty_boxed_slice`
unsafe { self.0.as_ref() }
crates/oxc_allocator/src/boxed.rs:224
- Same as above: the SAFETY comment mentions an "empty slice allocated" by
new_empty_boxed_slice, but that constructor is explicitly non-allocating. Rewording this will make the aliasing/validity reasoning aroundas_mut()for empty slices easier to audit.
// SAFETY: `self.0` is always a unique reference allocated from a `Bump` in `Box::new_in`,
// or an empty slice allocated from `Box::new_empty_boxed_slice`
unsafe { self.0.as_mut() }
crates/oxc_allocator/src/boxed.rs:182
- The inline comment says this is a no-op because
Box<[T]>and&[T]have the same layout, but the implementation is actually taking a&[T]viaas_ref()and then extending its lifetime. The layout claim is at best incidental here and could be misleading; consider rewording to emphasize that this is a runtime no-op aside from the unsafe lifetime extension.
//
// `#[inline(always)]` because this is a no-op. `Box<[T]>` and `&[T]` have the same layout.
#[expect(clippy::inline_always)]
#[inline(always)]
pub fn into_bump_slice(self) -> &'a [T] {
Merge activity
|
Add methods to allocator `Box` to make it easier to work with boxed slices. * `Box::<[T]>::new_empty_boxed_slice` - create a new `Box<[T]>` with an empty slice (equivalent to `Vec::new_in`). * `Box::<[T]>::into_bump_slice` + `Box::<[T]>::into_bump_slice_mut` - convert a boxed slice to a `&[T]` / `&mut [T]` with lifetime of the arena (equivalent to same methods on `Vec`). * Implement `Dummy` and `TakeIn` for boxed slices.
64eee51 to
8345318
Compare
### 🚀 Features - e8547cc parser: Report error for using declarations in ambient contexts (#19934) (camc314) - 8345318 allocator: Add methods for boxed slices `ArenaBox<[T]>` (#19968) (overlookmotel) - f83be30 allocator: Add `Vec::push_fast` method (#19959) (overlookmotel) ### 🐛 Bug Fixes - 291d867 transformer_plugins: Unwrap ChainExpression after define replacement removes optional markers (#20058) (IWANABETHATGUY) - 36b2e56 codegen: Print type for TSImportEqualsDeclaration (#20128) (camc314) - 5a246ec codegen: Print type arguments for JSXOpeningElement (#20127) (camc314) - a40870e codegen: Preserve parens for TSNonNullExpression (#20125) (camc314) - ae830b2 codegen: Print `declare` for `TSInterfaceDeclaration` (#20124) (camc314) - 92cfb14 linter/plugins: Fix types for `walkProgram` and `walkProgramWithCfg` (#20081) (overlookmotel) - ee0491e apps,napi: Explicitly specify libs in tsconfigs (#20071) (camc314) - 588009e codegen: Print `static` keyword for TSIndexSignature (#19755) (Dunqing) - 5a8799c codegen: Print `with_clause` for `ExportNamedDeclaration` (#20002) (Dunqing) - 7502afe parser: Correct capacity for tokens `Vec` (#19967) (overlookmotel) ### ⚡ Performance - 4ea8f9a napi: Remove `napi_build::setup()` from `oxc_napi` to avoid redundant rebuilds (#20094) (Boshen) - 2baa5fb napi: Unify build-test profile to coverage for cache sharing (#20090) (Boshen) - 8ba61dd parser: Make pushing tokens faster (#19960) (overlookmotel) Co-authored-by: Boshen <1430279+Boshen@users.noreply.github.com>

Add methods to allocator
Boxto make it easier to work with boxed slices.Box::<[T]>::new_empty_boxed_slice- create a newBox<[T]>with an empty slice (equivalent toVec::new_in).Box::<[T]>::into_bump_slice+Box::<[T]>::into_bump_slice_mut- convert a boxed slice to a&[T]/&mut [T]with lifetime of the arena (equivalent to same methods onVec).DummyandTakeInfor boxed slices.