Skip to content

feat(allocator): add methods for boxed slices ArenaBox<[T]>#19968

Merged
graphite-app[bot] merged 1 commit intomainfrom
om/03-03-feat_allocator_add_methods_for_boxed_slices_arenabox_t_
Mar 3, 2026
Merged

feat(allocator): add methods for boxed slices ArenaBox<[T]>#19968
graphite-app[bot] merged 1 commit intomainfrom
om/03-03-feat_allocator_add_methods_for_boxed_slices_arenabox_t_

Conversation

@overlookmotel
Copy link
Member

@overlookmotel overlookmotel commented Mar 3, 2026

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.

Copy link
Member Author

overlookmotel commented Mar 3, 2026


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

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.

@github-actions github-actions bot added the C-enhancement Category - New feature or request label Mar 3, 2026
@codspeed-hq
Copy link

codspeed-hq bot commented Mar 3, 2026

Merging this PR will not alter performance

✅ 53 untouched benchmarks
⏩ 3 skipped benchmarks1


Comparing om/03-03-feat_allocator_add_methods_for_boxed_slices_arenabox_t_ (64eee51) with main (7502afe)

Open in CodSpeed

Footnotes

  1. 3 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.

@overlookmotel overlookmotel marked this pull request as ready for review March 3, 2026 14:34
Copilot AI review requested due to automatic review settings March 3, 2026 14:34
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 + Dummy impls for Box<'a, [T]>.
  • Add Box::<[T]>::new_empty_boxed_slice constructor for empty boxed slices.
  • Add Box<[T]>::into_bump_slice / into_bump_slice_mut for 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_mut says 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 via mem::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) }
    }

@overlookmotel overlookmotel self-assigned this Mar 3, 2026
@overlookmotel overlookmotel force-pushed the om/03-03-feat_allocator_add_methods_for_boxed_slices_arenabox_t_ branch from d8c7299 to 64eee51 Compare March 3, 2026 15:19
@overlookmotel overlookmotel requested a review from Copilot March 3, 2026 15:20
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 cites Box<[T]> / &mut [T] layout equivalence, but the key operation is unsafe lifetime extension of the &mut [T] produced by as_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", but new_empty_boxed_slice intentionally does not allocate (it uses a dangling, aligned pointer with len=0). Consider rewording this to avoid implying an allocation happened and to clarify why NonNull<[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 around as_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] via as_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] {

@graphite-app graphite-app bot added the 0-merge Merge with Graphite Merge Queue label Mar 3, 2026
@graphite-app
Copy link
Contributor

graphite-app bot commented Mar 3, 2026

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.
@graphite-app graphite-app bot force-pushed the om/03-03-feat_allocator_add_methods_for_boxed_slices_arenabox_t_ branch from 64eee51 to 8345318 Compare March 3, 2026 16:23
@graphite-app graphite-app bot merged commit 8345318 into main Mar 3, 2026
22 checks passed
@graphite-app graphite-app bot deleted the om/03-03-feat_allocator_add_methods_for_boxed_slices_arenabox_t_ branch March 3, 2026 16:30
camc314 pushed a commit that referenced this pull request Mar 9, 2026
### 🚀 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

0-merge Merge with Graphite Merge Queue C-enhancement Category - New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants