Skip to content

event_loop: replace JsPoster's hand-rolled vtable with Arc<dyn JsPost> - #37518

Closed
robobun wants to merge 1 commit into
mainfrom
farm/c83f5856/event-loop-jsposter-arc-dyn
Closed

robobun wants to merge 1 commit into
mainfrom
farm/c83f5856/event-loop-jsposter-arc-dyn

Conversation

@robobun

@robobun robobun commented Aug 11, 2026

Copy link
Copy Markdown
Collaborator

What

bun_event_loop::JsPoster is how crates below bun_jsc (spawn's waiter thread, the bundler's JS-loop hops, ConcurrentPoster) post a ConcurrentTask to a JS VM from another thread. It was a type-erased pair built by hand: a *const () plus a &'static JsPosterVTable with five unsafe fn slots (post, embedded_work_scheduled, embedded_work_finished, clone, drop), hand-written unsafe impl Send/Sync, an unsafe fn from_raw constructor, and manual Clone/Drop impls that called through the table. bun_jsc::vm_handle filled two such tables with nine trampolines that cast the pointer back to an Arc<PosterData> or Arc<IsolatedPosterInner> leaked with Arc::into_raw (Arc::increment_strong_count for clone, Arc::from_raw for drop).

// before
pub struct JsPosterVTable { pub post: unsafe fn(*const (), NonNull<ConcurrentTask>) -> Posted, /* 4 more */ }
pub struct JsPoster { data: *const (), vtable: &'static JsPosterVTable }
unsafe impl Send for JsPoster {}
unsafe impl Sync for JsPoster {}
pub unsafe fn from_raw(data: *const (), vtable: &'static JsPosterVTable) -> Self;

// after
pub trait JsPost: Send + Sync {
    fn post(&self, task: NonNull<ConcurrentTask>) -> Posted;
    fn embedded_work_scheduled(&self);
    fn embedded_work_finished(&self);
}
#[derive(Clone)]
pub struct JsPoster(Arc<dyn JsPost>);
pub fn new<T: JsPost + 'static>(inner: Arc<T>) -> Self;

PosterData and IsolatedPosterInner now implement JsPost directly (the isolated loop's two embedded_work_* methods are the explicit no-ops the old table pointed at, with the same comment), and their to_js_poster constructors pass the Arc they already allocated to JsPoster::new. The two vtable statics and nine trampolines in VmHandle.rs are deleted, lib.rs exports JsPost in place of JsPosterVTable, and JsPoster's public post/embedded_work_scheduled/embedded_work_finished/Clone surface is unchanged, so none of its holders (bun_spawn::Process, bun_bundler::BundleV2, bun_jsc::ConcurrentPoster, the 9 .post() call sites) change. Net: 15 unsafe blocks, 10 unsafe fns, the 5 unsafe fn pointer slots and both unsafe impls removed; none added. 42 insertions, 117 deletions across 3 files.

Why

The trait object carries what the comments used to: the pointee is an Arc that outlives every holder (Arc<dyn JsPost> owns it), and the poster is Send + Sync because the implementing types are, which the compiler now checks at each impl JsPost instead of bun_event_loop asserting it about a type it cannot see. The erasure itself stays because bun_event_loop sits below bun_jsc and cannot name the implementations. Cost: Arc<dyn JsPost> is the same two words (16 bytes) as the old {data, vtable} pair and keeps the non-null niche, so Option<JsPoster> in Process and BundleV2 stays 16 bytes; the unsized coercion in JsPoster::new does not reallocate, so the heap object is the very same Arc allocation the trampolines operated on; post and embedded_work_* are still a single indirect call each, now straight into the impl method (the dispatch additionally reads the payload alignment from the same vtable line to locate the value inside the Arc, four ALU instructions on x86-64, on a path that then performs two SeqCst atomics, a queue push and a wakeup once per completed off-thread job); and clone/drop become the inline Arc count operations that previously sat behind an indirect call. No new allocation, check or branch on any path.

Part of a series of small type-system hardening changes; each PR stands alone.

Verification

cargo check and cargo clippy are clean for the touched crates. Debug build succeeds. bun bd test test/js/web/workers/worker-refused-completion.test.ts test/js/bun/spawn/spawnSync.test.ts test/js/bun/spawn/spawn.test.ts test/bake/dev/plugins.test.ts: 172 pass, 8 skip, 0 fail across 4 files (spawn.test.ts also re-runs itself with BUN_FEATURE_FLAG_FORCE_WAITER_THREAD: 139 pass, 7 skip, 0 fail).

JsPoster was a {*const (), &'static JsPosterVTable} pair with five unsafe fn slots, hand-written Send/Sync impls, an unsafe from_raw constructor and manual Clone/Drop; bun_jsc filled the two vtables with nine trampolines that cast the pointer back to a leaked Arc<PosterData> or Arc<IsolatedPosterInner>. JsPoster is now a newtype over Arc<dyn JsPost>, where JsPost: Send + Sync is implemented directly by PosterData and IsolatedPosterInner, and JsPoster::new takes the Arc each implementation already allocated. The public post/embedded_work_* methods are unchanged, so no holder (spawn, bundler, ConcurrentPoster) changes. Arc<dyn JsPost> is the same two words as the old pair and keeps the niche Option<JsPoster> relied on, the heap object is the same Arc allocation as before, post is still one vtable dispatch, and clone/drop become the inline Arc count operations the trampolines performed behind an indirect call. Removes 15 unsafe blocks, 10 unsafe fns, the 5 unsafe fn pointer slots and both unsafe Send/Sync impls; thread safety of the poster is now implied by the impl types.
@robobun
robobun requested a review from alii August 11, 2026 14:22
@robobun

robobun commented Aug 11, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 7:40 AM PT - Aug 11th, 2026

✅ @robobun, your commit eb6354b1fb5cad7c57dcbaa2492db70f8f4b1e8b passed in Build #92294! 🎉


🧪   To try this PR locally:

bunx bun-pr 37518

That installs a local version of the PR into your bun-37518 executable, so you can run:

bun-37518 --bun

@coderabbitai

coderabbitai Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 4747a7eb-02e7-4e9c-911c-76676db7587b

📥 Commits

Reviewing files that changed from the base of the PR and between da3851e and eb6354b.

📒 Files selected for processing (3)
  • src/event_loop/AnyEventLoop.rs
  • src/event_loop/lib.rs
  • src/jsc/VmHandle.rs

Walkthrough

Changes

The event loop replaces the unsafe raw-pointer JsPoster vtable with a public JsPost trait and Arc-backed storage. PosterData and IsolatedPosterInner now implement the trait and construct posters through JsPoster::new.

JsPoster dispatch

Layer / File(s) Summary
JsPost trait and Arc-backed poster
src/event_loop/AnyEventLoop.rs, src/event_loop/lib.rs
JsPoster delegates posting and embedded-work operations to Arc<dyn JsPost>. The unsafe raw constructor and JsPosterVTable export were removed.
VmHandle poster implementations
src/jsc/VmHandle.rs
PosterData and IsolatedPosterInner implement JsPost. Their poster construction no longer uses raw callbacks or vtables.

Suggested reviewers: dylan-conway, jarred-sumner

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the primary change from a hand-written vtable to Arc.
Description check ✅ Passed The description explains the change, rationale, API impact, safety improvements, performance considerations, and verification results.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Comment @coderabbitai help to get the list of available commands.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM — mechanical replacement of a hand-rolled vtable with Arc<dyn JsPost>, net -75 lines and ~15 unsafe blocks removed with no behavior change.

What was reviewed

  • Verified JsPoster's public surface (post, embedded_work_*, Clone, Send/Sync) is preserved; Arc<dyn JsPost> auto-derives Send + Sync via the trait's supertrait bounds, which the compiler now checks per-impl.
  • Confirmed both impl bodies (PosterData, IsolatedPosterInner) are byte-for-byte the old trampoline bodies; the isolated-loop embedded_work_* no-ops keep the same rationale comment.
  • Grepped for remaining references to JsPosterVTable / JsPoster::from_raw (none) and for direct callers of IsolatedPosterInner::post outside the trait (none — only to_js_poster/new in event_loop.rs).
Extended reasoning...

Overview

This PR replaces bun_event_loop::JsPoster's hand-rolled type erasure — a *const () + &'static JsPosterVTable pair with five unsafe fn slots, manual Clone/Drop impls calling through the table, hand-written unsafe impl Send/Sync, and nine trampolines in VmHandle.rs that cast the erased pointer back to a leaked Arc — with a standard Arc<dyn JsPost> trait object. PosterData and IsolatedPosterInner now impl JsPost directly, and JsPoster::new takes the same Arc they were already allocating (unsized coercion, no reallocation). Three files touched: src/event_loop/AnyEventLoop.rs (the trait + newtype), src/event_loop/lib.rs (re-export swap), src/jsc/VmHandle.rs (impls replace trampolines + vtable statics). 42 insertions, 117 deletions.

Security risks

None. This is an internal refactor of the dispatch mechanism for posting concurrent tasks between threads; no user input, parsing, auth, or external surface is involved. The change strictly reduces the trusted-unsafe surface: the Send + Sync guarantee that bun_event_loop previously asserted blindly about a type it could not name is now checked by the compiler at each impl JsPost site (PosterData is auto-Send + Sync via VmHandle: Arc<Shared> and LoopKind: Copy; IsolatedPosterInner retains its existing justified unsafe impls for the gated raw pointer).

Level of scrutiny

Medium — this is thread-safety-adjacent code (cross-thread task posting to a JS VM), but the change is a textbook "let the compiler generate the vtable" refactor with no logic touched. Each new trait method body is the old trampoline body minus the unsafe { &*data.cast::<T>() } recovery, and the old clone/drop fn-pointer slots (Arc::increment_strong_count / Arc::from_raw) become the semantically identical #[derive(Clone)] on Arc. The size/niche analysis in the PR description checks out: Arc<dyn Trait> is two words with a non-null niche, same as the old {data, vtable} pair, so Option<JsPoster> in Process/BundleV2 stays 16 bytes.

Other factors

  • JsPoster's caller-facing API (post, embedded_work_scheduled, embedded_work_finished, Clone) is unchanged, so none of the 9 .post() sites or the Option<JsPoster> holders need edits — I grepped and found no remaining references to the removed JsPosterVTable or JsPoster::from_raw.
  • IsolatedPosterInner::post moved from pub(crate) inherent to trait impl; grep confirms its only external users go through to_js_poster/new, so no call site breaks.
  • No new tests are expected here per REVIEW.md conventions: this is a behavior-preserving internal refactor whose invariants are compiler-checked, and the PR ran the existing spawn/spawnSync/worker-refusal/plugins suites (172 pass) that exercise both JsPost implementations, including the BUN_FEATURE_FLAG_FORCE_WAITER_THREAD path.
  • The isolated-loop no-op embedded_work_* bodies preserve the explanatory comment verbatim, satisfying the "don't silently delete a load-bearing comment" rule.

Jarred-Sumner pushed a commit that referenced this pull request Sep 24, 2026
…hs (#43840)

### Problem
- `src/codegen/generate-classes.ts` still has the DOMJIT emitter (C++
signatures, `WithoutTypeChecks` wrappers, result-type asserts, Rust
thunks). None of it can run: `define()` in `class-definitions.ts` has
set `DOMJIT = undefined` on each field since #14005 (2024-09), and all
31 `*.classes.ts` files go through `define()`.
- Nothing is left for it to bind: #35002 deleted each Rust
`*_without_type_checks` fast path, and #36756 and #36903 removed the C++
leftovers.
- A `DOMJIT:` option in a `.classes.ts` file does nothing. Five exist.

### Fix
- Delete the emitter, the option type, the strip in `define()`, the five
ignored blocks, and the stale notes next to them: 6 files, 273 lines
removed.
- The generated output is the same except for 94 empty `#if
BUN_DEBUG`/`#endif` pairs in `ZigGeneratedClasses.h` and three
DOMJIT-only `#include`s in `ZigGeneratedClasses.cpp`.
`generated_classes.rs` is byte-identical.
- Verified: the generator run before and after, `bun bd`, `tsc -p src`,
and the tests in the Notes.
- Self-reviewed: 13 concerns raised, 13 addressed (Notes).

### Background
- DOMJIT is the JavaScriptCore fast path that lets the JIT call a host
function with unboxed, type-checked arguments. The generator could emit
a C++ signature and a Rust thunk for each method.
- The hand-written DOMJIT users (`Buffer.alloc`, `performance.now`,
`bun:ffi`) do not use this generator. They stay.
- Earlier sweeps (#39249, #41169) called this removal a design call.
This PR asks for that call alone.

### Downsides
- To turn generated DOMJIT on again, a person must restore these paths
from git history and write the Rust fast paths again.
- No runtime cost found. Checked: the generated `.cpp`, `.h` and `.rs`
differ only as Fix says.

<details><summary>Notes</summary>

**Removed**
- `generate-classes.ts`: `DOMJITName`, `argTypeName`, `DOMJITType`,
`DOMJITFunctionDeclaration`, `DOMJITFunctionDefinition`,
`domJITTypeCheckFields`, `RustDOMJITArgType`. Also the `DOMJIT` branches
in `zigExportName`, `propRow`, `renderDecls`, the `expectedResultType`
asserts in the host-function wrapper, both Rust thunk loops, and the
`DOMJITAbstractHeap.h`, `FrameTracers.h`, `DFGAbstractHeap.h` includes
of the generated prologue. The destructures that named `DOMJIT` also
lose the unused `cache` and `value` bindings.
- `class-definitions.ts`: the `DOMJIT?:` option type and the two
`.map()` calls in `define()` that erased it.
- Ignored live blocks: `Crypto.randomUUID`, `Crypto.timingSafeEqual`
(`crypto.classes.ts`), `ServerWebSocket.publishText`, `publishBinary`
(`server.classes.ts`), `TextDecoder.decode` (`encoding.classes.ts`).
- Stale notes: the commented-out `// DOMJIT: {` blocks with their crash
notes on `sendText`, `sendBinary` (2023) and `getRandomValues` (#13470,
2024-08), and three orphan "DOMJIT fast path" comments in
`src/runtime/webcore/Crypto.rs` whose functions #35002 deleted.

**Self-review, and what changed because of it**
- The first draft mixed this design call with 22 log scopes and seven
fields. It now ships alone, with its history in the body.
- The leftover DOMJIT notes (`Crypto.rs`, the commented-out blocks) are
folded in.
- Three deletions that open PRs carry were dropped (#40232, #41385).
- Five items that open PRs use were taken out of the held branch
(#43283, #31855, #42819, #39222, #37518). Two `builtins.d.ts` lines were
dropped too: `src/codegen/replacements.ts` defines
`$ImportKindLabelToId`, so that declaration is live.

**History**
- #13470 (2024-08) turned DOMJIT off for `getRandomValues`. #14005
(2024-09) added the strip in `define()` as the repair for the #14001
segfault. #35224 found the cause (the wrappers returned `{ result }`
with a null exception slot) and tried to repair the generated wrappers.
A stale-PR cleanup closed it with no maintainer comment. #35002 deleted
the Rust fast paths, so the option cannot come back without new native
code.

**Kept on purpose**
- The hand-written `DomCall` path for `bun:ffi` (`src/jsc/host_fn.rs`,
`src/runtime/ffi`), the C++ DOMJIT signatures in `JSBuffer.cpp`,
`JSPerformance.cpp`, `NodeVM.cpp`, `JSSQLStatement.cpp`, and
`test/js/bun/jsc/domjit.test.ts`.

**Tests (debug build)**
- `test/js/web/encoding/text-decoder.test.js` 127 pass,
`test/js/web/web-globals.test.js` 23 pass,
`test/js/bun/util/randomUUIDv5.test.ts` 40 pass,
`test/js/bun/websocket/websocket-server.test.ts -t sendBinary` 5 pass.
- `websocket-server.test.ts -t "publish|send"`: 44 pass, 4 time out near
19 s under debug+ASAN. A debug binary built from main fails the same 4.
- `test/js/bun/jsc/domjit.test.ts`: 40 pass, 10 time out at the
100k-iteration sizes. A debug binary built from main gives the same
40/10.

**The rest of this sweep**
- Relink of the debug build with `-Wl,--gc-sections`, then the DWARF
line table of the result: 2,290 of 31,118 Rust `fn`s have no live line.
After `cargo check` on six targets only three `pub fn`s had no caller
anywhere. The 96 trait impls with no caller are the ones #43664 kept on
purpose.
- clang `-fsyntax-only -Wunused-function -Wunused-template
-Wunused-member-function -Wunused-macros` over bun's 177 C/C++
translation units (the build passes `-Wno-unused-function`): ten hits.
Open PRs delete them, or an `#if` uses them.
- oxlint `no-unused-vars` over `src/js`, `src/codegen`, `scripts`,
`packages`. cargo's `unused_dependencies` lint over four targets. A scan
for commented-out blocks (62 lines in the repo). Nothing new that is
certain.
- Each deletion was compared with the diffs of the 36 open dead-code
PRs. Left out because an open PR has it: `Bun__napi_get_version`
(#40232), two unused generator locals (#41385).

**Verified and held for the next run** (branch
`robobun/9a0817f5/dead-code-scopes-fields`, 25 files, 81 lines removed)
- 20 `declare_scope!` scopes that nothing logs to: `JSC`, `STR`,
`Bundle` and `scan_counter` (outer pair), `Store`, `hot_reloader`,
`CLI`, `LibUVBackend`, `ResolveInfoRequest`, `GetHostByAddrInfoRequest`,
`CAresNameInfo`, `GetNameInfoRequest`, `CAresReverse`, `CAresLookup`,
`quic_session`, `PathWatcherManager`, `S3Client`, `S3Stat`, `AWS`, `uws`
(`uws_sys/socket.rs`). rustc does not lint an item that another crate's
macro expands.
- Fields: `Runtime::Features.jsx_optimization_inline` with the local
`can_be_inlined`, `DebugOptions.output_file`, `ArchiveIterator.filter`,
`PackageManager.total_scripts`, `CommandLineArguments.lockfile`,
`ArgumentsSlice::_vm`. Also `struct_Channeldata` and two empty modules
in `napi_body.rs`.
- It passed `rust:check-all` (12 targets), release and `--cfg bun_debug
--cfg bun_asan` checks on linux, windows and darwin, and `cargo check
--tests` before the trim below. The trimmed commit passes `cargo check`
on linux.
- Taken out because an open PR uses the item:
`BundleOptions.code_coverage` (#43283 reads it), scope `ModuleLoader`
(#31855), scope `PROCESS` (#42819, #39222), `impl Clone for JsPoster`
and its vtable slot (#37518 rewrites the vtable).

**Follow-up candidates, not verified dead**
- `Parser Options.preserve_unused_imports_ts` is never `true`. tsconfig
`importsNotUsedAsValues` is parsed into
`preserve_imports_not_used_as_values` but never reaches the parser, in
the released binary too. This looks like a missing feature.
- `completions/bun-cli.json` (4,513 lines) and
`misctools/generate-cli-completions.ts` (728 lines): nothing in the repo
reads the JSON, but feature PRs still edit it by hand.
- `bench/snippets/runner-entrypoint.js` (244 lines): no reference, first
line says "this isn't done yet", last real change 2023-05.
- Ten `impl_timer_owner!` accessors have no caller because `dispatch.rs`
recovers the owner with its own `owner!` macro. Which mechanism stays is
a design call.
- `mordant-baseline.toml` still counts about 170 `unused_pub` findings
(`sys/lib.rs` 56, `libuv_sys/libuv.rs` 41, `errno/windows_errno.rs` 31).
`bun run rust:mordant` names them.

</details>
@github-actions

Copy link
Copy Markdown
Contributor

Closing this PR because it has been inactive for more than 90 days.

@github-actions github-actions Bot closed this Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants