Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/runtime/api/JSTranspiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use std::io::Write as _;

use crate::node::{Encoding, StringOrBuffer};
use bun_alloc::{Arena, ArenaVec}; // bumpalo::Bump / bumpalo::collections::Vec re-exports
use bun_alloc::{Arena, ArenaVec};
use bun_ast::Expr;
use bun_ast::Loader;
use bun_ast::{ImportRecord, ImportRecordFlags};
Expand Down Expand Up @@ -987,7 +987,15 @@
log: bun_ast::Log::init(),
..Default::default()
};
let arena = Box::new(Arena::new());
// `borrowing_default()` wraps `mi_heap_main()` so the inner `Transpiler`
// allocates from the process-global heap, matching
// `Transpiler.Transpiler.init(bun.default_allocator, ...)`. Every parse
// (`scan` / `transformSync` / `scanImports` / async `transform`) swaps in
// a fresh per-call `Arena::new()` via `TranspilerStateGuard`, so this
// arena is only the resting-state handle and `configure_defines`' bump.
// An owned `mi_heap_new()` here would pin a dedicated mimalloc heap per
// `new Bun.Transpiler()` for the instance's lifetime.
let arena = Box::new(Arena::borrowing_default());

Check failure on line 998 in src/runtime/api/JSTranspiler.rs

View check run for this annotation

Claude / Claude Code Review

Config-string leak: borrowing_default() arena no longer bulk-frees on Drop

Switching to `Arena::borrowing_default()` makes the arena's `Drop` a no-op (`MimallocArena::drop` returns early when `!self.owns`), so config-time allocations into this arena — notably `arena.alloc_slice_copy(&buf)` for each string value in `exports.replace` (`export_replacement_value()`), plus `configure_defines()`' bump — are now stranded in `mi_heap_main` for the process lifetime instead of being bulk-freed when the `JSTranspiler` is GC'd. The Zig reference uses a separate per-instance bump a
Comment thread
claude[bot] marked this conversation as resolved.
Resolved
Comment thread
claude[bot] marked this conversation as resolved.
Resolved
// SAFETY: `arena` is heap-allocated and moved (as a Box) into `Box<JSTranspiler>` below;
// its address is stable for the lifetime of the JSTranspiler. `Transpiler<'static>` forces
// the borrow to 'static, so launder through a raw ptr.
Expand Down
38 changes: 38 additions & 0 deletions test/js/bun/transpiler/transpiler-arena-heap.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { heapStats } from "bun:jsc";
import { expect, test } from "bun:test";

// The inner `Transpiler` is constructed with the process-global allocator
// (`bun.default_allocator` in the reference implementation). Constructing a
// `Bun.Transpiler` must not `mi_heap_new()` a dedicated mimalloc heap per
// instance; each owned heap pins tens of KB of segment metadata for the
// object's lifetime.
test("new Bun.Transpiler() does not create a per-instance mimalloc heap", () => {
// Warm up: first construction may lazily initialize shared state.
new Bun.Transpiler({ loader: "ts" });
Bun.gc(true);

const before = heapStats({ dump: true }).mimallocDump.heaps.length;

const instances: Bun.Transpiler[] = [];
const N = 64;
for (let i = 0; i < N; i++) {
instances.push(new Bun.Transpiler({ loader: "ts" }));
}

const after = heapStats({ dump: true }).mimallocDump.heaps.length;
const delta = after - before;

// Allow a small slack for unrelated lazy heap creation, but nowhere near
// one heap per instance.
expect({ before, after, delta, instances: instances.length }).toEqual({
before: expect.any(Number),
after: expect.any(Number),
delta: expect.any(Number),
instances: N,
});
Comment thread
claude[bot] marked this conversation as resolved.
Resolved
expect(delta).toBeLessThan(N / 4);

// Keep `instances` live across the measurement and verify they still work.
expect(instances[0].transformSync("export const x: number = 1;")).toContain("const x = 1");
expect(instances[N - 1].transformSync("export const y: number = 2;")).toContain("const y = 2");
});
Loading