From 7c5c3304e46004b0d2e7444935b084746f2d7c95 Mon Sep 17 00:00:00 2001 From: Dylan Conway Date: Fri, 29 May 2026 00:11:55 +0000 Subject: [PATCH 1/2] bun_alloc: delete unused MimallocHeapRef, TypedArena, BumpWithFallback, and Zig-port stubs --- src/bun_alloc/lib.rs | 4 +- src/bun_alloc/maybe_owned.rs | 6 -- src/bun_alloc/memory.rs | 10 --- src/bun_alloc/stack_fallback.rs | 110 -------------------------------- 4 files changed, 1 insertion(+), 129 deletions(-) diff --git a/src/bun_alloc/lib.rs b/src/bun_alloc/lib.rs index 9b7a3d9f82f..a19e3febdd0 100644 --- a/src/bun_alloc/lib.rs +++ b/src/bun_alloc/lib.rs @@ -326,8 +326,6 @@ macro_rules! arena_format { __s }}; } -/// `typed_arena::Arena` — typed slab with stable addresses (AST node Store). -pub type TypedArena = typed_arena::Arena; /// `bun.use_mimalloc` — false under ASAN, where the global allocator is `std::alloc::System`. pub const USE_MIMALLOC: bool = cfg!(not(bun_asan)); @@ -526,7 +524,7 @@ pub use buffer_fallback_allocator::BufferFallbackAllocator; pub use max_heap_allocator::MaxHeapAllocator; pub use maybe_owned::MaybeOwned; pub use nullable_allocator::NullableAllocator; -pub use stack_fallback::{ArenaPtr, BumpWithFallback, MimallocHeapRef, StackFallback}; +pub use stack_fallback::{ArenaPtr, StackFallback}; #[path = "MimallocArena.rs"] pub mod mimalloc_arena; diff --git a/src/bun_alloc/maybe_owned.rs b/src/bun_alloc/maybe_owned.rs index 55833c14262..65411d46868 100644 --- a/src/bun_alloc/maybe_owned.rs +++ b/src/bun_alloc/maybe_owned.rs @@ -69,12 +69,6 @@ impl MaybeOwned { self._parent.as_ref() } - pub fn into_parent(self) -> Option { - // Zig: `defer self.* = undefined; return self.rawParent();` - // Taking `self` by value consumes it; no explicit invalidation needed. - self._parent - } - /// Used by smart pointer types and allocator wrappers. See `crate::borrow`. pub fn borrow(&self) -> MaybeOwnedBorrowed { // Borrowed view carries no allocator state — just the owned/borrowed bit. diff --git a/src/bun_alloc/memory.rs b/src/bun_alloc/memory.rs index 717efa9058a..2aa0849b100 100644 --- a/src/bun_alloc/memory.rs +++ b/src/bun_alloc/memory.rs @@ -2,16 +2,6 @@ /// Default-initializes a value of type `T`. /// -/// Zig tried, in order: `T.initDefault()`, then `T.init()`, then `.{}`. All three -/// collapse into Rust's `Default` trait — types that had `initDefault`/`init` in Zig -/// should `impl Default` in their Rust port. -// PORT NOTE: `std.meta.hasFn` (≈ `@hasDecl`) fallback chain → single `Default` bound -// per §Comptime reflection ("optional behavior → trait with default method"). -#[inline] -pub fn init_default() -> T { - T::default() -} - // ────────────────────────────────────────────────────────────────────────────── // PORT NOTE: `exemptedFromDeinit`, `deinitIsVoid`, and `deinit` are intentionally // NOT ported as functions. diff --git a/src/bun_alloc/stack_fallback.rs b/src/bun_alloc/stack_fallback.rs index 80e3035f857..69880418b7b 100644 --- a/src/bun_alloc/stack_fallback.rs +++ b/src/bun_alloc/stack_fallback.rs @@ -59,9 +59,6 @@ pub struct StackFallback { buf: UnsafeCell<[MaybeUninit; N]>, } -/// Back-compat alias for the previous name; prefer [`StackFallback`]. -pub type BumpWithFallback = StackFallback; - impl StackFallback { /// Zig: `std.heap.stackFallback(N, fallback)`. `const` — `MaybeUninit: /// Copy`, so `[MaybeUninit::uninit(); N]` needs no inline-const; @@ -107,13 +104,6 @@ impl StackFallback { &self.fallback } - /// Mutably borrow the fallback allocator (e.g. to rebind a heap pointer - /// after the backing `MimallocArena::reset` swapped it). - #[inline] - pub fn fallback_mut(&mut self) -> &mut A { - &mut self.fallback - } - #[inline(always)] fn buf_base(&self) -> *mut u8 { self.buf.get().cast::() @@ -405,106 +395,6 @@ unsafe impl Allocator for ArenaPtr { } } -// ── MimallocHeapRef ────────────────────────────────────────────────────────── -// -// Thin `Allocator` over a raw `*mut mi_heap_t`. Unlike [`ArenaPtr`] this -// addresses the C-heap-resident `mi_heap_t` directly (stable across moves of -// the `MimallocArena` wrapper struct), at the cost of bypassing -// `MimallocArena::track_alloc`. Kept for callers that only have a heap handle. -// -// `heap == null` routes to global `mi_malloc`/`mi_free`, matching -// [`crate::ast_alloc::AstAlloc`] when no AST scope is active. - -/// Borrowed `mi_heap_t*` as an [`Allocator`]. See section doc above. -#[derive(Clone, Copy)] -pub struct MimallocHeapRef { - heap: *mut mimalloc::Heap, -} - -impl MimallocHeapRef { - /// Wrap a live `mi_heap_t*`. The caller guarantees `heap` outlives every - /// allocation made through this ref (i.e. the owning `MimallocArena` is not - /// `reset()`/dropped while this ref is in use). - #[inline] - pub const fn new(heap: *mut mimalloc::Heap) -> Self { - Self { heap } - } - /// Null heap → process-global `mi_malloc`/`mi_free`. - #[inline] - pub const fn global() -> Self { - Self { - heap: ptr::null_mut(), - } - } - /// The wrapped heap pointer (null when global). - #[inline] - pub fn heap(&self) -> *mut mimalloc::Heap { - self.heap - } - /// Rebind to a new heap (e.g. after `MimallocArena::reset` rebuilt it). - #[inline] - pub fn set_heap(&mut self, heap: *mut mimalloc::Heap) { - self.heap = heap; - } -} - -// SAFETY: identical contract to `&MimallocArena` / `AstAlloc` — -// `mi_[heap_]malloc[_aligned]` yields ≥`size` bytes aligned to `align`; -// `mi_free` accepts any mimalloc-owned pointer regardless of origin heap; -// `mi_[heap_]realloc_aligned` preserves the `min(old,new)` prefix and frees -// the old block. `heap` must be null or a live `mi_heap_t*` for this ref's -// lifetime (caller contract — see [`MimallocHeapRef::new`]). -unsafe impl Allocator for MimallocHeapRef { - #[inline] - fn allocate(&self, layout: Layout) -> Result, AllocError> { - let h = self.heap; - let p = if h.is_null() { - mimalloc::mi_malloc_auto_align(layout.size(), layout.align()) - } else { - // SAFETY: `h` is live per the caller contract on `new`. - unsafe { mimalloc::mi_heap_malloc_auto_align(h, layout.size(), layout.align()) } - }; - alloc_result(p, layout.size()) - } - - #[inline] - unsafe fn deallocate(&self, ptr: NonNull, _layout: Layout) { - // SAFETY: `ptr` came from `mi_[heap_]malloc*` per `allocate`/`grow`; - // `mi_free` is heap-agnostic and thread-safe. - unsafe { mimalloc::mi_free(ptr.as_ptr().cast()) } - } - - #[inline] - unsafe fn grow( - &self, - ptr: NonNull, - _old: Layout, - new: Layout, - ) -> Result, AllocError> { - let h = self.heap; - // SAFETY: see `allocate`; realloc accepts cross-heap pointers. - let p = unsafe { - if h.is_null() { - mimalloc::mi_realloc_aligned(ptr.as_ptr().cast(), new.size(), new.align()) - } else { - mimalloc::mi_heap_realloc_aligned(h, ptr.as_ptr().cast(), new.size(), new.align()) - } - }; - alloc_result(p, new.size()) - } - - #[inline] - unsafe fn shrink( - &self, - ptr: NonNull, - old: Layout, - new: Layout, - ) -> Result, AllocError> { - // SAFETY: same realloc path as `grow`. - unsafe { self.grow(ptr, old, new) } - } -} - // ported from: vendor/zig/lib/std/heap.zig (stackFallback / StackFallbackAllocator) // ported from: vendor/zig/lib/std/heap/FixedBufferAllocator.zig (inlined) From 3b1c8cc38e07730e9eb30de4c688c130c5056d24 Mon Sep 17 00:00:00 2001 From: Dylan Conway Date: Fri, 29 May 2026 01:08:28 +0000 Subject: [PATCH 2/2] review: drop leftover init_default doc-comment lines --- src/bun_alloc/memory.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/bun_alloc/memory.rs b/src/bun_alloc/memory.rs index 2aa0849b100..bc97888d15d 100644 --- a/src/bun_alloc/memory.rs +++ b/src/bun_alloc/memory.rs @@ -1,7 +1,5 @@ //! Basic utilities for working with memory and objects. -/// Default-initializes a value of type `T`. -/// // ────────────────────────────────────────────────────────────────────────────── // PORT NOTE: `exemptedFromDeinit`, `deinitIsVoid`, and `deinit` are intentionally // NOT ported as functions.