Repository navigation
Remove 146 unsafe blocks from the dev server and the Bun.* APIs - #47393
Merged
Merged
Conversation
`render_json` took `&mut DevServer` and never used it. To pass it, the `.js.map` handler turned its borrow of the entry into a raw pointer and back. Without the parameter the handler borrows the entry. `finalize_bundle` renders a temporary `Entry`, a local, and had a guard that reached it through a raw pointer to clear `files` and `paths` at the end of the block. The local is dropped at the same brace, which frees the same two fields, so the guard is gone, and with it `Entry::deinit`, which nothing else called. `deinit` asserted that `ref_count` is 0. Two writes of that field were only there for the assert: the 1 in the temporary entry, and the zeroing of every entry in `Drop for DevServer`. Nothing reads either, so both are gone. The loop in `drop` keeps its own assert. Co-authored-by: dylanc <dylanc@anthropic.com>
The allocator was allocated in the bundle's arena and kept as a raw pointer. Because an arena value never runs `Drop`, a hand-written guard released its `AstAllocState` through that pointer, and entering the scope and taking the state went through it too. Nothing needs the allocator after the function returns: the nodes live in the arena and in the `AstAllocState`, and leaving the scope restores the thread-local pointer. It is a local now, like at the other callers of `borrowing`, and its own `Drop` releases the state on the error paths. `release_ast_state` has no caller left and is removed. `BundleV2::init`'s `alloc` parameter takes any lifetime, so it gets `&heap`. Co-authored-by: dylanc <dylanc@anthropic.com>
The slot that caches a route's bundle index is either a plain field of the framework route or the `Cell` in the `HTMLBundleRoute`. To give both one type, the function turned them into `*mut Option<Index>` and read and wrote through that. `Cell::from_mut` gives the plain field the type the other one already has. The borrow of `self.router` lasts until the final store, and the code in between only touches `graph_safety_lock`, `client_graph` and `route_bundles`. Co-authored-by: dylanc <dylanc@anthropic.com>
The three columns came from one `Slice`, whose `items_mut` borrows all of it, so each column was turned into a raw slice pointer and back. The two columns that are only read now come from `watchlist` itself, which leaves the `Slice` free for the `count` column. `flush_evictions` ran in a `defer!` that reached `self` through a raw pointer. Nothing after it leaves the function early, so it is the last statement, still before the guard that submits the event. Co-authored-by: dylanc <dylanc@anthropic.com>
The loop copied the dependency's `specifier` out as a raw pointer so that no borrow of `dev` was held during the call to the resolver. The dependency lives in `dev.directory_watchers` and the resolver in `dev.server_transpiler`, two different fields, so a plain borrow of the dependency can stay alive for the call. Co-authored-by: dylanc <dylanc@anthropic.com>
`arena.alloc` returns `&'a mut Framework`. Both callers turned it into a raw pointer at once, because `Drop for DevServer` needs one, and then dereferenced that pointer to get the `&'a Framework` for the transpiler options. They now keep the reference, take the raw pointer from it and let the reference coerce to the shared one. Co-authored-by: dylanc <dylanc@anthropic.com>
`on_open` and the console-log message only read the dev server, but got it from `dev()`, the `unsafe fn` that hands out `&mut DevServer`. The field is a `BackRef`, which derefs to `&DevServer`. A copy of it in a local leaves `self` free for the assignments in between. Co-authored-by: dylanc <dylanc@anthropic.com>
The caller unwrapped `pt.vm` to a raw pointer, and `init` wrapped that pointer in a `BackRef` again with `from_raw_mut`. It now takes the `BackRef`. Co-authored-by: dylanc <dylanc@anthropic.com>
…ions
The body of `emit_memory_visualizer_message_if_needed` is `{}`. Its only
two callers, both in `sweep_weak_refs`, went from the `SourceMapStore` to
the `DevServer` that contains it, and so formed a `&mut DevServer` over the
live `&mut SourceMapStore`, to call it.
`emit_visualizer_message_if_needed` is just as empty, so it goes too, with
its two calls.
Co-authored-by: dylanc <dylanc@anthropic.com>
Two `defer!`s at the top of `finalize_bundle` drained the bundle's requests and ran `finalize_bundle_cleanup` on every exit. Each had to reach `dev`, `bv2` and the current bundle through raw pointers, because a closure that borrowed them would have locked them for the whole body. The body is now `finalize_bundle_inner`, and `finalize_bundle` calls it, then the cleanup, then returns its result. The cleanup already matches on `dev.current_bundle`, so it drains the requests in the `Some` arm, before `bv2.deinit_without_freeing_arena()` as before. Co-authored-by: dylanc <dylanc@anthropic.com>
… `dev_ptr` Four places reached a second field of the dev server through a raw copy of `dev`, because the first borrow covered all of it. Two more kept the inspector agent as a raw pointer: - `route_bundle_ptr` borrows all of `dev`. Indexing `dev.route_bundles` leaves `dev.source_maps` free for `invalidate_client_bundle`. - The `Entry` from `put_or_increment_ref_count` borrows only `dev.source_maps`, so `dev.client_graph.take_source_map(entry)` compiles as it is. - `make_array_for_server_components_patch` and `send_serialized_failures` took `&mut self` but only read. With `&self` they can be given slices of `dev.incremental_result` and `dev.bundling_failures`. - `inspector()` ties the agent's lifetime to `&self`, although the agent lives in the VM's debugger. `BunFrontendDevServerAgent::of(vm)` holds the same body and `inspector()` forwards to it. Here the agent is borrowed from a copy of `dev.vm`, so it can be kept across the loop that mutates `dev`. The guard that takes `graph_safety_lock` again before the function returns is the only user of `dev_ptr` left, so the pointer is taken right above it. Co-authored-by: dylanc <dylanc@anthropic.com>
… slot The agent was a `#[repr(transparent)]` struct around `ErasedAgentSlot`, and `from_slot` cast `&ErasedAgentSlot` to `&Self`. It is only ever a view of the slot, so it now holds `&'a ErasedAgentSlot`, is `Copy`, and its methods take `self`. `Option<BunFrontendDevServerAgent<'_>>` is one nullable pointer, as `Option<&BunFrontendDevServerAgent>` was. Co-authored-by: dylanc <dylanc@anthropic.com>
…request The context kept `dev` as a raw pointer, and `ensure_route_is_bundled`, which holds `&mut DevServer` itself, called methods on the context that made a second `&mut DevServer` from it. `on_defer` and `on_loaded` now take `dev` as a parameter. `on_defer` also picked the request list inside `dev` and handed it to `defer_request` next to `&mut self`. `defer_request` now takes the `BundleQueueType` and picks the list itself, right before it links the node. `ReqOrSaved::Req` held `*mut Request`, although both places that build a context have `&mut Request`. It holds that reference now, which `SavedRequestUnion::Stack` and `prepare_and_save_js_request_context` take as it is. Co-authored-by: dylanc <dylanc@anthropic.com>
To print an initial response, `take_js_bundle_to_list` went from the client graph to the `DevServer` that contains it and read `root`, through `relative_path`, and `configuration_hash_key` from there. The step to the container needs `&mut self`, which is the only reason that both functions took it, and because of it `generate_client_bundle` had to copy the entry point's path out of the graph before the call. Both values are now fields of `TakeJSBundleOptionsClient`, the functions take `&self`, and the entry point's path is borrowed from the graph. The body of `DevServer::relative_path` moves to a free function that takes `root`, and the method forwards to it. Co-authored-by: dylanc <dylanc@anthropic.com>
`trace_all_route_imports` and `generate_client_bundle` took `&mut self` and a `&RouteBundle` that lives inside `self`, so every caller turned one of the two into a raw pointer. All that they read from the route bundle is `Copy`: the route index or the HTML file's index, and the source map id. `trace_all_route_imports` now takes the `RouteIndex` of a framework route. For an HTML route it only forwarded to `client_graph.trace_imports`, which the callers now call themselves, in the `match` on the route's data that used to be inside. `generate_client_bundle` takes a `TraceRoot`, a `Copy` enum of the two indices, and the source map id. `on_js_request_with_bundle` is folded into `serve_client_script`, its only caller, which has already looked the route bundle up. The `TraceRoot` is built only where the bundle has to be built, so a request for a cached bundle does what it did before, with one bounds check less. In `generate_client_bundle` the guard that resets the client graph owns `self`, and the one that released the source map on failure is an `if let Err`. Co-authored-by: dylanc <dylanc@anthropic.com>
It was an `unsafe fn` over `*mut Self` that made its accesses in 17 `unsafe` blocks, and was also handed a `&mut Framework` that points into `self.route_bundles`. Its last arm read that same route bundle again while the `&mut` was live. What it touches are separate fields: `route_bundles`, `router`, `server_graph` and `root`. With `&mut self` each is borrowed on its own. The one call that needs all of `self`, `generate_css_js_array`, now takes the route index, so no borrow is alive across it, and the result is stored through a fresh lookup. `client_file` is read before the borrow of the router type ends, and `global` comes from the caller, which has it. The function looks the route bundle up itself, so `on_framework_request_with_bundle` no longer does. `Data::framework` has no caller left. Co-authored-by: dylanc <dylanc@anthropic.com>
… not `MaybeUninit` The three transpiler fields were `MaybeUninit`, so every use after `init()` needed `assume_init_*`. But `init()` builds the whole struct under `Box<MaybeUninit<DevServer>>` and only calls `assume_init()` on it after the transpilers are written, so no `DevServer` with an unwritten server or client transpiler ever exists. Those two are `ManuallyDrop` and not plain fields to keep the point where they are dropped: inside `Drop for DevServer`, before the `bundler_framework_views` that their options borrow are dropped in place. Plain fields would be dropped after `drop` returns. The order in `drop` is unchanged. Their four accessors are gone, and the callers use the fields. `ssr_transpiler` stays `MaybeUninit`. Without a separate SSR graph it is a bitwise copy of `server_transpiler` and holds no value of its own. Its accessor, its uses under `separate_ssr_graph` and the check in `drop` are as they were. `start_async_bundle` gives the bundler a pointer to the slot with `NonNull::from(..).cast()`, which claims nothing about its contents. With that, the rest of `init()` can borrow fields through the `Box` instead of through `dev_ptr`: `framework.resolve` with the two resolvers, and the loop over the router types. `scan_initial_routes` passed `&mut *self_ptr` as the insertion handler next to `&mut self.router`. The handler only needs `root`, `server_graph` and `route_lookup`, so `DevRouteScan` borrows those three, like `RouteScan` of the production build and `JSFrameworkRouterScanCtx`. Co-authored-by: dylanc <dylanc@anthropic.com>
It took `&mut self` and a `&RouteBundle` inside `self`, so `on_html_request_with_bundle` kept both `self` and the route bundle as raw pointers and dereferenced them for each step. The function only uses the graph lock and the two graphs, none of which overlaps `route_bundles`. It takes those three, in the way `invalidate_client_bundle` takes the `SourceMapStore`, and the caller holds one `&mut RouteBundle` from the lookup to the store of the response. `GraphTraceState::new` holds the body of `init_graph_trace_state`, which needs all of `&self`, and that method forwards to it. Co-authored-by: dylanc <dylanc@anthropic.com>
`init_transpiler` fills a `MaybeUninit` slot. `MaybeUninit::write` gives it a `&mut Transpiler` to the result, which it dropped at the end, so the production build called `assume_init_mut` on each slot afterwards, for the SSR slot at every use. It now returns that reference. The production build keeps the three references. The SSR one is an `Option`, which replaces the tests of `separate_ssr_graph` at its uses. The bundler wants `&'a mut Transpiler<'a>`, which borrows the slot for as long as the arena. That is fine here, because nothing uses the slots after that call, so the reference is passed as it is instead of through a raw pointer, and the other two become `NonNull::from(..)` without a null check. Co-authored-by: dylanc <dylanc@anthropic.com>
`replace_path` went from the `Assets` to the `DevServer` that contains it, to read `magic` and `server` and to register the path in `client_graph`. Both callers hold `&mut DevServer` with nothing of it borrowed, so the function is a method of `DevServer` that starts by destructuring the three fields it works on. It takes the same arguments as before, and `Assets` does not recover its parent from its own address any more. Co-authored-by: dylanc <dylanc@anthropic.com>
…nstead of going through a raw pointer
`configure_bundler` takes `&mut Transpiler<'a>` with a borrow of its own, so
passing the `&'a mut Transpiler<'a>` from `bump.alloc` is an implicit reborrow
that ends when the call returns. The transpiler lives in `bump`, not in `self`,
so nothing conflicts with `&mut self`. The raw pointer and the two
`unsafe { &mut *tp }` are gone; the reference is passed and then returned.
Co-authored-by: dylanc <dylanc@anthropic.com>
`init` takes `impl IntoIterObject`, which is implemented for `*mut JSObject`,
and stores the raw pointer. Four callers in `JSTranspiler` and `JSBundler`
turned the pointer from `get_object()` / `get_own_object()` into a `&JSObject`
with `unsafe { &*obj }` only to hand it to `init`, which turned it back. They
pass the pointer as the other callers do.
Co-authored-by: dylanc <dylanc@anthropic.com>
…ry call `scan`, `transformSync` and `scanImports` pointed the long-lived `Transpiler` at their per-call arena, and a guard put the old one back. The `transform` task did the same to its own bitwise copy of the `Transpiler`, with nothing to put back. Each took a `detach_lifetime_ref` to `'static`. Nothing they call reads that field: its readers are `configure_defines`, `build_with_resolve_result_eager` and `build_css_output`, and `parse`, `print`, `MacroContext::init` and `cache::JavaScript::scan` reach none of them. `parse` and `print` take the arena as an argument with a lifetime of its own. The swap, the saved `prev_arena` and the four lifetime erasures are gone, and `&arena` is passed as it is. Co-authored-by: dylanc <dylanc@anthropic.com>
…tching again with `unreachable_unchecked`
Four places tested an enum with `matches!` and then matched it a second time
with `_ => unsafe { unreachable_unchecked() }` to get at the payload.
- The sourcemap loop in `do_compilation` binds `bytes` in the condition.
- `do_compilation` took `&mut self`, so `on_complete` had to move
`output_files` out of `self.result` and put it back. It only uses `config`,
`env` and `log`, so it takes those, and the caller lends it
`&mut build.output_files` next to them: four disjoint fields.
- `on_complete` handles `Pending` and `Err` as arms of the one match. No loan
of `this.result` is live in the `Err` arm, so `to_js_error` can borrow `this`.
Co-authored-by: dylanc <dylanc@anthropic.com>
… already there The PNG and WebP encoders tested the codec's buffer for null and then made `Encoded.bytes` with `NonNull::new_unchecked`, or with a `&mut [u8]` from `from_raw_parts_mut` that was only used for its pointer and length. `NonNull::new` is that same null test, and `NonNull::slice_from_raw_parts` (as in `Encoded::from_owned`) adds the length without claiming a reference. `WebPChunkIterator` is written out as a literal of zeroes instead of `MaybeUninit::zeroed().assume_init()`; it has no padding. The `Blob` arm of `PipelineTask::then` drops the `Encoded`, as the base64 arm does, instead of calling its `free` by hand: `Encoded::drop` makes the same call. Co-authored-by: dylanc <dylanc@anthropic.com>
`EncodeOptions.icc_profile` was an `Option<NonNull<[u8]>>` standing in for a
borrow that only has to last for one `encode()` call. It had one writer, right
before that call, and one reader, `unsafe { p.as_ref() }` at the top of
`encode()`; no other code set it, so the `is_none()` test in front of the write
was always true, and both system backends ignore it.
The field is gone and the one caller passes `decoded.icc_profile.as_deref()`
as an argument.
Co-authored-by: dylanc <dylanc@anthropic.com>
…start
`open` boxed the `Terminal`, leaked it with `heap::into_raw`, worked through
`unsafe { &*terminal }`, called `deref_()` by hand on each failure path and
wrapped the pointer with `unsafe { RefPtr::from_raw(..) }` at the end.
It returns a `RefPtr<Terminal>` anyway, so it makes one with `RefPtr::new`,
which is the same allocation. Every method it calls takes `&self`, which the
`RefPtr` derefs to. Returning early drops the `RefPtr`, which releases the
initial ref where the explicit `deref_()` did, so the one in the writer path
and the one in `fail_reader_start` (called only from `open`) are gone. The
reader's own ref is still released by hand.
Co-authored-by: dylanc <dylanc@anthropic.com>
…nspiler::print` takes `&self` - `TransformTask::run` kept a raw pointer to `self.transpiler.macro_context` in a scopeguard while it went on using `self.transpiler`. The guard holds `&mut *self.transpiler` and the body works through the guard; the task's other fields are disjoint places. - `TranspilerStateGuard` kept `JsCell::as_ptr()` and turned it into a `&mut` in its `Drop`. It borrows the `JsCell` for its own lifetime and restores the state with `with_mut`, the way the callers set that state up. - The six `print*` functions of `Transpiler` only read `self.options`, so they take `&self`, and `transformSync` prints through `JsCell::get()` instead of the unsafe `transpiler_mut()`. Co-authored-by: dylanc <dylanc@anthropic.com>
… access they have The constructor owns a fresh `Box<JSTranspiler>` and still went through the unsafe `JsCell::get_mut(&self)` for `config` and `transpiler`. `JsCell` gets `as_mut(&mut self)`, which is `UnsafeCell::get_mut` and needs no `unsafe`, and the constructor borrows the two fields with it. `Drop` called `clear_and_free()` through the raw `transpiler.log`. That pointer is `&self.config.log` whenever a `JSTranspiler` is dropped: the constructor sets it right after boxing, before anything can return, and the guard in `scan`, `transformSync` and `scanImports` puts it back on every exit. Dropping the `config` field frees the same messages, so the call is gone. Co-authored-by: dylanc <dylanc@anthropic.com>
…`new_unchecked`
Three callers had a non-null pointer by type, passed it on as `*mut T`, and the
receiver rebuilt the `NonNull` with `unsafe { NonNull::new_unchecked(..) }`.
- `CompletionDispatch::enqueue_task_concurrent` takes
`NonNull<ConcurrentTask>`: the bundler holds one and `Ticket::post` wants one.
- `Subprocess::on_process_exit` takes `NonNull<Process>`, made from the
`&mut Process` that the exit interface hands the thunk. Its only use is
`VirtualMachine::on_subprocess_exit`, which takes a `NonNull`.
- `CronJob::tick_settled` gets the `NonNull` inside its `ThisPtr` through the
existing `BackRef` conversions, as `html_rewriter` does for `deref_nn`.
Co-authored-by: dylanc <dylanc@anthropic.com>
robobun
added a commit
that referenced
this pull request
Oct 29, 2026
…le-script One conflict, in src/runtime/bake/dev_server/router_glue.rs. On main, #47393 ended `impl DevServer` after `client_page_files` and moved the `InsertionHandler` functions to the new `DevRouteScan`. This branch added `route_of_client_page_file` and `mark_client_page_routes_above` at that place. Keep both: the two functions stay in `impl DevServer`, and `DevRouteScan` follows it.
robobun
added a commit
that referenced
this pull request
Oct 29, 2026
Two conflicts, both in src/runtime/bake/dev_server/render.rs. On main, #47393 changed `generate_client_bundle`: it takes the root to trace from and the source map key, and no longer the `RouteBundle`. This branch passes the route bundle index to it and sets `html_route_index` for an HTML route. Keep both: the function takes the index beside main's two arguments, and tests `root` for an HTML route where it tested `route_bundle.data`.
robobun
added a commit
that referenced
this pull request
Oct 29, 2026
…-production-entry-point-index One conflict, in `build_with_vm` of src/runtime/bake/production/mod.rs, at the call of `generate_from_bake_production_cli`. On main, #47393 passes `server_transpiler` there, a reference and no longer a raw pointer. This branch passes the entry points as `&mut`. Keep both.
This was referenced Oct 29, 2026
Jarred-Sumner
added a commit
that referenced
this pull request
Oct 31, 2026
Two commits of main: the WebKit upgrade (#47112) and the removal of 146 unsafe blocks (#47393). Conflicts: - scripts/build/deps/webkit.ts keeps this branch's pin. Its engine is based on a WebKit main that already has that upgrade. - 375 files under test/aot/engine-tests keep this branch's text: they hold the compiler's output, and follow this branch's engine. Four that this branch had removed stay removed. - test/bundler/bundler_bytecode_portable.test.ts keeps this branch's hashes, which depend on the engine. - test/bundler/compile-aot-size.test.ts has main's bound for trimmed executables and this branch's bound for the compiler.
dylan-conway
added a commit
that referenced
this pull request
Nov 3, 2026
… package manager (#47414) ### What does this PR do? 50 separate changes, one commit each: 16 in the core crates (`bun_core`, `bun_collections`, the TOML and XML parsers, the crash handler), 16 in the resolver and the code next to it (the HTTP client's setup, source maps, the standalone module graph), and 18 in the package manager. Two more commits answer the `mordant` lint job: 51 takes back a part of change 5, and 52 moves a `match` that change 33 left in two places into a method. Nothing is left of commits 53 and 54: they changed three test files so that two of `main`'s tests pass, #47316 has since rewritten those files on `main`, and the merge of `main` takes `main`'s versions. So this PR changes nothing outside `src/`. Commit 56 answers a review comment: `read_directory` and its siblings return a shared reference. The changes follow #47291, #47317, #47338, #47382 and #47393 and work the same way: signatures name the type and the lifetime that the value already has, hand-written pointer code becomes plain ownership and borrows that the compiler checks, and what was never needed is deleted. Together: **126 fewer `unsafe` blocks and 8 fewer `unsafe fn`s**, +877/−1651 over 73 files. No safe function became `unsafe`, and no wrapper type was added. **[Where the compiled code changes](#where-the-compiled-code-changes) lists every place where the program does different work.** | # | Change | `unsafe` | Diff | |---|---|---|---| | 1 | toml: the parser keeps tables and arrays as `StoreRef` | −6 blocks | +43/−64, 1 file | | 2 | bun_core: remove `boxed_zeroed_unchecked`, `ZStr::boxed` and `OptionsEnvArg for Box<ZStr>` | −3 blocks, −1 `unsafe fn` | +3/−50, 4 files | | 3 | xml: `Unit::from_u16` and `Unit::from_bytes` instead of casts | −3 blocks | +20/−22, 1 file | | 4 | crash_handler: `UNSUPPORTED_UV_FUNCTION` holds the name's bytes | −2 blocks | +6/−13, 1 file | | 5 | xml_index: `StructuralIndex::seek` and `at` read the window with `get` | −2 blocks | +14/−10, 1 file | | 6 | collections: `HiveSlot` borrows its hive | −2 blocks, −1 `unsafe fn` | +28/−60, 1 file | | 7 | bun_core: `vec::extend_from_fn` is `Vec::extend` | −1 block | +5/−28, 2 files | | 8 | fmt: the shared temp buffer is an `Option<Box>` | −3 blocks | +65/−119, 1 file | | 9 | SmolStr: `Inlined::all_chars` views the `u128` with `bytes_of_mut` | −1 block | +2/−11, 1 file | | 10 | bun_core: `impl io::Write for Writer` calls the inherent methods | −2 blocks | +2/−6, 1 file | | 11 | atomic_cell: `unreachable!()` for the widths that `Atom` excludes | −1 block | +2/−3, 1 file | | 12 | highway: `memmem` subtracts addresses | −1 block | +2/−2, 1 file | | 13 | Check constant strings at compile time instead of `from_utf8_unchecked` | −2 blocks | +17/−40, 4 files | | 14 | Call `gettid`, `getpid`, `alarm`, `_exit` and `raise` as `safe fn` | −7 blocks, +3 `unsafe extern` blocks | +25/−24, 5 files | | 15 | bun_core: `fd_path_raw_w` takes `&mut [u16]` | −5 blocks, −1 `unsafe fn` | +17/−30, 2 files | | 16 | output: `STDOUT_STREAM` and `STDERR_STREAM` are atomics | −3 blocks | +21/−13, 1 file | | 17 | resolver: `BrowserMapPath::map` is `&'static BrowserMap` | −4 blocks | +5/−12, 1 file | | 18 | resolver: keep the slot that `MaybeUninit::write` returns | −2 blocks | +4/−8, 1 file | | 19 | resolver: drop three lifetime casts that change nothing | −3 blocks | +7/−14, 2 files | | 20 | resolver: `RealFS::read_directory` and `EntryLookup` keep their `'static` | −3 blocks | +21/−55, 4 files | | 21 | http: `h3::ClientContext::get_or_create` reads the loop itself | −2 blocks | +6/−14, 2 files | | 22 | boringssl: `ssl_ctx_setup` takes `&OwnedSslCtx` | −2 blocks, −1 `unsafe fn` | +11/−22, 2 files | | 23 | sourcemap: `impl_pod!` for `SyncEntry` and `ObjectBrace` | −3 blocks | +9/−20, 1 file | | 24 | sourcemap: `WindowReader<'a>` borrows the stream | −1 block | +32/−40, 1 file | | 25 | resolver: four `unsafe` blocks that plain borrows replace | −4 blocks | +18/−38, 4 files | | 26 | resolver: `package_json` travels as `&'static PackageJSON` | −3 blocks | +30/−77, 6 files | | 27 | resolver: `load_tsconfig` keeps its boxes | −4 blocks | +16/−37, 1 file | | 28 | resolver: `BIN_FOLDERS` is initialised at compile time | −2 blocks | +17/−39, 2 files | | 29 | standalone_graph: in `inject`, `zname` borrows `zname_owned` | −2 blocks | +3/−12, 1 file | | 30 | sourcemap: `ParsedSourceMap` holds the standalone graph's sources by type | −1 block | +22/−62, 4 files | | 31 | http: `InitOpts::abs_ca_file_name` is a `&'static ZStr` | −1 block | +14/−36, 3 files | | 32 | exe_format: `_mh_execute_header` is a `safe static` | −1 block | +3/−3, 1 file | | 33 | install: `init` reborrows the `&'static mut DirEntry` it matched on | −2 blocks | +2/−9, 1 file | | 34 | install: `save_lockfile` uses `progress.root` instead of a stashed node pointer | −2 blocks | +2/−15, 1 file | | 35 | install: drop four raw-pointer and lifetime detours that borrowck does not need | −4 blocks | +16/−44, 3 files | | 36 | install: `find_workspace_root_above` borrows `walk.path` for the source path | −1 block | +2/−9, 1 file | | 37 | install: `AutoInstaller::path_for_resolution` takes `&mut PathBuffer` | −1 block | +3/−15, 3 files | | 38 | install: `NetworkTask` reads its manager through the `ParentRef` it stores | −1 block | +7/−22, 1 file | | 39 | install: remove three one-line `unsafe` blocks that have a safe spelling | −3 blocks | +10/−19, 3 files | | 40 | install: clear `root_lifecycle_scripts` with a `scopeguard` on `manager` | −1 block | +3/−10, 1 file | | 41 | semver: delete `query::Group.input` | −1 block | +22/−37, 3 files | | 42 | install: `UpdateRequest.e_string` keeps the `StoreRef` it is made from | −5 blocks | +16/−41, 3 files | | 43 | install: `TarballStream::init` returns a `Box` and `populate_result` is a safe fn | −4 blocks, −2 `unsafe fn` | +149/−202, 4 files | | 44 | install: the Windows hardlink task is passed and reclaimed as a `Box` | −5 blocks, −1 `unsafe fn` | +22/−32, 1 file | | 45 | install: the Windows `Hardlinker` keeps the cwd slice it gets | −1 block | +4/−25, 1 file | | 46 | install: `ROOT_PACKAGE_JSON_PATH` becomes `PackageManager::root_package_json_path` | −8 blocks | +40/−36, 8 files | | 47 | install: `Diff::generate` takes `to_update: bool` instead of the update requests | −1 block | +24/−28, 4 files | | 48 | install: `Package::parse_from_real_manager` takes `&mut PackageManager` | −1 block, −1 `unsafe fn` | +13/−35, 2 files | | 49 | install: in `init`, `original_cwd` borrows the boxed copy of the cwd | −1 block | +3/−9, 1 file | | 50 | install: remove two pieces of dead code that needed `unsafe` | −2 blocks | +29/−61, 2 files | | 51 | xml_index: `seek_refill` calls `at` for every index, as it did | 0 | +4/−8, 1 file | | 52 | resolver: `EntriesOption::try_entries_mut` returns the listing or the read error | 0 | +14/−8, 2 files | | 53 | test: `custom-accessor-reached-computed-key.ir` allows one more member in eleven types | 0 | nothing, since the merge | | 54 | test: record the growth in memory of one shape of build-scaling, as #47329 does | 0 | nothing, since the merge | | 55 | the merge of `main` | | | | 56 | resolver: `read_directory` and its siblings return `&'static EntriesOption` | 0 | +21/−21, 5 files | The commits can be reviewed one at a time. Worth knowing before reading on: - **Nothing that users can see changes**, so the PR adds no test. One leak ends, on a path that hardly ever runs: a `tsconfig.json` chain of more than 64 `extends` frees the configs it has read (change 27). - **Five changes end an aliasing or type bug that had no visible effect.** - Change 30: the standalone graph stored a pointer to one struct as an integer, and the reader cast it to another `repr(Rust)` struct with the same fields. - Change 40: a guard wrote through a raw pointer to the `PackageManager` that was stale by the time it ran. - Change 47: with `--frozen-lockfile` and for `bun pm`, `parse_with_json` made a `&Lockfile` while a `&mut` to the same lockfile was live. - Change 49: `original_cwd` pointed into a buffer that was overwritten while it was still in use. Nothing read the overwritten bytes. - Change 20: a `&mut Entry` could overlap a `&'static Entry`. The store it was made for wrote the value that was already there, so the store is gone. - **Change 14 adds three `unsafe extern "C"` blocks.** They hold `safe fn` declarations of `gettid`, `alarm`, `_exit` and `raise`, next to their callers, as the neighbouring code does for other libc functions. - **New functions, all without `unsafe`:** `Unit::from_u16` and `Unit::from_bytes` (change 3), `with_shared_temp_buffer` (8), `WindowReader::new` (24), the `const fn BoundedArray::new` (28) and `EntriesOption::try_entries_mut` (52). - **Seven changes are compiled only for Windows or macOS, in whole or in part:** 15, 44 and 45 (Windows), 32 (macOS), and single lines of 16, 29 and 43. - **Three structs get smaller, and no file format changes:** `semver::query::Group`, and with it every `Dependency`, by 16 bytes (41), `resolver::Result` and `MatchResult` by 8 (26), and `UpdateRequest` by 8 (42). All of them hold pointers and exist only in memory. - `bun.lockb` stores a `Dependency` as the 26 bytes of `dependency::to_external`: name, name hash, behavior, tag and the literal of the version. The range is not stored. `to_dependency` parses it from the literal again on load. - `write_array` and `read_array` take only `NoUninit` and `AnyBitPattern` types, which `Dependency` and `Group` are not. - `padding_checker.rs` and `bun.lockb.rs` assert at compile time the size and alignment of the structs that are written as they are in memory: `Resolution`, `Meta`, `Bin`, `Scripts`, `PatchedDep`, `semver::Version`, `String` and `ExternalString` among others, and `NpmPackage` and `PackageVersion` of the manifest cache. This PR touches neither those files nor those types. ### Where the compiled code changes I compared the release assembly (x86-64 Linux, `--emit=asm`) of 21 crates, before and after, function by function, with names demangled and labels normalised: the 18 crates that this PR touches, and `bun_jsc`, `bun_io` and `bun_shell_parser`, which use what changes in `bun_core`, `bun_collections` and the resolver. | Crate | Functions | Differ | Other constants or names only | Same length | Shorter | Longer | Instructions | Branches | Compares | Calls | Panic call sites | |---|--:|--:|--:|--:|--:|--:|--:|--:|--:|--:|--:| | `bun_collections`, `bun_highway`, `bun_paths`, `bun_exe_format`, `bun_lint`, `bun_io` | 4,265 | 0 | | | | | | | | | | | `bun_core` | 1,148 | 13 | 1 | 2 | 9 | 1 | −237 | −32 | −34 | −20 | −3 | | `bun_parsers` | 412 | 5 | 1 | 0 | 0 | 4 | +30 | +4 | +4 | +2 | +2 | | `bun_semver` | 70 | 12 | 6 | 2 | 4 | 0 | −18 | 0 | 0 | 0 | 0 | | `bun_crash_handler` | 120 | 2 | 0 | 0 | 2 | 0 | −8 | −2 | −1 | −2 | 0 | | `bun_boringssl` | 30 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | | `bun_sourcemap` | 104 | 9 | 0 | 4 | 1 | 4 | +14 | 0 | 0 | 0 | 0 | | `bun_install_types` | 34 | 5 | 5 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | | `bun_resolver` | 443 | 29 | 10 | 4 | 13 | 2 | −93 | −2 | −2 | +5 | −1 | | `bun_http` | 380 | 5 | 0 | 1 | 1 | 3 | 0 | 0 | 0 | +1 | +1 | | `bun_standalone_graph` | 208 | 3 | 0 | 0 | 2 | 1 | −12 | 0 | 0 | −1 | 0 | | `bun_install` | 2,819 | 667 | 561 | 45 | 34 | 27 | −165 | −39 | −50 | −16 | −13 | | `bun_bundler` | 1,983 | 25 | 12 | 2 | 5 | 6 | 0 | 0 | −1 | 0 | 0 | | `bun_shell_parser` | 121 | 1 | 0 | 0 | 0 | 1 | +2 | 0 | 0 | 0 | 0 | | `bun_jsc` | 1,707 | 10 | 6 | 1 | 3 | 0 | −6 | 0 | 0 | 0 | 0 | | `bun_runtime` | 10,459 | 98 | 78 | 4 | 4 | 12 | +72 | +10 | +9 | +6 | +2 | The table is from before the merge of `main` and before commit 56. Commit 56 changes no machine code: `.text`, `.rodata`, `.data`, `.data.rel.ro` and `.eh_frame` of a release build for x86-64 Linux are byte for byte the same with and without it. The last five columns are net, and count the functions that exist on one side only: 21 only before and 19 only after, which were removed, renamed or inlined, or are generic code with another type argument. **Most of what differs is the size of four structs.** `semver::query::Group` loses 16 bytes (change 41), so a `Dependency` goes from 240 to 224. `UpdateRequest` goes from 280 to 256 (41 and 42). `resolver::Result` goes from 296 to 288, and `MatchResult` loses 8 too (26). `PackageManager` gains the 16 bytes of its new field (46), which moves the fields behind it. That gives other offsets and strides (the fourth column), and where a stride changes, the compiler also multiplies differently (`imul $296` is now `lea` and `shl`) and shapes loops differently. Of the 106 functions of `bun_install` that differ in more than constants, 90 differ by at most 5 instructions and have no compare, branch or panic site more. So do all 13 of `bun_bundler` and all 4 of `bun_jsc`. **Checks.** One path makes a check that it did not make before: `seek_refill` of the XML index, once per refill of its window, which takes in 8,192 units of input. These are all the functions in which the listing shows more conditional branches, compares or panic call sites than before, and why: | Function | Listing | Why | |---|---|---| | `compute_color_depth` (`bun_core`) | 385 to 408, +6 branches, +6 compares | `index_of` got shorter (change 12), and the compiler now inlines it here. Runs once per process. | | `EncodedSlice::fmt`, `FormatUTF16::fmt` (`bun_core`) | 382 to 365 and 389 to 380, +1 panic site each | They called `SharedTempBufferBorrow::new` and its `drop`, which each looked the thread local up. What those two did is now inline (change 8), with the panic for a destroyed thread local that both contained. 5 branches fewer each. | | `WrappedWriter::write_16_bit` (`bun_runtime`) | 100 to 162, +9 branches, +9 compares, +6 calls, +3 panic sites | The same: the two functions that it called, of 35 and 38 instructions, are inline. It asks for the address of the thread local once instead of twice. | | `StructuralIndex::seek_refill`, for `u8` and `u16` (`bun_parsers`) | 35 to 36, +1 branch, +1 compare | After `fill_to` has returned, `at` compares the index with the length of the window again, where it read the element unchecked (changes 5 and 51). | | `load_node_modules` (`bun_resolver`) | 2,828 to 2,848, +3 branches, +3 compares | The compiler duplicates three tests at exits: twice the one in front of a `dealloc`, once the one of the debug log's indent. One per path, as before. | | `finalize_result`, `load_as_file_or_directory` (`bun_resolver`) | 735 to 712 (+1 branch, −3 compares, +1 call) and 564 to 559 (−1 branch, +1 compare) | `Option<&PackageJSON>` is one word where `Option<*const _>` was a tag and a pointer (change 26). In `finalize_result` the compiler duplicates one call of `BSSStringList::append`. | | `load_tsconfig` (`bun_resolver`) | 481 to 502, +1 compare, +5 calls, −1 panic site | The drops of the boxed configs (change 27): three call sites of their drop code and two of `dealloc`. An `unwrap` goes. | | `h3::ClientContext::get_or_create` (`bun_http`) | 74 to 84, +2 branches, +2 compares, +1 panic site | It reads the loop itself (change 21). The same two tests leave its caller `HTTPClient::start_::<true>` (940 to 928), and they now run only when the engine is created. | | `add_catalog::prepare`, one instance of `reject_unknown_update_requests`, `update_package_json_and_install_with_manager_with_updates` (`bun_install`) | 939 to 937 (+2, +2), 512 to 516 (+2, +2), 3,702 to 3,722 (+2 branches, −2 compares, +5 calls, +1 panic site) | Their source is not touched. They walk, copy and drop `UpdateRequest`s, whose size and field order change. The third has the same 25 allocations, and four more copies of the `dealloc` in its drop code. | | `package_json_editor::edit` (`bun_install`) | 3,005 to 3,003, +2 branches, −2 compares | `Option<StoreRef>` is one word where `Option<*mut _>` was two (change 42). | | Change | What differs | Effect | |---|---|---| | 1 | `toml::Parser::parse_keyval` goes from 275 to 282 and `TOML::parse` from 601 to 602: moves. | Same tree. | | 2 | `ZStr::boxed` and the two methods of `OptionsEnvArg for Box<ZStr>` go, 109 instructions. | Nothing called them. | | 3 | The two `structural_index_chunk` are still inline in their callers, which do not change. A copy of each (10 instructions) is now also emitted on its own, and nothing in the crate calls it. That is +20 of the +30 instructions of `bun_parsers`, and its 2 calls and 2 panic sites. | None. | | 4, 13, 14 | The crash handler goes from 825 to 819: two `strlen` calls and their branches go. `CrashHandler__unsupportedUVFunction` goes from 48 to 46, without the test for null. Because it is known not to return, `napi::uv_posix::unsupported` (6) is inlined in the six `uv_*` stubs that call it, without its `unreachable!`. One closure of the crash handler and one of `__runtime_embed_load` have another number in their names. | Only on the way to a crash. | | 7 | `extend_from_fn::<Dependency>` (69) and `::<u32>` (40) are `Vec::extend_trusted` (57 and 33). | One bounds check fewer per call. | | 8 | Apart from the three functions above: `format_utf16_type::<Formatter>` (150) is `with_shared_temp_buffer` with its closure (134). `SharedTempBufferBorrow::new` (35), its `drop` (38) and the slot's `drop` (9) go. The thread local's destructor goes from 10 to 8, and `bun_runtime` gets a copy of it (6). | Same text. After a nested use the inner buffer is freed, not the outer. | | 9 | **`SmolStr::from_char` (3 instructions) was inline in the shell's brace lexer, which is in other crates, and is a function that they call.** rustc offers a body to other crates by itself only while it makes no call, and this one now calls `unwrap` until LLVM folds that away. `braces::Token::to_text` goes from 37 to 39 and ends three arms in a jump to it. The two `tokenize` make one call more each (581 to 581 and 669 to 676). | One call per `{`, `}` or `,` token in this comparison, which does not link. The build that ships inlines across crates. | | 12 | In `index_of` (44 to 39) the second test of `haystack.len() < needle.len()` folds away. `SplitIterator::next` goes from 93 to 91 and `StringJoiner::push_node` from 82 to 81. `replace` keeps its 122. | Same index. | | 16 | The four `Source::configure_*thread*` functions are 5 shorter each, with 2 compares fewer. The compiler had reduced each static to a flag and rebuilt the descriptor from it with a compare. Now it loads the descriptor. `stdio::init` keeps its 211. | Same descriptors. | | 17 to 20, 25 | `check_package_path` goes from 775 to 725 and `check_relative_path` from 766 to 747 (both with change 26). `add_entry_with_store` goes from 572 to 570, without the store of `dir`. `read_file_contents` keeps its 709. | Same results. | | 22 | `ssl_ctx_setup` (18) and `HTTPContext::<true>::init_with_opts` (49) keep their length. | Same calls. | | 23, 24 | `InternalSourceMap::find` goes from 176 to 177, `append_vlq_to` from 154 to 161 and `Builder::finalize` from 209 to 217: moves to and from stack slots. `seed_window` goes from 77 to 72. No compare or branch more. | The two loops compute the stream once, not once per window. | | 26 | Apart from the sizes: `handle_esm_resolution` goes from 487 to 481, `resolve_and_auto_install` from 1,305 to 1,300, `get_loader_and_virtual_source` from 368 to 365, and the `read_dir_info_package_json` thunk from 26 to 22. | Same package.json. | | 28 | `bin_dirs` goes from 18 to 11 and `dir_info_uncached` from 1,099 to 1,089, without the loads and stores of the flag. | Same list. | | 29, 30 | `LazySourceMap::load` goes from 230 to 219. `Lookup::get_source_code` goes from 180 to 189, with the same tests in another order: it looks at the new field before it asks for a provider. `SourceContentPtr::from_provider` (6) goes. `to_executable` is 2 shorter, `target_executable` 1 longer. | Same sources. | | 31 | `http_thread_on_init_error` goes from 107 to 99. **The empty default of the field is the address of a static NUL byte, where it was a constant. Each of the 11 functions that build a default `InitOpts` needs one `lea` more for it:** `preconnect` and `send_sync_for_result` in `bun_http`, and 9 in `bun_runtime`, `FetchTasklet::queue` among them. | One instruction more per call, so per `fetch()`. | | 33, 46, 49, 50 | `init` goes from 2,339 to 2,275 (with 31), with 8 branches, 5 calls and 2 panic sites fewer. `allocate_package_manager` is 4 longer and the drop of `PackageManager` 1. `create_fake_temporary_node_executable` goes from 541 to 518. The readers of the path load it from the manager: between 4 shorter and 3 longer. | One copy of the cwd fewer. | | 36 | `find_workspace_root_above` goes from 1,286 to 1,276. | One copy and one bounds check fewer per package.json above the cwd. | | 39 | `StringBuilder` loses a pointer that release builds computed and stored and never read. `allocate` goes from 62 to 60. Three functions that have the builder inline, and that also copy `Dependency`s (41), have fewer branches and panic sites: `Package::clone` (1,130 to 1,105, −6 and −2), `fetch_necessary_package_metadata_after_yarn_or_pnpm_migration` (1,055 to 1,042, −2 and −1) and `get_or_put_resolved_package_with_find_result` (3,118 to 3,123, −6 and −2). | Same strings. | | 40 | The guard's `drop` (21) has another name. `install_with_manager` goes from 3,871 to 3,864 (with 7, 35 and 47). | Runs at the same point. | | 41 | `parse_with` goes from 858 to 848, `Group::clone` from 38 to 34, `default` and `from` lose 2 each. `parse_dependency` goes from 1,842 to 1,844, with one panic site fewer. | Same ranges. | | 42 | `edit_after_resolve_slow` goes from 3,471 to 3,447 and `move_to_front` from 144 to 123. | Same package.json. | | 43 | `tarball_stream::drain_callback`, which has `populate_result` inline, goes from 1,872 to 1,859, with 5 branches fewer. | Same files. | | 47, 48 | `Diff::generate_inner` goes from 5,192 to 5,193, with 4 branches and 9 compares fewer. `parse_with_json` goes from 4,287 to 4,294, with 5 branches fewer. | Same diff. | Changes 5, 21 and 27 are in the table of checks. Changes 3, 6, 10, 11, 34, 35, 37 and 38 ask for the same operations in the same order. Where they show up at all, it is in registers: `save_lockfile` goes from 294 to 292, and `run_tasks_erased` keeps its 6,254. Changes 15, 32, 44 and 45 are not compiled on Linux. The comparison covers x86-64 Linux only, one crate at a time. The build that ships also inlines across crates when it links. ### The changes in detail #### 1. toml: the parser keeps tables and arrays as `StoreRef` `e_object()` and `e_array()` return a `StoreRef`, which is `Copy` and derefs to the node. The parser called `.as_ptr()` on it, passed `*mut E::Object` / `*mut E::Array` around and dereferenced the pointer in six `unsafe` blocks. It keeps the `StoreRef` instead, as the YAML and XML parsers do. The `meta` map is still keyed by the node's address. #### 2. bun_core: remove `boxed_zeroed_unchecked`, `ZStr::boxed` and `OptionsEnvArg for Box<ZStr>` Nothing calls `boxed_zeroed_unchecked`. `ZStr::boxed` is called only by `impl OptionsEnvArg for Box<ZStr>`, and `append_options_env` is used only with `&'static ZStr`, `String` and `OptionWord`: no `Box<ZStr>` exists anywhere else. One `unsafe fn` and three `unsafe` blocks go with them. #### 3. xml: `Unit::from_u16` and `Unit::from_bytes` instead of casts The scanner is generic over `u8` and `u16` code units. Three places that run for one width only turned a value of the concrete type into the generic one, or back, with `transmute_copy` or `from_raw_parts`. `find_ascii` uses `Unit::bytes`, which is the identity for `u8`. The other two are trait methods: the identity in the impl for the width they are reached with, `unreachable!()` in the other one, where `units_of` had `assert!(!U::WIDE)`. Each caller is behind the constant `U::WIDE`. #### 4. crash_handler: `UNSUPPORTED_UV_FUNCTION` holds the name's bytes `CrashHandler__unsupportedUVFunction` stored the `*const c_char` it was given in a thread local, and the two places that print it turned the pointer into bytes again in an `unsafe` block each. The function already measures the string for its own panic message. It does that first and stores the `&'static [u8]`: it never returns, which its signature says with `-> !`, so the string outlives every read. The readers take the slice as it is. The arm for a null `name` goes: the function's contract excludes null, and it passed `name` to `cstr` a few lines further down anyway. #### 5. xml_index: `StructuralIndex::seek` and `at` read the window with `get` Both compared an index with `win.len()` and then read the element with `get_unchecked`. `slice::get` is that comparison and that read. In `seek` the loop condition becomes `while let Some(&p) = win.get(rel)`. `at` calls `fill_to` when `get` misses and tries again: `fill_to` returns only once the index is in the window. `seek_refill` called `at` for every index. It calls `fill_to` and then walks the window as `seek` does, so it reads `base` and the window once per fill. #### 6. collections: `HiveSlot` borrows its hive `HiveSlot<'h, ..>` kept the hive as a `*const HiveArray` that is null for a heap slot, next to a `PhantomData<&'h HiveArray>`, and its `Drop` dereferenced the pointer in an `unsafe` block. That pair is `Option<&'h HiveArray>`: the same word with the same null test, and `'h` was borrow-checked already. All of `HiveArray` is in `UnsafeCell` or `Cell`, and `HiveBox` holds the same reference. `HiveArray::drop_all` was an `unsafe fn` with `Drop` as its only caller. Its body moves there, with the `SAFETY` comment of the call on the `drop_in_place` that needs it. The comment also names what it relies on where a token is forgotten (`assume_init`, `ManuallyDrop`): that the slot has been written. #### 7. bun_core: `vec::extend_from_fn` is `Vec::extend` It reserved, wrote `f(i)` into `spare_capacity_mut()[..n]` and called `set_len` in an `unsafe` block. `(0..n).map(f)` is a `TrustedLen` iterator, for which `Vec::extend` does the same: one `reserve`, raw writes, and the length kept in a local until the end. The check of `spare[..n]`, which the compiler could not remove, goes away. If `f` panics, the elements written so far are in the `Vec`, which is what the doc comment says. #### 8. fmt: the shared temp buffer is an `Option<Box>` The thread local held `Option<NonNull<[u8; 32 KiB]>>` in a wrapper type with hand-written `Drop` and `Deref`, and a guard type took the pointer out, dereferenced it, and on drop put it back or freed it: three `unsafe` blocks for what `Option<Box<_>>` does by itself, in the same word. `with_shared_temp_buffer` takes the `Box` out of the cell (or allocates one), lends it to a closure and puts it back. The three users run their body in that closure. While one runs the cell is empty, so a writer that formats UTF-16 or Latin-1 again still gets a buffer of its own. After such a nested use the inner buffer is the one that is freed, not the outer. #### 9. SmolStr: `Inlined::all_chars` views the `u128` with `bytes_of_mut` It cast `&mut self.0` to `&mut [u8; 15]` through a raw pointer. `bytes_of_mut` gives the 16 bytes of the `u128`, as `slice()` above does with `bytes_of`, and `first_chunk_mut` the first 15. The length is a constant, so the `unwrap` folds away. `Inlined::ptr` had no other caller. #### 10. bun_core: `impl io::Write for Writer` calls the inherent methods `write_all` and `flush` of the trait impl repeated the bodies of the inherent `Writer::write_all` and `Writer::flush`, `unsafe` block included. They call those instead. All four are `#[inline]`. #### 11. atomic_cell: `unreachable!()` for the widths that `Atom` excludes `size_dispatch!` matches on `size_of::<T>()`, a constant in every instantiation, so an optimized build drops the arm for other widths however it is written, and `unreachable_unchecked()` bought nothing. #### 12. highway: `memmem` subtracts addresses The index of the match is the pointer that `highway_memmem` returns minus the start of the haystack. For `u8`, `offset_from` is that subtraction and nothing else, and subtracting the two `addr()`s needs no `unsafe`. It is the same `sub` where overflow checks are off. #### 13. Check constant strings at compile time instead of `from_utf8_unchecked` The crash handler's `GIT_SHA` is the first 7 characters of a `&str` constant: `str::split_at` is a `const fn`, so the detour through bytes and `from_utf8_unchecked` is not needed. `env::GIT_SHA_SHORT`, the first 9 characters, made the same detour through `const_str_slice`, with `core::str::from_utf8`. It uses `split_at` too, and `const_str_slice`, which had no other caller, goes. `__runtime_embed_load` turned the `&[u8]` constants `CODEGEN_PATH` and `BASE_PATH` into `&str` at run time without a check. They become `&str` constants through the `const fn` `core::str::from_utf8`, so that a path that is not UTF-8 fails the build. The doc comment of `strings::str_utf8` names that use instead of `const_str_slice`. #### 14. Call `gettid`, `getpid`, `alarm`, `_exit` and `raise` as `safe fn` Seven `unsafe` blocks in `bun_core` and the crash handler only called a libc function that takes integers or nothing and has no memory-safety precondition. The other OS arms of `thread_id::current_uncached`, and `Global.rs`, `spawn/process.rs` and `ParentDeathWatchdog.rs`, declare such functions as `safe fn` in an `unsafe extern "C"` block next to the caller. These sites do the same, with the signatures that the `libc` crate has, so they call the same symbols (`gettid` still goes through `--wrap=gettid`). For `getpid`, `debug.rs` uses the declaration that `output.rs` has. #### 15. bun_core: `fd_path_raw_w` takes `&mut [u16]` It was an `unsafe fn` that took a pointer and a capacity, and both callers passed `as_mut_ptr()` and `len()` of a slice. With the slice, only the call to `GetFinalPathNameByHandleW` is `unsafe`: the `\\?\` prefix is read by index and removed with `copy_within`. Every index comes after a comparison of `n`, which is the length of the slice, with a larger constant, and each `copy_within` range after one with a constant no smaller than its start, so no bounds check is left. That is why `copy_within` is called in each of the two arms and not once with the offset they produce. #### 16. output: `STDOUT_STREAM` and `STDERR_STREAM` are atomics Each of the two statics holds a file descriptor that `set_init` writes once at startup and that every thread reads when it is configured. They were `RacyCell<File>`, read and written in `unsafe` blocks. An `AtomicI32` (`AtomicU64` on Windows, where an `Fd` is 64 bits) with `Relaxed` loads and stores is the same plain load and store, as for `bun_stdio_tty` below. #### 17. resolver: `BrowserMapPath::map` is `&'static BrowserMap` The one constructor passes `&package_json.browser_map`, and `DirInfo::package_json()` already returns `&'static PackageJSON`. The field shortened that to `'b`, so each of the four matches in `check_path` widened the value back with `Interned::assume`. With the real lifetime on the field, `map.get()` yields a `&'static Box<[u8]>` that is assigned to `remapped` as is. #### 18. resolver: keep the slot that `MaybeUninit::write` returns `dir_info_cached_miss` wrote a queue item with `queue[i].write(..)`, dropped the `&mut` that call returns, and a few lines later got the same slot back with `unsafe { queue[i].assume_init_mut() }`. Only `rfs.entries.get(top)` runs in between and it does not touch `queue`, so the returned reference is kept. #### 19. resolver: drop three lifetime casts that change nothing `BSSStringList::append` and `print` return a slice whose lifetime the caller picks, so `FilenameStoreAppender::append` and the stores' `print` already had the lifetime they return; the `Interned::assume(..).as_bytes()` after each call was a no-op, as `append_slice` right above shows. `add_entry_with_store` dereferenced `stored` twice, once behind a `&*ptr::from_ref(..)` of a slice that already had an unbounded lifetime. It now makes one `&'static Entry` and uses it for the key and for the return value. #### 20. resolver: `RealFS::read_directory` and `EntryLookup` keep their `'static` `RealFS::read_directory` forwards to `read_directory_with_iterator`, which returns `&'static mut EntriesOption`, but declared the result as a borrow of `&mut self`. `FileSystem::read_directory` and `DirEntryAccessor::openat` cast it back to `'static` through a raw pointer. It now returns what it gets, and both callers use the value as it comes. `EntryLookup<'a>` tied the `&Entry` it hands out to the `DirEntry` it was looked up in, while its own safety note rests on the entry being a slot of the never-freed `EntryStore`, the only source of the pointers in `DirEntry::data`. `EntriesOption::lookup` widened a `&DirEntry` to `'static` only to get an `EntryLookup<'static>` out of `get`. The parameter is gone and `entry()` returns `&'static Entry`. One `&mut Entry` could overlap such a reference: `add_entry_with_store` borrowed a recycled entry mutably to store `dir`. All four callers that pass a previous map build the new `DirEntry` with a copy of the old one's `dir`, and an entry's `dir` is only ever a copy of its `DirEntry`'s, so the store wrote the pointer and length that were already there. The store is gone and the entry is borrowed shared; the rest of that block uses `mutex`, `need_stat` and `cache()`, which take `&self`. #### 21. http: `h3::ClientContext::get_or_create` reads the loop itself Both callers passed `unsafe { NonNull::new_unchecked(http_thread().uws_loop) }` and the callee only unwrapped it again with `as_ptr()` for the FFI call. The parameter is gone; the existing FFI block passes `http_thread().uws_loop()`, and only when the engine has to be created. #### 22. boringssl: `ssl_ctx_setup` takes `&OwnedSslCtx` It was an `unsafe fn` over `*mut SSL_CTX` whose only requirement was that the context is live. Its two callers, both in `HTTPContext`, had just stored an `OwnedSslCtx` in `self.secure` and fetched the raw pointer back through an `unwrap`. `OwnedSslCtx` owns a reference, so taking it by reference puts that requirement in the type and the function is safe. The callers pass what `self.secure.insert(..)` returns, and `HTTPContext::ssl_ctx` is gone. #### 23. sourcemap: `impl_pod!` for `SyncEntry` and `ObjectBrace` `Builder::finalize` viewed both tables as bytes with `slice::from_raw_parts`, and `for_each_object_brace` read each `ObjectBrace` with `ptr::read_unaligned`, each behind a comment that the struct is `repr(C)` with no padding. `impl_pod!` has rustc prove that neither struct has padding and that every bit pattern is a value of it, so the views are `slice_as_bytes` and the read is `read_prefix`. The macro does not look at the `repr`: both structs and `LineColumnOffset` are `repr(C)`, and `Ordinal` is `repr(transparent)`. The byte lengths come from those slices, so `copy_from_slice` compares the same values as before, and `read_prefix` is given a `&[u8; 16]`, so its length test is a constant. #### 24. sourcemap: `WindowReader<'a>` borrows the stream `WindowReader` kept the stream as `*const [u8]` and handed it out through `fn bytes<'a>(&self) -> &'a [u8]`, an unbounded lifetime from a raw pointer. Every reader is a local of `find`, `for_each` or `append_vlq_to`, so the field is `&'a [u8]`: copying the reference out leaves `self` free to mutate, which is what the accessor was for. A reader started out as `DANGLING`, with a null stream, and `parse` stored the stream it was given. A reference cannot be null, so `WindowReader::new(stream)` takes the stream, and `parse` reads it from `self`. The two loops compute the stream once instead of once per window, and so also for a map with no windows. That is three header loads that are valid for every blob; `object_braces()` already does them unconditionally. #### 25. resolver: four `unsafe` blocks that plain borrows replace - `Resolver::standalone_module_graph` is `Option<&'static dyn ..>`. Its only two setters, in `VirtualMachine`, assign a `'static` graph, so the transmute in `for_worker` that widened `'_` to `'a` is a copy. `for_worker` stays `unsafe` for `env_loader`. - The close-or-store guard in `RealFS::kind` captures `&mut cache.fd` instead of a `*mut EntryCache`; nothing else touches `cache` while the guard lives. - `FileSystem::init_with_force` returns `INSTANCE.get().cast()` when already loaded, instead of going through a `&mut MaybeUninit` for the same address. - `read_file_with_handle_impl` writes the trailing NUL through `spare_capacity_mut().first_mut()`: the same compare and the same store. #### 26. resolver: `package_json` travels as `&'static PackageJSON` `Result::package_json`, `MatchResult::package_json` and the `read_dir_info_package_json` hook held `Option<*const PackageJSON>`. Every value stored there comes from `DirInfo::package_json()`, `DirInfo::enclosing_package_json` or `FallbackModule::package_json`, which are `&'static PackageJSON` already, so each store cast a reference to a pointer and each read cast it back with `unsafe { &*p }`. The reason given for the pointer was that zeroed initialisation stays valid. Nothing zero-initialises these structs, their `Path` fields hold slices that could not be zero anyway, and an all-zero `Option<&T>` is `None`. The fields and the hook hold `Option<&'static PackageJSON>`, the four functions that store their `package_json` argument take `&'static`, and `Result::deref_package_json` is gone. Both structs shrink by 8 bytes. #### 27. resolver: `load_tsconfig` keeps its boxes Each `Box<TSConfigJSON>` from `parse_tsconfig` was turned into a raw pointer, kept in a `BoundedArray<*mut _, 64>`, read through a `BackRef`, dereferenced twice per merge step and turned back into a box twice. Nothing but this walk holds the configs, so they stay boxes: `current` owns the newest one and the array owns the ones before it, 63 of them, so a chain is still cut off at the 65th config. Two `expect`s and an `unwrap` go with the pointers. When a chain is that long, the configs read so far are freed with the array instead of leaked. #### 28. resolver: `BIN_FOLDERS` is initialised at compile time The static was a `RacyCell<MaybeUninit<BinFolderArray>>` with a `BIN_FOLDERS_LOADED` flag beside it, because `BoundedArray` had no const constructor: both append sites first wrote an empty array under the flag, and every access went through `assume_init_*`. `BoundedArray::new()` is a `const fn` with the body of `default()`, so the static starts as an empty array. The flag, its three loads and both initialising blocks are gone; `bin_dirs()` returns the empty slice of that array where it returned `&[]`. #### 29. standalone_graph: in `inject`, `zname` borrows `zname_owned` The two places that repoint `zname` at a heap buffer stored the box in `zname_owned` and then rebuilt a `&ZStr` from its raw pointer and length, with a comment that the box outlives it. `zname` now borrows what `zname_owned.insert(..)` returns, so the compiler checks that: the old `zname` is last used to build the new buffer, before `zname_owned` is overwritten. The `let _ = (&mut zname_owned, &mut zname);` that keeps Windows from warning about two variables it never changes is compiled for Windows only, since it would borrow `zname_owned` again while `zname` is in use. #### 30. sourcemap: `ParsedSourceMap` holds the standalone graph's sources by type The standalone graph boxed its own `SerializedSourceMapLoaded`, cast the pointer to `*const SourceProviderMap` and stored it as an integer in `underlying_provider`, with `is_standalone_module_graph` set so that nothing would call it as a provider. `Lookup::get_source_code` cast that integer to `SerializedSourceMap::Loaded`, a different `repr(Rust)` struct with the same fields, and dereferenced it. The flag is replaced by `standalone_module_graph: Option<&'static Loaded>`. The graph builds a `Loaded` and leaks it, as it leaked the box before, and `get_source_code` reads through the reference, before it asks for a provider, which such a map no longer has. The duplicate struct, `standalone_module_graph_data` and `SourceContentPtr::data` are gone, and so is `SourceContentPtr::from_provider`, which only forwarded to `from_source_provider` and whose one remaining caller calls that instead. #### 31. http: `InitOpts::abs_ca_file_name` is a `&'static ZStr` The field was a `&'static [u8]` that was NUL-terminated by contract. `on_init_error_noop` rebuilt a `ZStr` from its pointer and length in an `unsafe` block, while the one producer, `PackageManager::init`, stored a slice whose length included the NUL, and its own error handler stripped it again. The producer already holds a `ZBox`, so it parks that in `ABS_CA_FILE_NAME` and passes the `&ZStr`. Both handlers use the field as it is. #### 32. exe_format: `_mh_execute_header` is a `safe static` `find_identity` on macOS copied the header out of the extern static with `unsafe { header_address.read() }`. The linker defines the symbol for every executable, the header is eight integers, and it stays mapped read-only for the life of the process, so reading it has no precondition. The declaration says so with `safe static` and the header is read by value. The block that views the load commands after it stays. #### 33. install: `init` reborrows the `&'static mut DirEntry` it matched on `FileSystem::read_directory` returns `&'static mut EntriesOption`, and its `Entries` arm holds a `&'static mut DirEntry`. `init` and `init_with_runtime_once` turned the matched reference into a raw pointer and back in an `unsafe` block to get a `'static` lifetime that a plain reborrow already has. Both now write `&mut **e`, as `init_with_runtime_once` does further down for `manager.root_dir`. #### 34. install: `save_lockfile` uses `progress.root` instead of a stashed node pointer `Progress::start` returns `&mut self.root`. `save_lockfile` kept that reference as a `*mut Node`, initialised to null, and dereferenced it in two `unsafe` blocks. The first use is now chained onto `start()`, and the second is `this.progress.root.end()`, which is the same node and the spelling the function already uses two lines further down. #### 35. install: drop four raw-pointer and lifetime detours that borrowck does not need - `install_with_manager`: `maybe_root.parse` got `&mut *mgr` through a raw pointer. Its other arguments are a stack `Lockfile`, the detached `log_mut()` and an owned source, so `manager` can be passed directly, as `bun link` does. - `run_tasks`, extract arm: `alias` was `detach_lifetime(tarball.name.slice())` next to `resolution`, a plain borrow of the same `tarball`. While it is live the arm only touches `task.data`, `Copy` fields and `&self` accessors, and makes shared reads of `task.request`. - `run_tasks`, manifest arm: `name` was detached from `task.callback`. Every other use of `task` in the arm is a different field, except `retry_with_abbreviated_manifest(&mut self)`, whose two arguments are now computed first, in the same order. - `populate_manifest_cache`: `(*mgr).end_progress_bar()` after `sleep_until` returned is `manager.end_progress_bar()`; `mgr` is not used afterwards. #### 36. install: `find_workspace_root_above` borrows `walk.path` for the source path Each package.json above the cwd had its path copied into the process-global `ROOT_PACKAGE_JSON_PATH_BUF` in an `unsafe` block, only to name the `Source` that is parsed in the same loop iteration. `Source<'a>` already borrows the loop-local `json_buf`, the body does not touch `walk` again, and log locations own their bytes, so the source now borrows `walk.path`. This removes one copy and one bounds check per package.json. That check could not fail: the copy was only reached after `openat_a` had opened the path, and `openat_a` refuses a path that does not fit a `PathBuffer`. The global buffer is left with a single writer, `init`. #### 37. install: `AutoInstaller::path_for_resolution` takes `&mut PathBuffer` The trait method took `&mut [u8]`. Its only implementation debug-asserted the length and cast the slice's pointer back to `&mut PathBuffer` in an `unsafe` block, and its only caller, the resolver, passes `bufs!(path_in_global_disk_cache)`, which is a `&mut PathBuffer` that was deref-coerced to a slice. The parameter now has the type both ends use, so the cast, the assert and a length that release builds never checked are gone. The call in the resolver is unchanged. This was the last place that reinterpreted a `&mut [u8]` as a `PathBuffer`, so the sentence in the `PathBuffer` doc that gave such casts as the reason for its alignment is gone too. #### 38. install: `NetworkTask` reads its manager through the `ParentRef` it stores `pm_mut()` turned the `package_manager` back-reference into a `&'a mut PackageManager` with an unbounded lifetime. Its two users, `manifest_request` and `for_tarball`, only call `&self` methods and read fields, while their callers hold the manager mutably. They now copy the `ParentRef`, which is `Copy` and derefs to `&PackageManager`, into a local, so `self` stays free to be assigned to. The accessor is deleted. #### 39. install: remove three one-line `unsafe` blocks that have a safe spelling - `lockfile::StringBuilder.ptr` was an `Option<*mut u8>` computed with `as_mut_ptr().add(prev_len)` and only ever asked `is_some()`, in debug assertions. It is now `allocated: bool`. - `Task::Id::for_npm_package` hashed the bytes of a `semver::Version` through `ffi::slice`. `VersionType<u64>` is `NoUninit`, so `bun_core::bytes_of` yields the same bytes. - `__bun_regex_compile` takes a reference and returns an `Option`, and its definition in `bun_jsc` is a safe fn, so the `extern "Rust"` declaration in `bun_install_types` says `safe fn`. #### 40. install: clear `root_lifecycle_scripts` with a `scopeguard` on `manager` `install_with_manager` declared a local `ClearRootLifecycleScripts(*mut PackageManager)` whose `Drop` wrote through the raw pointer. The pointer was taken from `manager`, and `manager` itself was used for the rest of the function, so the pointer was stale by the time the guard ran. The guard now owns the `&mut PackageManager` and the rest of the function reborrows it from the guard. It is declared at the same place, so it runs at the same point. #### 41. semver: delete `query::Group.input` `Group` stored the parsed text as a lifetime-erased `*const [u8]`. The only read was in `Package::parse_dependency`, an `unsafe` deref in the same function that had passed that slice to `parse`; `bun lint` overwrote the field after parsing so that it would not dangle. `parse_dependency` now keeps the slice next to the range, in `workspace_range: Option<(Group, &[u8])>`, and the field is gone, which makes `Group`, and every `DependencyVersion` and `Dependency` with it, 16 bytes smaller. #### 42. install: `UpdateRequest.e_string` keeps the `StoreRef` it is made from The field was `Option<*mut E::String>`. Every writer got a `StoreRef<E::EString>` from `Data::e_string()` or `string_entry()` and called `.as_ptr()` on it, and every reader dereferenced the raw pointer in an `unsafe` block. `StoreRef` is the AST's own `Copy` handle, with `Deref`, `DerefMut` and pointer-identity `PartialEq`, and `edit` already writes through one a few lines below. The field now stores the handle, so the writers drop `.as_ptr()`, the readers in `edit` and `add_catalog` use it directly, and the two comparisons compare handles. #### 43. install: `TarballStream::init` returns a `Box` and `populate_result` is a safe fn - `init` ended in `heap::into_raw(Box::new(..))`, and both callers passed the pointer straight to `heap::take`. It now returns the `Box`. - `populate_result` was an `unsafe fn` taking `*mut Task`, with its whole body in one `unsafe` block. All it needs from the task is the tarball of the `extract` request and the log, and all it leaves there is `data`, `err` and `status`. It now takes `&ExtractTarball` and `&mut Log` and returns `Result<ExtractData, Error>`, with no `unsafe` inside. `finish`, which is `unsafe` anyway and reads the same union field again further down, reads the union and stores the three fields. - The entry path was built with `OSPathSliceZ::from_raw_mut`, although only the Windows escape pass writes to it, and that pass starts by asking for the slice. The checks and the escape pass now work on `&mut norm_buf[..norm_len]`, and the NUL-terminated view is made afterwards with `from_buf`. That leaves `WStr::from_raw_mut`, `WStr::as_mut_slice` and `DerefMut for WStr` without a user, so they are deleted. - The comments in `init` and `finish` name the function that sets the `extract` variant, and describe how the stream is really freed: by dropping `network_task.tarball_stream.take()`. #### 44. install: the Windows hardlink task is passed and reclaimed as a `Box` `HardLinkWindowsInstallTask::init` returned `into_raw(Box::new(..))` for an `unsafe fn push(*mut T)`, and `run_from_thread_pool` kept the raw pointer: it called `run()` and stored `err` through it, and reclaimed the `Box` on two separate paths. The worker owns the task from the first line of the callback, so it now takes the `Box` back right after the `complete_one` guard, as `UninstallTask::run` does. `init` returns the `Box`, and `push` takes it, is a safe fn, and requires `TaskType: Send + 'static`, like `OwnedTask`. The drop order is unchanged: the slot lock, the task, then `complete_one`. #### 45. install: the Windows `Hardlinker` keeps the cwd slice it gets `get_fd_path_w` returns a sub-slice of `cwd_buf`. `link` turned it into an offset, with `offset_from`, and a length, and sliced the buffer again for every file, because the slice supposedly could not stay borrowed across the loop. Nothing else uses `cwd_buf`, so the slice is now kept and used directly. This also saves one bounds check per linked file. #### 46. install: `ROOT_PACKAGE_JSON_PATH` becomes `PackageManager::root_package_json_path` The path was a `pub static RacyCell<&ZStr>` that `init` wrote and eight places read in `unsafe` blocks. Every reader has the manager at hand, so the path is now a field that `allocate_package_manager` fills in. `init` passes the `ZStr` it builds in the static buffer, and auto-install passes `ZStr::EMPTY`, the static's initial value, which `root_patched_dependencies` relies on to fall back to the package.json next to the lockfile. A process allocates one manager: a second `init` only follows `MissingPackageJSON`, which is returned before the path is built and before the manager is allocated, and `init_with_runtime` runs under a `Once`. #### 47. install: `Diff::generate` takes `to_update: bool` instead of the update requests `install_with_manager` passed `Some(&manager.update_requests[..])`, made in an `unsafe` block, next to `&mut *manager` whenever `manager.to_update` was set, and `None` otherwise. `generate_inner` only asked the slice whether it was empty, which it also asks `pm.update_requests` itself at its top. The parameter is now the bool, and the emptiness is read once from `pm` and used in both places. The callers that passed `None` pass `false`. The SAFETY text at that call also says that `Diff::generate` never reaches `manager.lockfile` through `pm`. That was false with `--frozen-lockfile` and for `bun pm`: `parse_with_json` built `SkipIfInLockfile(&pm.lockfile)` on every parse, while `from_lockfile` was a live `&mut` to the same `Lockfile`. Only the two `process_names_array` calls of the `workspaces` group consume the value, so it is now built only under `FEATURES.workspaces`, which the parses that `Diff::generate` and the folder resolver make do not set. The behaviour is the same. The comment in folder_resolver.rs states that property, and the one in prune.rs no longer claims it for its own `Features::main()` parse, which does read `pm.lockfile`, at a point where nothing else borrows it. #### 48. install: `Package::parse_from_real_manager` takes `&mut PackageManager` It was an `unsafe fn` taking `*mut PackageManager`, and its one caller passed `ptr::from_mut(self)` in an `unsafe` block. The contract asked for a live manager whose `lockfile` and `log` point to live allocations outside it. A `&mut PackageManager` gives the first, `lockfile` is a `Box`, and `log` comes from the safe `log_mut()`. What is left is that the `&mut Lockfile` it splits off must be the only way to the lockfile during the parse. `parse_with_json` derefs `pm.lockfile` only under `Features::workspaces`, so the `features` parameter is gone and the function always parses with `Features::NPM`, which is what its caller passed. With that nothing is left for a caller to promise, and the function is safe. One `unsafe` expression remains inside it, the `&mut *lockfile`. #### 49. install: in `init`, `original_cwd` borrows the boxed copy of the cwd `original_cwd` was a slice of `original_package_json_path_buf`, reached through `ZStr::from_raw` so that the borrow checker would not see it. The directory walk, `this_cwd` and a later pointer comparison all hang on that slice, and they are still in use when the buffer is truncated and gets `/package.json` appended again. If package.json is in a parent directory, that write lands inside `original_cwd`. `init` already makes a boxed copy of the cwd to return it. The copy is now made from `top_level_dir_no_trailing_slash`, the bytes the buffer was filled with, and `original_cwd` borrows it, so the walk no longer points into the buffer. The first `original_package_json_path` is only read before the buffer changes and is reassigned before its next use, so it can be a plain `from_buf` borrow. Nothing read the overwritten bytes, so the behaviour is the same. #### 50. install: remove two pieces of dead code that needed `unsafe` - `create_fake_temporary_node_executable` turned a non-empty `optional_bun_path` into a `ZStr` with `from_raw`, trusting the caller of a safe `pub fn` to have put a NUL behind the slice. Both callers pass an empty slice and only read what the function writes back, so the branch is gone. - `init` copied the cwd into `CWD_BUF` right after reading it. Nothing reads the buffer before `init` overwrites it further down and publishes it as `top_level_dir`, so the first copy is gone. #### 51. xml_index: `seek_refill` calls `at` for every index, as it did The commit that made `seek` and `at` read the window with `get` also gave `seek_refill` a loop of its own over the window. That left `at` without a caller outside the unit tests and the benches, which the `unused_pub` lint reports. `seek_refill` has its old body again. Compared with `main`, `seek_refill` is one instruction, one compare and one branch longer (35 to 36 instructions, for `u8` and for `u16`): `at` compares the index with the length of the window again after `fill_to` has returned. That happens once per refill. #### 52. resolver: `EntriesOption::try_entries_mut` returns the listing or the read error `init` and `init_with_runtime_once` of the package manager turned the result of `read_directory` into a `&'static mut DirEntry`, or returned its error, with one `match` each. Since an earlier commit wrote `&mut **e` in both, the two are equal arm for arm, which the `same_match_twice` lint reports. The mapping is now a method of `EntriesOption`, next to `entries_mut`, which panics on `Err`. Both callers apply `?` to it. The result borrows from `&mut self`, which is `'static` for the value that `read_directory` returns. Nothing changes in the compiled code: the method is inlined into both callers. #### 53 and 54. Two fixes for tests of `main`'s, which the merge of `main` takes out again `test/aot/engine/ir-6.test.ts` and `test/aot/build-scaling/build-scaling-2.test.ts` failed without this PR too. Commit 53 brought in the change that #47074 makes to one fixture, and commit 54 the two files of #47329, byte for byte. #47316 has since rewritten all three files on `main`. The fixture no longer has the eleven types that commit 53 relaxed, and the shape of commit 54 no longer has a recorded growth: it went from 17,206 to 4,416 million instructions. Two of the three files conflicted. The merge takes `main`'s version of all three. #### 56. resolver: `read_directory` and its siblings return `&'static EntriesOption` `FileSystem::read_directory`, `RealFS::read_directory`, `read_directory_with_iterator` and `read_directory_error` returned `&'static mut EntriesOption`: a slot of the shared directory cache, or the thread's scratch slot. They are safe functions, so two calls for one directory gave safe code two `&mut` to one slot. That is so on `main` too, for all but `RealFS::read_directory`, whose result change 20 widened to what its callee returns. `entries_at_locked` handed out a `&mut` to the same slots. No caller writes through the result: - The package manager reads `data`, `dir` and `fd` and calls `has_comptime_query`. - The test scanner reads the error or walks `data`. - `DirEntryAccessor::openat` and `load_as_file` made a shared reference of it at once. - Both callers of `entries_at_locked` call `as_entries`. So they all return a shared reference, `PackageManager::root_dir` is a `&'static DirEntry`, and the `try_entries_mut` of commit 52 is `try_entries(&self)`. What stays as it is on `main`: a read with a newer generation rewrites a slot's `DirEntry` in place under `entries_mutex`, and the next miss on a thread overwrites its scratch slot, while an earlier result may still be held. Readers of `data` take the mutex, and the doc of `read_directory` says to read an `Err` before the next call. ### How did you verify your code works? - **Static checks on the first 52 commits, which hold all the code:** `cargo fmt --check`, `cargo clippy --workspace` (0 warnings), the source lints and `cargo check --workspace --release` on Linux, and `cargo check --workspace --release` for `x86_64-pc-windows-msvc` and `aarch64-apple-darwin`, because seven changes touch code that only those targets compile. - **Assembly:** the comparison above. - **230 test files**, on a debug build with ASAN of these commits and on a debug build of the base: all 114 files of `test/cli/install/`, and the files for the rest: TOML and XML, source maps, the shell's brace expansion, `test/js/bun/resolve/`, the bundler's resolver tests, `bun build --compile`, HTTP/3 and TLS, `console.log` and `Bun.inspect`, auto-install and `bunfig.toml`. - 220 files pass completely, 17,876 tests. - 10 files fail, in exactly the same tests on both builds: | File | Tests that fail | Why | |---|---|---| | `bun-run.test.ts` | 28 | They run `bun-debug` by name, and it was not on my `PATH`. | | `bun-install-git-deps.test.ts` | 19 | My `git` refuses to make their tags ("no tag message?"). | | `bun-install.test.ts` | 4 | Git URLs. | | `bun-install-lifecycle-scripts.test.ts` | 2 | "does not use 100% cpu", on a machine that ran other tests at the same time. | | `bundler_compile.test.ts` | 7 | Packages that I did not have. | | `bun-build-compile.test.ts` | 3 | Their time limit of 60 seconds. | | `run-crash-handler.test.ts` | 1 | It expects a frame name that a debug build prints differently. | | `resolve.test.ts` | 1 | `tsyringe`, which I did not have. | | `stringWidth.test.ts` | 1 | `string-width`, which I did not have. | | `inspect-error-leak.test.ts` | 1 | Its time limit of 10 seconds: 115 seconds here and 116 on the base, and +359 MB on both. | - The 13 source-map files and `brace.test.ts` ran after my last edit to change 24, the way a `WindowReader` gets its stream. The other files ran before it. - **After the tests had run** I removed six `#[inline]` attributes that an earlier form of changes 3 and 9 had added, and took one edit out of change 14, so that the crash handler calls `libc::abort()` as it does on `main`. The static checks ran again after that, and so did the assembly comparison of `bun_crash_handler`, with the numbers above. - **`bun.lockb`:** a build of the base, a build of the first 52 commits and Bun 1.4.4 write the same 10,218 bytes for one project, which has ranges with `||`, a hyphen, `*`, `1.x` and prereleases, an alias, a dist-tag, two catalogs, two workspaces, an override, and peer, optional, dev and trusted dependencies. Each of the three reads what each of the three wrote: `bun bun.lockb` prints the same text and the same hash, `bun install --frozen-lockfile` passes and leaves the file as it is, `bun pm ls --all` prints the same tree, and `bun add` turns the file into the same 10,554 bytes. The ten `.lockb` files in the repository print the same text on all three. - **Commits 51 and 52 came after that.** With them I ran all the static checks again and compared the assembly of `bun_parsers`, `bun_resolver` and `bun_install` again: 52 changes no instruction, and the numbers above for `bun_parsers` include 51. On a debug build of these 52 commits, `xml.test.ts`, `xml-test-suite.test.ts`, four auto-install files, `bun-remove.test.ts`, `bun-link.test.ts` and `bun-pm.test.ts` pass completely, 2,315 tests. `bun-add.test.ts` and `run-autoinstall.test.ts` did not finish in the 50 seconds that I gave each file. - **The merge of `main` (3bf1a0646) came last.** Everything above is from before it. - The only conflicts were in two of the three test files of commits 53 and 54. 16 files under `src/` that both sides changed merged by themselves. - The 2,496 lines that this PR adds and removes under `src/` are the same before and after the merge, in the same order, and so are the counts of `unsafe` against `main`. - On the merged tree, `cargo fmt --check`, the source lints and `cargo clippy --workspace` (0 warnings) pass, and a debug build with ASAN and a release build link and run. - On the debug build I ran 30 test files for the code that both sides changed and for the parsers and the package manager: `import.meta.glob`, import queries, `test/js/bun/resolve/`, the bundler's resolver cache, source maps of compiled executables, the crash handler, `bun:ffi`, TOML, XML, `bun pm`, `bun link`, `bun remove` and `bun.lockb`. 29 pass completely, 3,997 tests. In `resolve.test.ts` one test fails, the one that needs `tsyringe`, as in the table above. - **Commit 56** came after the merge. With it, `cargo fmt --check`, the source lints, `cargo clippy --workspace` (0 warnings) and `cargo check --workspace --release` pass on Linux, and so does `cargo check --workspace --release` for `x86_64-pc-windows-msvc` and `aarch64-apple-darwin`. The release binaries with and without it have the same bytes in `.text` (105,908,048 bytes), `.rodata`, `.data`, `.data.rel.ro` and `.eh_frame`. --- _Generated by [Claude Code](https://claude.ai/code)_ --------- Co-authored-by: Claude <noreply@anthropic.com>
dylan-conway
added a commit
that referenced
this pull request
Nov 4, 2026
…nd server code (#47871) ### What does this PR do? 46 separate changes, one commit each: 17 in the VM and its workers (`bun_jsc`), 15 in DNS, timers, IPC and the module loader's hooks, and 14 in the socket and server code. They follow #47291, #47317, #47338, #47382, #47393 and #47414 and work the same way: signatures name the type and the lifetime that the value already has, hand-written pointer code becomes plain ownership and borrows that the compiler checks, and what was never needed is deleted. Together: **187 fewer `unsafe` blocks, 37 fewer `unsafe fn`s and 1 fewer `unsafe impl`**, +1314/−2230 over 57 files. No safe function became `unsafe`, and no wrapper type was added. **[Where the compiled code changes](#where-the-compiled-code-changes) lists every place where the program does different work.** | # | Change | `unsafe` | Diff | |---|---|---|---| | 1 | worker: `new Worker()` names the preload that does not resolve | 0 | +77/−91, 2 files | | 2 | worker: the helpers that walk a parent's children take `&VirtualMachine` | −14 blocks, −5 `unsafe fn` | +36/−65, 2 files | | 3 | jsc: `remap_zig_exception` has no `Tail` drop guard | −3 blocks | +45/−62, 1 file | | 4 | jsc: `print_error_instance_js` keeps the parser-arena flag in a local | −2 blocks | +7/−10, 1 file | | 5 | jsc: four `RuntimeHooks` slots take the reference their caller holds | −8 blocks, −6 `unsafe fn` | +20/−42, 3 files | | 6 | jsc: borrow the uws loop as `&Loop`, not `&mut Loop` | −5 blocks | +48/−66, 8 files | | 7 | jsc: `GarbageCollectionController::init` takes `&VirtualMachine` | −1 block | +5/−11, 2 files | | 8 | jsc: `RareData::close_all_socket_groups` has no `self` | −1 block | +3/−7, 2 files | | 9 | watcher: remove `WatchItem.count` | −1 block | +0/−16, 3 files | | 10 | jsc, valkey: drop three detours through a raw pointer | −3 blocks | +8/−12, 3 files | | 11 | worker: remove `unsafe impl Send for ThreadStart` | −1 `unsafe impl` | +6/−8, 2 files | | 12 | hot reload: remove `ImportWatcher::None` | −1 block | +38/−68, 3 files | | 13 | jsc: `remap_stack_frame_positions` takes `&mut [ZigStackFrame]` | −1 block, −1 `unsafe fn` | +4/−13, 2 files | | 14 | jsc: eight functions exported to C++ take references | −8 blocks, −7 `unsafe fn` | +31/−56, 3 files | | 15 | worker: the exports the messaging proxy calls take `&WebWorker` | 0 | +6/−10, 1 file | | 16 | jsc: the GitHub-annotation guard borrows the exception | 0 | +5/−8, 1 file | | 17 | worker: `join_child_workers` takes the list of children, not the VM | 0 | +4/−6, 2 files | | 18 | dns: the pending caches are borrowed shared, and each record type names its own | −5 blocks | +66/−180, 2 files | | 19 | dns: the request and lookup-node constructors take `&Resolver` | −6 blocks | +34/−52, 2 files | | 20 | dns: lookup nodes are completed through `&mut self` and freed by their owner | −24 blocks, −12 `unsafe fn` | +234/−384, 2 files | | 21 | dns: the connect-path cache key borrows `&[u8]`, not a `&ZStr` | −1 block | +27/−55, 2 files | | 22 | dns: `get_channel_or_error` returns the `&mut Channel` that `get_channel` gave it | −6 blocks | +22/−33, 1 file | | 23 | dns, ipc: fill a fresh `Box` before releasing it, and two more raw hops | −4 blocks | +21/−30, 2 files | | 24 | module loader: the guard that closes the input fd owns the fd and its flag | −6 blocks | +13/−45, 1 file | | 25 | ipc: `SendQueueOwner::Instance` carries no pointer | −7 blocks | +29/−39, 2 files | | 26 | jsc: the `retroactively_report_discovered_tests` hook takes `&mut TestReporterHandle` | −2 blocks, −2 `unsafe fn` | +10/−19, 3 files | | 27 | module loader: `TranspileArgs::extra` is `&TranspileExtra` | −5 blocks | +45/−57, 3 files | | 28 | dispatch: `__bun_fire_timer` takes `now: &Timespec`, and one poll arm reuses `poll_ref` | −3 blocks | +18/−32, 2 files | | 29 | timer: `clear_timer` downcasts with `as_class_ref`, as it needs only `&T` | −3 blocks | +10/−14, 1 file | | 30 | jsc_hooks: drop lifetime erasers and a raw round trip that change nothing | −2 blocks | +8/−20, 1 file | | 31 | timer, jsc_hooks: three `*mut` parameters whose contract a reference states | −5 blocks, −2 `unsafe fn` | +33/−62, 4 files | | 32 | timer: `increment_timer_ref` and `increment_immediate_ref` take `&Loop` | −4 blocks | +13/−25, 4 files | | 33 | server: `run_corked_with_type` takes `fn(&U)` and `&U` | −7 blocks | +46/−80, 4 files | | 34 | server: `NewServer` request dispatch takes `&self` | −12 blocks | +173/−289, 3 files | | 35 | tls: `SSLConfig` reads buffers and files through `&`, not `&mut` | −6 blocks | +11/−24, 3 files | | 36 | server: `attach_http2` takes `&self` | −4 blocks | +17/−22, 1 file | | 37 | jsc: `VirtualMachine::{enqueue_task, tick, drain_microtasks}` take `&self` | −4 blocks | +23/−34, 6 files | | 38 | server: `write_chunk` and `on_request_body_stream_drained` take `&self` | −2 blocks | +10/−21, 2 files | | 39 | server: `listen()` borrows the SNI names and the unix path | −3 blocks | +14/−38, 1 file | | 40 | socket: `Listener` types its late write and its SNI out-parameter | −4 blocks | +35/−29, 2 files | | 41 | socket: drop round trips through raw pointers | −3 blocks | +4/−14, 2 files | | 42 | server: `upgrade()` guards with the reference it holds; `ServerWebSocket::init` takes its flags | −2 blocks | +8/−11, 2 files | | 43 | node:http: slice the unwritten tail of a zero-copy write | −1 block | +3/−6, 1 file | | 44 | tls: `no_password` is a safe `extern "C" fn` | −1 `unsafe fn` | +1/−1, 1 file | | 45 | server, socket: type the out-parameters of four exported functions as references | −6 blocks, −1 `unsafe fn` | +17/−25, 3 files | | 46 | server: `request_pool` is a `&'static` pool | −2 blocks | +32/−44, 5 files | The commits can be reviewed one at a time. Worth knowing before reading on: - **Change 1 fixes a bug, with a test.** `new Worker(url, { preload: ["./does-not-exist.js"] })` threw `TypeError: undefined`. It now names the preload that does not resolve. Nothing else that users can see changes. - **Thirteen changes end an aliasing or contract bug that had no visible effect.** - Changes 3, 7, 8, 17 and 37: a `&mut VirtualMachine`, `&mut RareData` or `&mut ZigException` was live while other code reached the same object through another pointer. - Change 6: the uws loop was borrowed as `&mut Loop` across `tick`, whose callbacks reach the loop again. - Change 18: two constructors retagged a whole `HiveArray` as `&mut` and then wrote through an older pointer to one of its slots. - Change 21: `__bun_dns_prefetch` made a `ZStr` from a slice with no NUL behind it. Nothing read that byte. - Change 35: `SSLConfig` made a `&mut` to array buffers and blobs that JS can reach. - Changes 31, 32 and 38: five safe functions dereferenced a raw pointer argument. - Change 40: `listen()` wrote a field through a raw pointer while a `&Listener` was live. - **Eighteen functions that C or C++ calls take references** (changes 14, 15, 40 and 45). Each message names the call sites and what they pass. - **Two new functions, both without `unsafe`:** `remap_zig_exception_frames` (change 3) and `CAresRecordType::cache` (18). - **Parts of six changes are compiled only for Windows or macOS:** 6 (`uws::Timer::create`), 18, 19 and 20 (`dns_sd.rs`, and one Windows function), 25 (the Windows IPC client) and 37 (`WindowsNamedPipeContext`, `on_dns_poll_uv`). ### Where the compiled code changes I compared the release assembly (x86-64 Linux, `--emit=asm`) of all 134 crates of the workspace, before and after. For 109 of them the files are the same byte for byte, among them `bun_collections`, `bun_dns` and `bun_libuv_sys`, which this PR touches, and `bun_uws`, `bun_resolver` and `bun_spawn`. The other 25 I compared function by function, with names demangled and labels normalised. In 21 of the 25 no function differs: what differs is line numbers in the debug information, the paths of the two checkouts and, in one function of `bun_sql_jsc`, the commit's own hash. | Crate | Functions | Differ | Other constants or names only | Same length | Shorter | Longer | Instructions | Branches | Compares | Calls | Panic call sites | |---|--:|--:|--:|--:|--:|--:|--:|--:|--:|--:|--:| | 21 crates, among them `bun_event_loop`, `bun_uws_sys`, `bun_http`, `bun_http_jsc`, `bun_install`, `bun_io` and `bun_sql_jsc` | 11,451 | 0 | | | | | | | | | | | `bun_watcher` | 67 | 6 | 1 | 0 | 5 | 0 | −34 | 0 | 0 | −1 | 0 | | `bun_bundler` | 1,981 | 3 | 0 | 0 | 3 | 0 | −26 | 0 | 0 | −1 | 0 | | `bun_jsc` | 1,765 | 35 | 3 | 5 | 21 | 6 | −416 | −60 | −45 | −31 | −8 | | `bun_runtime` | 11,204 | 634 | 294 | 87 | 216 | 37 | −1,831 | −47 | −36 | −212 | −8 | The last five columns are net, and count the functions that exist on one side only: 36 only before and 38 only after, which were removed, renamed or inlined, or are no longer inlined. **Three things account for most of the 634 functions of `bun_runtime`.** - **One field has another place (change 46).** `request_pool` is a reference, which cannot be null, and the compiler sorts such a field differently: it is at offset 72 of `NewServer`, where it was at 1568, and what lies between moves by 8 bytes. 261 functions differ only in those offsets. 24 more differ only in constants for reasons of the same kind: the fields of `Listener` (change 40), of `TranspileExtra` (27) and of the DNS requests (19 and 20) are laid out differently, and the tag of `SendQueueOwner` has other values (25). - **A finished job is not copied to the stack (change 10).** `Job::schedule` has two closures per kind of job, and `Job::take` is inline in them. It copied the whole job out of its allocation, freed the allocation and then took the job apart. It now takes the two parts that it returns out of the allocation. 118 closures differ: 113 are between 1 and 25 instructions shorter, 2 have the same length, and 3 are longer: one by 5, and the two of `ReadFile`, which are in the table of checks. 90 of them have one or two calls of `memcpy` fewer. Together: −1,235 instructions. **The order changes:** the rest of the job, which holds its ticket on the VM, is dropped and the allocation freed after the keep-alive is released, not before. All three happen on the JS thread, with no script in between. - **`RequestContext` is compiled eight times,** so each difference in it (changes 33, 34 and 37) shows up eight times. **Checks.** No path makes a check that it did not make before. These are all the functions in which the listing shows more conditional branches, compares or panic call sites than before, and why: | Function | Listing | Why | |---|---|---| | `RequestContext::do_render_with_body`, 8 instances | 543 to 583, +7 branches, +8 compares, +2 calls | `do_render_blob` got short enough (199 to 44, change 33) for the compiler to inline it here, where it was a jump. See change 33 below. | | `RequestContext::on_abort`, the 4 instances for HTTP/2 and HTTP/3 | 415 to 415, +1 branch, +1 compare, −1 call | One lookup of the thread local goes (change 37). The compiler duplicates one test at an exit. The other 4 instances go from 358 to 347, with two lookups fewer. | | The two closures of `Job::<ReadFile>::schedule` | 23 to 57 and 26 to 63, +5 branches, +4 compares, +4 calls each | They called `Job::<ReadFile>::take` (68 instructions), which is now short enough to be inline, as it is for every other job (change 10). | | `GetAddrInfoRequest::on_cares_complete`, `GetNameInfoRequest::on_cares_complete`, `Resolver::drain_pending_host_native`, `DNSLookup::on_complete_native` | 312 to 370, 295 to 326, 304 to 375, 56 to 65; between +1 and +6 branches | They called `DNSLookup::on_complete_with_array` (53) or `CAresNameInfo::on_complete` (44), the first three at four places each. What those did is inline now, at two places each, with the drop of a waiter that `destroy` did (change 20). The two functions no longer exist on their own. | | `Bun__remapStackFramePositions` | 33 to 42, +2 branches, +2 compares, +1 panic site | It builds the slice (change 13). The test for a null pointer and its panic come from `remap_stack_frame_positions` (222 to 216, −1 of each). The second test, of the length, runs only for a null pointer. | | `timer::All::clear_timer` | 229 to 225, +1 compare | Change 29. A value that is not a cell skips two calls into C++. | | `dispatch::run_task::handle` | 3,121 to 3,119, +1 branch, −1 compare | `SendQueueOwner` has another shape (change 25): the deferred close, which is inline here, tests the tag and then the pointer, where it compared one word twice. | | `WebWorker__create` (`bun_jsc`) | 2,135 to 2,175, +4 branches, +4 compares, +4 calls, +1 panic site | The bug fix (change 1). The message for a preload that does not resolve is built here. | | `install_bun_watcher` (`bun_jsc`) | 31 to 33, +1 branch, +1 compare | The compiler duplicates the test that follows the allocation, once per arm (change 12). One per path, as before. | | `EventLoop::run_pending_test_steps` (`bun_jsc`) | 76 to 76, +1 branch, +1 compare | It passes the hook the global object (change 5). The test that the VM has one comes from the hook, `run_pending_test_step`, which goes from 6 to 1, with one branch, one compare and one call fewer. | | `Bun.serve`, the function for the servers without TLS | 1,596 to 1,607, +2 branches, +4 compares, +7 calls, −1 panic site | Changes 36 and 39 take two bounds checks and one panic site away. The path after a failed `listen` (`deinit`, `has_exception`, `throw`) was shared by the two kinds of server, and the compiler now gives each its own copy. One per path, as before. | | `Bun.listen` | 694 to 693, +1 branch, +1 compare, −1 call | The fields of `Listener` are laid out differently (change 40), and the compiler arranges the clean-up of the error paths differently: two drops of the TLS context share one call, and one test of a length is there twice, once per path. | | Change | What differs | Effect | |---|---|---| | 1 | Apart from `WebWorker__create`: `resolve_entry_point_specifier` goes from 364 to 289, with 17 branches and 8 calls fewer, `resolve_entry_point_specifier_quietly` from 81 to 73, with 3 branches fewer, and the worker thread's entry from 1,016 to 945, with 10 branches and 9 calls fewer. | The fix. The resolves that only want a path build no error text. | | 2, 15 | `on_room_timer` goes from 94 to 87, `WebWorker__join` from 18 to 11, `__setRef` from 26 to 20, `__releaseParentPollRef` from 41 to 35 and `__requestTermination` from 86 to 80. Each loses its test for null and the panic behind it. | Same calls. | | 3 | `remap_zig_exception` goes from 2,364 to 2,304, and the guard's `drop` (65) goes. | Same two steps, at the same point. | | 5 | `apply_standalone_runtime_flags` goes from 15 to 16: a move. `run_pending_test_step` is in the table of checks. | Same flags, same step. | | 6 | `tick_while_paused` goes from 22 to 23. **With `&self` the compiler loads and tests the loop's pointer once per turn, where it did so once in front of the loop.** | One load and one test more per tick of the loop, only while a debugger holds the VM paused. | | 6, 7 | `process_gc_timer` and `perform_gc` read the VM from the event loop's field, not through the thread local. In `perform_gc` (26 to 27), `auto_tick` (235 to 240) and `auto_tick_active` (227 to 232) their early exits now come before the lookup of the thread local. **`tick_possibly_forever` (98 to 103) kept the thread local's address in a register across the tick and now asks for it again afterwards.** | One lookup fewer where the GC timer has nothing to do. One more per blocking tick of an otherwise idle loop. | | 9 | `WatchItem` loses 4 bytes. `MultiArrayList::<WatchItem>::ensure_total_capacity` is 14 shorter in each of the four crates that have a copy, without the `memcpy` of the column. `flush_evictions` goes from 259 to 250, and `add_file`, `add_file_by_path_slow`, `append_directory_assume_capacity` the `on_file_update` of the hot reloader, the dev server and the bundler, and `BundleV2::on_parse_task_complete` are between 3 and 6 shorter. This is all that differs in `bun_watcher` and `bun_bundler`. | One increment fewer per file event. | | 10 | Apart from the jobs: `JSValkeyClient::connect` goes from 469 to 464. | Same calls. | | 12 | `TranspilerJob::run` goes from 3,427 to 3,372, with 13 branches fewer. `snapshot_package_json`, `add_to_watcher_if_needed`, `add_main_to_watcher_if_needed`, `report_exception_in_hot_reloaded_module_if_needed`, `enable_hot_module_reloading`, `auto_watch_file`, `TestCommand::exec` and one closure of `bun check --watch` lose one branch each. `bun_watcher_mut` (10) goes. | Same watcher. | | 17 | `join_child_workers` was inline in `teardown`, its one caller, already. The separate copy (40), which was emitted because the function was `pub`, goes. `teardown` goes from 516 to 514 (with 6, 8 and 12). | Same joins. | | 18 to 20, 22 | 57 functions of the DNS code differ and three go, by −454 instructions together. Eleven of the twelve `CAresLookup::process_resolve` are 12 or 19 shorter and the twelve callbacks of `ResolveInfoRequest` 17 or 18, each with 3 branches fewer: the tests of `allocated` and of the optional resolver. `CAresReverse::destroy` and two `on_complete` functions go. Five functions are longer: the four in the table of checks, and `process_resolve` for `ANY` (155 to 159, one branch fewer), which has the drop of its reply inline. | Same results. A waiter is freed where `destroy` freed it. | | 21 | `Bun.dns.prefetch` goes from 54 to 9 and the test-only `seedCacheForTesting` from 713 to 670. | One allocation, one copy and one free fewer per `prefetch`. | | 23 | `getaddrinfo`, which has `addrinfo_chain` inline, is the same 543 instructions. | None. | | 24, 27 | `transpile_source_code_of_any_provenance::<false>` goes from 4,586 to 4,573, with 2 branches fewer, and `transpile_source_code_inner::<true>` from 2,709 to 2,682, with 6 fewer. | One store fewer after the close. | | 25 | 16 functions of the IPC code differ, one goes and two come, by −10 together. `SpawnIPC::on_close` goes from 19 to 6, `close_socket` from 59 to 52 with 4 branches fewer, and `continue_send` from 406 to 398. `handle_ipc_message` goes from 623 to 629, with no branch, compare or call more. The compiler inlines differently: `schedule_deferred` (49) is inline, and `close_socket_next_tick` (51) and `socket_closed_notify` (57) are functions of their own. | Same messages and the same close. | | 28 | `__bun_fire_timer` goes from 1,668 to 1,669. | Same time. | | 30 | The `blob_store_path` thunk goes from 5 to 2. | Same slice. | | 33 | Each of the ten cork sites loaded the context's pointer to itself from a field and now passes the `self` that it has. `render_metadata_corked`, a single jump, goes. **What the compiler inlines changes:** `honor_embedded_file_range` (151) was inline in `do_render_blob` and is a function of its own, called only for a request with a `Range` header. `do_render_blob` goes from 199 to 44 and is inline in `do_render_with_body`. `do_render_stream` is 7 longer (616 to 623, and 597 to 604 in the four instances of the debug servers: moves to and from the stack), and the handler of `render_missing` goes from 95 to 98 (in those four it keeps its 118). `AnyRequestContext::write_chunk`, which has one cork site of each of the eight instances inline, goes from 579 to 539. `bun_uws_sys` compiles to the same code. | A response with a body saves a jump and five pushes and pops. One with a `Range` header makes one call more. | | 34, 37 | `on_node_http_request` goes from 459 to 455, with 4 branches fewer. It looked the thread local up once, at its start, and kept the address. It now does so at the four places that use it: the two calls of `uncaught_exception`, and the two releases of a keep-alive (for a body that nobody reads, and for a request that ends inside the dispatch after a throw or an upgrade). `deinit_if_we_can` goes from 186 to 173 and the servers' finalizers from 80 to 61, with two lookups fewer each. `schedule_background_pull` is 1 shorter. | A `node:http` request that reaches none of the four makes no lookup, where it made one. | | 36, 39 | `Bun.serve` is three functions. The two for the servers with TLS go from 1,331 to 1,316, with 3 branches and 3 panic sites fewer each. The third is in the table of checks. | Same names and paths. | | 40 | `Bun.listen` is in the table of checks. | Same port. | | 42 | `ServerWebSocket::init` goes from 66 to 67 and the closure of `upgrade()` that calls it from 219 to 220. | **One instruction more in each per `server.upgrade()`:** the flags are an argument. | | 43 | `NodeHTTPResponse.write` goes from 661 to 653, with one branch fewer. | Same bytes. | | 45 | `NodeHTTPResponse__createForJS` goes from 190 to 189. | Same values. | Changes 13, 29 and 46 are described above. Changes 4, 8, 11, 14, 16, 26, 31, 32, 35, 38, 41 and 44 ask for the same operations in the same order. Where they show up at all, it is in registers: `print_error_instance_js` keeps its 419 and `connect_finish` its 668. What changes 6, 18, 19, 20, 25 and 37 do to code for Windows or macOS is not compiled on Linux. The comparison covers x86-64 Linux only, one crate at a time. The build that ships also inlines across crates when it links, and there a lookup of the thread local is two instructions, not a call. ### The changes in detail #### 1. worker: `new Worker()` names the preload that does not resolve `WebWorker::create` gave the transpiler the address of a temporary `Log` and then moved that `Log` into a scope guard. The resolver wrote its error into the slot the log had been moved out of, which was never dropped, and the error message was built from the empty copy: the constructor threw `TypeError: undefined`. The `Log` now stays where it is, and the guard only restores the previous log pointer. `resolve_entry_point_specifier` takes neither the log nor a string to fill. It returns why it failed: a text of its own, or the resolver's error, which is in the log the transpiler points at. Only the preload loop turns that error into a message. The two resolves in `create()` that only want a path create no JS objects for a text nobody reads. Neither does `spin()`, whose log already has the error and is reported by `flush_logs`. #### 2. worker: the helpers that walk a parent's children take `&VirtualMachine` `has_ending_child`, `has_returning_child`, `has_room_to_wait_for`, `join_returning_children` and `start_waiting_children` were `unsafe fn`s over `*mut VirtualMachine` whose whole contract was "the calling thread's VM". They only read the VM, and a `&VirtualMachine` cannot leave its thread, so the reference states that contract. `create()` passes the reference it already has. `on_room_timer` takes `&self` and the VM whose timer heap fired it, which is the worker's parent, instead of a pointer it tested for null and a second pointer it read from the worker. It stays `unsafe`: it must only be called on a worker that waits for room. #### 3. jsc: `remap_zig_exception` has no `Tail` drop guard A `Tail` drop guard held `self` and `exception` as raw pointers and turned them back into `&mut` in `Drop`, while the body went on using `self` and a third `&mut` made from the guard's pointer. The guard existed so that the last two steps also ran after the body's one early `return`. Panics abort, so it never ran on any other path. The body moves into a private method, `remap_zig_exception_frames`, whose only caller is `remap_zig_exception`. The two steps follow the call and use the function's own `self`, `exception` and `exception_list`, so they still run whichever way the body returns. The preview flag is a `&mut bool` instead of a `Cell`, which is the only change inside the body. #### 4. jsc: `print_error_instance_js` keeps the parser-arena flag in a local It turned the `&mut ZigException` it got from the holder into a raw pointer and dereferenced that twice, only so that it could also lend out the holder's `need_to_clear_parser_arena_on_deinit` field. `remap_zig_exception` now writes the flag into a local, which is copied into the holder before `deinit`, its only reader. This is what `write_trace` already does, and `exception` stays a plain `&mut`. #### 5. jsc: four `RuntimeHooks` slots take the reference their caller holds `process_exit`, `create_node_fs` and `apply_standalone_runtime_flags` took raw pointers. Every caller made the pointer from a reference, and the first thing each hook did was turn it back into one. `run_pending_test_step` took the VM as a raw pointer only to read its `global()`, and its caller holds a `&VirtualMachine`. The slots now take `&JSGlobalObject`, `&mut VirtualMachine`, `&mut Transpiler` and `&JSGlobalObject`. The third stays `unsafe` because it downcasts the module graph. The others have no other contract and are safe functions. #### 6. jsc: borrow the uws loop as `&Loop`, not `&mut Loop` Every `uws::Loop` method takes `&self` and every field is a `Cell`, because the callbacks that `tick` and `close_all_groups` dispatch reach the loop again. `VirtualMachine::uws_loop_mut`, `platform_loop_opt` and `hold_forever_poll` still dealt in `&mut Loop`, which claims an exclusive borrow across those calls. - `uws_loop_mut` returns `&Loop` and is now called `uws_loop_ref`, like the accessor on `HTTPThread`. `platform_loop_opt` returns `Option<&_>`. - `hold_forever_poll`, and `uws::Timer::create` on Windows, take `&Loop`. `us_create_timer` then asks for nothing but a live loop on its own thread, so it is a `safe fn` like `us_timer_ext` and `us_timer_set`. - `tick_possibly_forever` borrows the loop once instead of dereferencing the pointer four times, and teardown uses the accessor. - `tick_while_paused` only reads the event loop, so it takes `&self`. - On Windows `platform_loop_opt` hands out the libuv loop, on which `has_handles` calls `uv::Loop::has_active_io_handles`. That takes `&self`: of the loop, `uv_walk` writes only `handle_queue`, which becomes a `Cell` like `active_handles`. #### 7. jsc: `GarbageCollectionController::init` takes `&VirtualMachine` `init` takes `&self` and only reads `vm.jsc_vm`, but asked for `&mut VirtualMachine`. To pass that next to the controller, which is a field of the VM, `ensure_waker` projected the field through a raw pointer and made a `&mut` of the whole VM from inside the VM's own event loop. With `&VirtualMachine` both are shared borrows of the reference `ensure_waker` already uses. `process_gc_timer` and `perform_gc` take `&self` too, so their callers no longer go through `as_mut()`. #### 8. jsc: `RareData::close_all_socket_groups` has no `self` It took `&mut self`, discarded it with `let _ = self;` and worked only on the VM's uws loop. To call it, the stop phase made a `&mut RareData` and a `&VirtualMachine` from the same pointer, and the `&mut` stayed a live argument while socket close handlers reached `RareData` again. It is now an associated function of the VM reference. The caller still runs it only when the VM has `rare_data`. #### 9. watcher: remove `WatchItem.count` The field was set to zero when an item was added and incremented for every file event by the hot reloader and the dev server. Nothing ever read it. To increment it, the hot reloader built a `&mut [u32]` out of a `&WatchList` with `unsafe`, and the dev server did the same through `items_mut`. The field, its two initialisers and both increments are gone. #### 10. jsc, valkey: drop three detours through a raw pointer - The `ArenaReset` guard in `fetch_without_on_load_plugins` turned its `&mut VirtualMachine` into a pointer and back to call `reset_arena`, a safe function. A plain reborrow does. - `JSValkeyClient::connect` took `&mut VirtualMachine` first, so it had to launder the context it borrowed from the VM through a pointer. It now keeps the client's `&'static VirtualMachine` and calls `as_mut()` only for `client_socket_groups_in`, as `socket_body.rs` does. - `Job::take` makes the `Box` first and unlinks the box's header, instead of dereferencing the raw pointer for the unlink and boxing it afterwards. The allocation is now freed after the keep-alive is released, not before. #### 11. worker: remove `unsafe impl Send for ThreadStart` A `ThreadStart` never crosses a thread by value. What the `spawn` closure captures is `OwnedByThread`, a pointer to it with a `Send` impl of its own, so no bound needed the impl on `ThreadStart`. Its SAFETY text, which `OwnedByThread` referred to, moves to `OwnedByThread`. The inventory of the thread-door source lint loses the entry. #### 12. hot reload: remove `ImportWatcher::None` The variant was never constructed: `vm.bun_watcher` is null until `install_bun_watcher` stores a `Hot` or a `Watch`. Every match still carried an arm for it, and the `HotReloaderCtx` impl and the transpiler job dereferenced the pointer just to rule it out. Without the variant "is a watcher installed" is the null test that `VirtualMachine::is_watcher_enabled` already was, and the trait method calls that. The transpiler job kept the answer in a flag and then matched the same `Option` again. It matches once, in the place of the flag. The dead arms go, and two `if let`s that can no longer fail become `let`s. #### 13. jsc: `remap_stack_frame_positions` takes `&mut [ZigStackFrame]` It was an `unsafe fn` over a pointer and a count, and the first thing it did with them was build the slice. Its only caller is the function exported to C++, which had an `unsafe` block of its own for the call. The export now builds the slice, where the pointer from C++ arrives, and the method is safe. #### 14. jsc: eight functions exported to C++ take references These functions took raw pointers and their only `unsafe` act was `&*p`, `&mut *p` or `*out = x`. None has a Rust caller. A reference has the same ABI and puts the contract in the signature, as `Bun__VmHandle__retain` and `Bun__setExitCode` already do. - Out-parameters become `&mut`: `Bun__standaloneInternalModuleBytecode`, `Bun__standaloneModuleKey`, `Bun__standalonePrelinkedModuleGraph` and `Bun__standalonePrelinkedModuleName`. - The VM becomes `&VirtualMachine` in `Bun__VirtualMachine__replaceEnteredContext` and `&mut VirtualMachine` in `Bun__ScriptExecutionContext__create`. - The handle becomes `&Shared` in `Bun__VmHandle__scriptAllowed` and `Bun__VmHandle__stateAddress`. All but `Bun__standaloneModuleKey`, which still builds a slice from a pointer and a length, are now safe functions. The C++ call sites are `generateInternalModuleFromText`, `StandaloneGlobalObject::moduleLoaderResolve`, `JSVMClientData::prelinkedModuleGraph` and `prelinkedModuleKey`, which pass the addresses of distinct initialised locals, the constructor and destructor of `ErrorHandlerContextScope` and the `ScriptExecutionContext(parent)` constructor, which pass the thread's `bunVM`, and `runPendingWork` and `JSVMClientData::create`, which pass the client data's `vmHandle`. #### 15. worker: the exports the messaging proxy calls take `&WebWorker` `WebWorker__join`, `__setRef`, `__requestTermination` and `__releaseParentPollRef` took `*mut WebWorker`, tested it for null and wrapped it in a `ParentRef` only to use it as a `&WebWorker`. Their callers are `terminateWorkerGlobalScope`, `setKeepAlive` and `releaseWorkerThread` in WorkerMessagingProxy.cpp. Each returns early when `m_workerThread` is null, and the proxy holds a ref on the worker until it calls `WebWorker__deref`, so the parameter can be the reference. #### 16. jsc: the GitHub-annotation guard borrows the exception `print_error_instance_body` kept the `ZigException` for its deferred annotation in a `BackRef`, a pointer without a lifetime. After the guard is made the function only reads the exception, so the guard can hold a plain `&ZigException` and the compiler checks what the field's comment asserted. #### 17. worker: `join_child_workers` takes the list of children, not the VM It took `&mut VirtualMachine`, moved `child_workers` out of it and then called into C++ for each child. C++ comes back through `WebWorker__releaseParentPollRef` and `WebWorker__join`, which reach the same VM through the worker's `parent` pointer and read that same field. So the VM was read through another pointer while the `&mut VirtualMachine` parameter, which promises that nothing else touches it, was still live. `teardown` now takes the list out of the VM, inside the `unsafe` block it already has, and hands it over. That borrow of the field ends before the first child is released, and the function holds no reference to the VM. Its only caller is in this crate, so it is `pub(crate)`. #### 18. dns: the pending caches are borrowed shared, and each record type names its own `pending_host_cache`, two of the `HasPendingCacheKey::pending_cache` impls and the Windows `cancel_pending_uv_requests_for_teardown` made a `&mut HiveArray` out of `&Resolver` with `JsCell::get_mut`. Every `HiveArray` method that they or their callers use takes `&self`, so they borrow `&HiveArray` with the safe `JsCell::get`. `get_key_addr`, `get_key_nameinfo` and two places in dns_sd.rs took the hive `&mut` through `with_mut` for the same `&self` methods; they use `get` too. `pending_cache_for::<T>` made its `&mut HiveArray` another way: it matched on `T::CACHE_FIELD`, took `JsCell::as_ptr` of the chosen field and pointer-cast it to the `HiveArray` of `T`. Each field already has exactly that type, so `CAresRecordType` gets `fn cache(&Resolver)`, in which each impl names its field, and the function and its cast are gone. Nothing else read `CACHE_FIELD`: it goes, with the `field` parameters that only passed it along and the fourteen `PendingCacheField` variants that only they used. `ResolveInfoRequest::init` and `GetAddrInfoRequest::init` used to retag the whole hive `&mut` and then write through the older slot pointer `new`. With a shared borrow that order is fine. #### 19. dns: the request and lookup-node constructors take `&Resolver` Every caller holds a `&Resolver` and passed `Some(this.as_ctx_ptr())` (or the bare pointer, for `DNSLookup::init`). The constructors then took their ref with `resolver.map(|r| unsafe { RefPtr::init_ref(r) })` and reached the pending cache through `(*resolver.unwrap())`. They take `resolver: &Resolver`: the ref comes from `resolver.ref_guard()`, the cache is read through the reference, and `resolver_for_caching` is stored once at construction, not a second time in the `New` arm. #### 20. dns: lookup nodes are completed through `&mut self` and freed by their owner `CAresNameInfo`, `CAresReverse`, `CAresLookup<T>` and `DNSLookup` are either the inline `head` of a request or a boxed waiter chained behind it. Their completion functions were `unsafe fn(this: *mut Self, ..)` that ended in `Self::destroy(this)`, which freed `this` if its `allocated` flag said it was a waiter. The caller always knows which of the two it holds, so the functions take `&mut self` and free nothing. A head is completed in place and dropped with its request as before. A waiter is completed as `heap::take(waiter).on_complete(..)`: the temporary box drops at the end of that statement, where `destroy` ran. `destroy`, `allocated` and `CAresLookup::new` are gone. `init` returns `Box<Self>`, `append` takes the box and releases it into the list, and the callers read the promise before they hand the box over. The functions also moved the promise to a local with `mem::take` before they settled it; they settle `self.promise` where it is. Nothing else uses a node while it is completed: each drain empties the cache slot and reads `next` first, the dns_sd path has removed the request from `inflight`, and `LibcRequest` is consumed. c-ares has the request as the `arg` of its query, but the pinned c-ares takes a query off all its lists before it runs the callback (`end_query`, `ares_flush_requeue`, `ares_cancel`, `ares_destroy`), so a cancel or destroy from inside a completion does not call back for the same request. Not changed here: a c-ares thunk forms a `&mut` to the whole request and calls `on_*(&mut self)` on it, and under that borrow the drain uses and frees the request through `key.lookup`. `process_get_addr_info_native` loses its `result` parameter, for which every caller passed null. The three functions that take a raw c-ares result stay `unsafe fn`, with that as their whole contract. #### 21. dns: the connect-path cache key borrows `&[u8]`, not a `&ZStr` `__bun_dns_prefetch(loop_, hostname: *const u8, len, port)` built its key with `ZStr::from_raw(hostname, len)`, whose contract is `ptr[len] == 0`. Its one caller, `bun install`, passes the hostname of the registry URL, a sub-slice of the URL text with no NUL after it, so that contract was broken. It had no visible effect: nothing read the byte after the slice. Nothing needs the NUL there. The borrowed `RequestKey::host` is only hashed, compared and logged as bytes, and `to_owned()` makes the NUL-terminated copy that `getaddrinfo` gets. So the key, `getaddrinfo` and `prefetch` take `Option<&[u8]>`, the extern takes `hostname: &[u8]`, and `us_getaddrinfo` reads its C string with `ffi::cstr(..).to_bytes()`. `prefetch_from_js` and `seed_cache_for_testing` no longer allocate a NUL-terminated copy only to borrow it. #### 22. dns: `get_channel_or_error` returns the `&mut Channel` that `get_channel` gave it `get_channel` hands out `&mut c_ares::Channel`. `get_channel_or_error` turned it into `*mut`, and its callers turned it back with `unsafe { &mut *channel }` for `ares_process_fd`, `ares_set_local_ip4`, `ares_queue_active_queries` and `ares_cancel`. It and `get_channel_from_vm` now return the reference, and the `*_channel_*` helpers take `&mut Channel`, which coerces to `*mut` at the raw FFI calls. `reverse`, `do_resolve_cares` and `c_ares_lookup_with_normalized_name` put `get_channel`'s reference in a `*mut` local and called through `(*channel)`; they keep the reference. `Channel` is a zero-sized opaque type, asserted in c_ares.rs, so a callback that derives its own reference to the same channel cannot conflict with this one. `close_channel_for_terminate` took a `RefPtr` guard from `this` and then dereferenced `this` in two more blocks. It calls through the guard. #### 23. dns, ipc: fill a fresh `Box` before releasing it, and two more raw hops `Resolver::init` and the connect-path `Request::new` returned `heap::into_raw(Box::new(..))`, and `new_resolver` and `seed_cache_for_testing` then set fields of the object they had just made through the raw pointer. Both constructors return `Box<Self>`; the callers fill it in safely and release it where the raw pointer is needed (`to_js_ptr`, the global cache). `addrinfo_chain` linked its nodes with `(*base.add(i - 1)).ai_next = base.add(i)`. The same loop writes `nodes[i - 1].ai_next = &raw mut nodes[i]`. `i` runs over `1..nodes.len()`, so the compiler drops both bounds checks. `InternalMsgHolder::flush` took `self.messages` through the laundered `this` pointer. It takes it from `self` before that pointer is formed. #### 24. module loader: the guard that closes the input fd owns the fd and its flag `transpile_source_code_of_any_provenance` kept `should_close_input_file_fd` and `input_file_fd` as two locals, gave a scopeguard a `*mut` to each, and reached both only through those pointers afterwards, with an `unsafe` block per use, so that a fresh `&mut` would not invalidate the guard's pointers. The guard's payload is now the `(bool, Fd)` pair itself, and the two names are `&mut` borrows of it. The parser's `file_fd_ptr` reborrow ends with the parse call, since `ParseResult` does not carry that lifetime. The guard is declared where it was, so it still runs before `arena_guard`. The store of `Fd::INVALID` after the close, which nothing could read, is gone. #### 25. ipc: `SendQueueOwner::Instance` carries no pointer The variant held a `NonNull<IPCInstance>`, dereferenced only to call `IPCInstance::handle_ipc_close(&self)` and `handle_ipc_message(&self, ..)`. Neither touches `self` (both work on the thread's VM), and every other match arm ignored the pointer. It is a unit variant and the two functions have no receiver. With no back-pointer to fill in, `get_ipc_instance` passes the owner to `SendQueue::new` and reads the queue's address from the `RefPtr` before boxing the instance, and `set_owner`, which had no other caller, is gone. `emit_handle_ipc_message` only asks whether there is an instance. #### 26. jsc: the `retroactively_report_discovered_tests` hook takes `&mut TestReporterHandle` The `RuntimeHooks` slot was `unsafe fn(agent: *mut TestReporterHandle, i32) -> i32`, and its body wrote `unsafe { &mut *agent }` at both places where it reports a test. Its one caller has just stored the live handle, so it makes the reference once, in the block it already had, and the hook is a safe fn that takes `&mut TestReporterHandle`. The handle is a zero-sized opaque type. #### 27. module loader: `TranspileArgs::extra` is `&TranspileExtra` The field was `*mut TranspileExtra`, read through an `unsafe` block at every use, "so the `.wasm` re-entry can mutate `loader` and recurse". That re-entry, which sets `loader` and `module_type`, was the only write. The field is a shared reference, and those two fields of `TranspileExtra` are `Cell`s. The re-entry makes the same two stores with `Cell::set` and recurses with the same `args`. The five places that build a `TranspileExtra` wrap the two values in `Cell::new`, and the one place that reads them uses `get`. #### 28. dispatch: `__bun_fire_timer` takes `now: &Timespec`, and one poll arm reuses `poll_ref` `EventLoopTimer::fire`, the only caller of `__bun_fire_timer`, holds `now: ×pec` and passed it to a `*const` parameter, which the arms turned back into a reference with `&*now`. The extern declaration, the function and the arm macros take `&ElTimespec`. The BunTest arm also copied the two fields into a `bun_core::Timespec`; `ElTimespec` is a re-export of that type, so it passes `now` on. `__bun_run_file_poll` starts with `poll_ref = &mut *poll`. The MEMORY_PRESSURE arm made the same reference a second time from the raw pointer; it uses `poll_ref`. Its handler reaches the poll, which it may free, only through that reference. #### 29. timer: `clear_timer` downcasts with `as_class_ref`, as it needs only `&T` `clear_timer` collected an `Option<*mut TimerObjectInternals>` with `addr_of_mut!`, tested it, and dereferenced it once to call `cancel(&self, vm)`. It collects a `&TimerObjectInternals`: the object branches downcast with `as_class_ref`, the id branches borrow the field in the block they already had, and the branch that produced `None` returns directly. `as_class_ref` tests `is_cell()` before the `fromJS` call, which `from_js` alone does not. `is_string_literal()` has just made the same test on the same value. A value that is not a cell now skips the two `fromJS` calls. #### 30. jsc_hooks: drop lifetime erasers and a raw round trip that change nothing `get_loader_and_virtual_source` widened `path.text` to `&'static [u8]` with `from_raw_parts` before storing it in `virtual_source_to_use`. That slot is an `Option``<Source<'a>>' and 'path.text' is '&'a [u8]', which the blob branch above already stores there as it is.`` In the `VmLoaderCtx` impl, `main`, `blob_store_path` and `blob_shared_view` rebuilt the slice they return to make it `'static`. `(*this)` is a raw place, whose borrow is unbounded, and `blob(b)` is `&'static Blob`, so the slices have that lifetime already. `load_preloads` took `&raw const` of a preload's bytes and turned it back into a reference in a second block. It borrows them in one. #### 31. timer, jsc_hooks: three `*mut` parameters whose contract a reference states `DateHeaderTimer::enable` was an `unsafe fn` and `All::update_date_header_timer_if_necessary` a safe fn with a `# Safety` section, both only because they took `vm: *mut VirtualMachine`, of which they read `uws_loop()`. They take `&VirtualMachine`, `enable` is safe, and the three callers write `&*vm` in the block they already had. `enable` reaches the loop through `uws_loop_ref()`, as `DateHeaderTimer::run` does. No JS runs under that borrow, and the timer heap it updates is not part of the VM. `normalize_specifier_for_loader` was an `unsafe fn` over `jsc_vm` that read only `origin.host` and `origin.path`. It is a safe fn that takes `&URL`. `TimerObjectInternals::run(this: *mut Self, ..)` opened with `&*this`, and both callers hold that same `&Self` across the call. It takes `&self`. Nothing makes a `&mut` to a live timer (`init` and `deinit` are the only `&mut self` methods) and the callers pin the parent, so the JS callback can only reach it through another `&Self`. It stays an `unsafe fn` for `vm` and `thrown`. #### 32. timer: `increment_timer_ref` and `increment_immediate_ref` take `&Loop` Both were safe functions with a `# Safety` section: they took `uws_loop: *mut Loop` and dereferenced it in two blocks each. `Loop::ref_` and `unref` take `&self`, so the parameter is `&Loop` and the functions have no `unsafe` and no contract left. The five callers got the pointer from `VirtualMachine::uws_loop()`. They now call `uws_loop_ref()`, which returns the same pointer as a `&Loop`. #### 33. server: `run_corked_with_type` takes `fn(&U)` and `&U` Every cork site in `RequestContext` held `&self`, erased it to a `*mut Self` (or the address of a stack `StreamPair`/`HeaderResponsePair` to a `*mut`) for `run_corked_with_type(fn(*mut U), *mut U)`, and the callee opened with `unsafe { &*ptr }` to get the same reference back. The handler and its context are typed `fn(&U)` and `&U`: the same two words in the stack tuple that the trampoline reads. The sites pass `self` or `&pair`, and the callees take `&self` or `&Pair`. `do_render_stream` asked for `&mut` on its pair and never wrote through it. `render_metadata_corked` and `drain_response_buffer_and_metadata_corked` only turned the pointer into a reference, and the closures in `render_missing` and `do_render_blob` only passed theirs on. All four go, and the methods behind them are the handlers. #### 34. server: `NewServer` request dispatch takes `&self` `prepare_js_request_context`, `on_saved_request`, `handle_request`, `on_request`, `on_node_http_request` and their `_for` twins took `this: *mut Self` and each re-derived `&*this` one to three times. Every field of `NewServer` is a `Cell`/`JsCell` or fixed after `init()`, every callee already takes `&self`, and the HTTP/2 and HTTP/3 path even started from `&self`, cast it to `*mut` and dereferenced it again. They are `&self` methods. The two `extern "C"` trampolines turn the uWS user data into the reference, like `on_listen`, and `AnyServer` dispatches through its tag-checked accessors. `RequestContext::create` gets `&ThisServer` for its `BackRef`. `on_user_route_request` keeps its raw `*const UserRoute` and forms the server reference once. `on_node_http_request` returns early on a null `node_http_response`, so it forms one `&NodeHTTPResponse` after that instead of five behind null tests. #### 35. tls: `SSLConfig` reads buffers and files through `&`, not `&mut` `handle_file` and `handle_file_array` wrote `unsafe { &mut *val.get() }` on an `ExternalShared` to fill `SingleFile::Buffer(&mut JSCArrayBuffer)` and `File(&mut Blob)`, which `handle_single_file` only reads. Both are refcounted objects that JS can reach, so `&mut` was the wrong type as well. `SingleFile` holds shared references, filled through `ExternalShared`'s `Deref`, and is `Copy`. `JSCArrayBuffer::as_array_buffer` takes `&self`: `JSC__ArrayBuffer__asBunArrayBuffer` only reads the buffer. #### 36. server: `attach_http2` takes `&self` It took `this: *mut Self` only because its two failure arms called `Self::deinit(this)`; everything else read `(*this).field` in four `unsafe` blocks. Both callers in `listen()` hold the raw pointer and return `JSValue::ZERO` on `false`, so they free the server there, in the same order (throw, deinit, return), and `attach_http2` is a `&self` method. #### 37. jsc: `VirtualMachine::{enqueue_task, tick, drain_microtasks}` take `&self` Their bodies are only `self.event_loop_mut().x()`, and `event_loop_mut` takes `&self`. The `&mut self` receiver made the server write `unsafe { (*vm).drain_microtasks() }` on the raw thread-local pointer, which is a `&mut VirtualMachine` live while script runs. `on_node_http_request` drains through the `&VirtualMachine` it already enters its scopes with, and reads `vm_mut()` only where it reports an uncaught exception. `schedule_deinit` enqueues through `server.vm()`. `RequestContext::on_abort`, `RewriterPipe::schedule_background_pull` and `Resolver::on_dns_poll_uv` drop the `.as_mut()` on the `&VirtualMachine` they hold, and `WindowsNamedPipeContext` its cast of a `&'static VirtualMachine` to `*mut`. Every other caller is unchanged, the `.as_mut().tick()` ones included. #### 38. server: `write_chunk` and `on_request_body_stream_drained` take `&self` Both took `this: *mut Self` and opened with `unsafe { &*this }`: two safe functions that dereferenced a raw argument. They are `&self` methods. No dereference goes away. `AnyRequestContext` is their only caller, and the two move into the reference arm of its `dispatch!` macro, which does the same tag match and cast and already serves thirteen other methods. What is gained is the two signatures. `end_chunk` stays on the raw-pointer arm: it adopts the pipe's ref, which needs the pointer it was given. #### 39. server: `listen()` borrows the SNI names and the unix path The SNI loop indexed `config.sni`, kept `(name_ptr, name_len)` and rebuilt a `&CStr` and a `&ZStr` from them; `Addr::Unix { ptr, len }` was rebuilt into a `&ZStr` further down. Both only erased a borrow of `this_ref.config`. `this_ref` is a `Copy` `BackRef` local that lives to the end of the function and `Self::deinit(this)` takes the raw pointer, so the borrow checker accepts the plain form, as it does for `server_name` a few lines above. The loop iterates the slice, keeps the `&CStr` and uses `ZStr::from_cstr`; `Addr::Unix` holds `&ZStr` from `ZBox::as_zstr`. #### 40. socket: `Listener` types its late write and its SNI out-parameter `listen()` only knows the bound port after the wrapper exists, so it wrote the plain `connection` field through the raw payload pointer while `this_ref: &Listener` was live. The field is a `JsCell` like its siblings (`repr(transparent)`), assigned through `this_ref` in `with_mut`: the same drop of the placeholder, then the same store. `decode_sni_result` tested `abort_handshake: *mut c_int` for null and wrote through it in three places. The parameter is `Option<&mut c_int>`, the same ABI, test and store, from the top down: in `us_dispatch_socket_server_name`, `us_dispatch_server_name`, the callback type of `ListenSocket::on_server_name`, and `dispatch_server_name`. The call site: `us_select_cert_cb` (openssl.c) passes `&abort_handshake`, a local initialised to 0, to one entry point or the other. Nothing else has its address, so the JS callback cannot reach it, and a nested handshake has its own. #### 41. socket: drop round trips through raw pointers The TLS upgrade picked its `SocketGroups` in an `if` whose arms both yield `&mut SocketGroups`, cast the first to `*mut` to name a common type and dereferenced the result again. The `if` yields the reference. `dgram_get_sock_name_fd` rebuilt the address bytes in both arms with `from_raw_parts((&raw const addr.addr).cast(), len)`. The IPv6 `addr` is a `[u8; 16]`, so `&addr6.addr` is that slice. The IPv4 one is a `u32`, read through `bun_core::bytes_of`. #### 42. server: `upgrade()` guards with the reference it holds; `ServerWebSocket::init` takes its flags `upgrade()` already holds `upgrader: &RequestContext` and uses it after the option getters ran, but its scope guard kept the raw pointer and dereferenced it again for the paired `deref()`. The guard holds the reference. `init_for_ws_package` called `init` and then `unsafe { &*this }` on the returned pointer just to set one flag bit. `init` takes the initial `Flags`, so the struct is built with the bit set. Nothing reads it before `close()`. `server.upgrade()`, the other caller, passes one more argument for it: it zeroes a register, and `init` stores the flags from that register instead of an immediate. #### 43. node:http: slice the unwritten tail of a zero-copy write The tail was built with `slice_from_raw_parts(bytes.as_ptr().add(consumed), bytes_len - consumed)`. It is `&bytes[consumed..]`, as the `else` arm wrote it and `drain_pending_pinned_write` does. It is sliced once, directly under the early return for `consumed >= bytes_len`, and both arms use it. `bytes_len` is `bytes.len()` of the same local, so that comparison implies the bounds check. #### 44. tls: `no_password` is a safe `extern "C" fn` The PEM password callback ignores its arguments and returns 0, so it has no contract for a caller to uphold. A safe `extern "C" fn` item coerces to the `unsafe extern "C" fn` pointer that `PEM_bytes_read_bio` takes. #### 45. server, socket: type the out-parameters of four exported functions as references `NodeHTTPResponse__createForJS`, `NodeHTTPResponse__adoptRawRequestHeaders`, `us_bundled_root_cert` and `Bun__parseIpAddress` took raw pointers that each dereferenced once in an `unsafe` block. They take `&mut bool` and `&mut *mut NodeHTTPResponse`, `&NodeHTTPResponse`, `&mut usize`, and `Option<&mut MaybeUninit<sockaddr_storage>>` (it keeps its null test). The ABI is the same, and `us_bundled_root_cert` is a safe `extern "C" fn`. `createForJS` also keeps its `Box` until both `jsc::Ref`s are taken instead of dereferencing the pointer it has just leaked. The call sites: `NodeHTTPServer__onRequest` (NodeHTTP.cpp) passes `&hasBody`, a local initialised to `false`, and the `&mut node_http_response` it got from `on_node_http_request`, then `*nodeHttpResponsePtr`, which `createForJS` has just set; `us_bundled_roots` (root_certs.cpp) passes `&roots.lens[i]` of the vector it has just resized; `try_parse_ip` (context.c, for `&local_addr_storage` and `&addr` of `us_socket_group_connect`) and `us_quic_socket_context_connect` (quic.c, `&peer_ss`) pass uninitialised `sockaddr_storage` locals. #### 46. server: `request_pool` is a `&'static` pool The HTTP/1 `RequestContext` pool is a leaked thread-local allocation that is never freed, and `claim`/`put` take `&self`, but the server stored it as `*mut Pool` and wrote `unsafe { (*server.request_pool).claim() }` for each request. The field is set in `init()` and never null, so it is a plain `&'static Pool`: the same load and call, with no `Option` in between. `Fallback::new_boxed` returns `Box<Self>` like `HiveArray::new_boxed`, and `request_pool()` leaks it. Its two other callers, which turned the leaked pointer straight back into a `Box`, use `Box::into_raw`. `mux_request_pool` is null until `listen()` and stays raw. ### How did you verify your code works? - **Static checks on the final commits:** `cargo fmt --check`, `cargo clippy --workspace` (0 warnings), the source lints and `cargo check --workspace --release` on Linux, and `cargo check --workspace --release` for `x86_64-pc-windows-msvc` and `aarch64-apple-darwin`, because six changes touch code that only those targets compile. - **Assembly:** the comparison above. - **The test of change 1** fails on a debug build of the base (`TypeError: undefined`) and passes on a debug build of these commits, with `BUN_JSC_validateExceptionChecks=1` too. - **30 test files on the final commits**, on a debug build with ASAN, all at once and each cut off after 50 seconds: the files for workers, DNS, timers, fake timers, `vi` utilities, sockets, UDP, `Bun.serve`, WebSocket servers, IPC, `--watch` and the resolver. - 21 files pass completely, 1,432 tests. - 4 files fail in one test each, and in the same test on a debug build of the base: `resolve` (needs `tsyringe`), `vitest-apis` (needs `jest-extended`), `socket` (needs the public internet) and `setInterval` (a memory test that a debug build does not finish in its 30 seconds). - `setImmediate.test.js` failed in one test, which gives a child process 500 ms to start and exit. On its own the file passes, five times out of five. - 4 files were cut off: `hot`, `worker_threads`, `setTimeout` and `error-gc-test`. - **422 test files before the last rebase.** The rebase brought in 31 commits of `main`. In it I adapted three of the commits here to `main`'s new code (1, 27 and 31), and a review after it changed six (1, 5, 6, 28, 30 and 31). The 422 files ran on a debug build with ASAN of the commits as they were and, where something failed, on a debug build of their base: all of `test/js/web/workers/`, `test/js/node/worker_threads/`, `test/js/bun/dns/`, `test/js/node/dns/`, `test/js/web/timers/`, `test/js/bun/net/`, `test/js/node/net/`, `test/js/node/tls/`, `test/js/bun/http/`, `test/js/node/http/`, `test/js/node/http2/`, `test/js/bun/websocket/`, `test/js/web/websocket/`, `test/js/bun/udp/`, `test/js/node/child_process/`, `test/js/bun/resolve/`, `test/cli/hot/` and `test/cli/watch/`, and the files for IPC, error printing, stack traces and GC. - 396 files pass completely, 9,753 tests. - 3 of the 396 failed the first time, with eight files running at once on a busy machine, in tests with a time limit: `watch.test.ts` (3 tests), `stack-frame-paths.test.ts` (2) and `h2-conformance.test.ts` (2). I ran each three more times on its own: all pass every time. - 26 files fail in the same tests on both builds (`serve-body-leak` in one more on the base): | Why | Files (tests that fail) | |---|---| | They need the public internet or its DNS. | `resolve-dns` (30), `node-dns` (28), `node-tls-connect` (3), `dns-resolver-class` (2), `websocket` (2), `node-net` (2), `bun-connect-x509` (1), `socket` (1) | | They need packages that I did not have. | `proxy` (`axios`), `plugins` (`svelte`), `node-http-connect` (2, `proxy`), `resolve` (1, `tsyringe`) | | A time limit or a memory bound that a debug build with ASAN misses. | `serve-body-leak` (1), `req-url-leak` (1), `error-gc-test` (1), `inspect-error-leak` (1), `setInterval` (1), `timer-gc-roots` (1), `node-net` (1) | | They compare a long record of calls with an expected one. | `node-http-state-through-an-heir-and-a-proxy` (2), `node-http-closed-response` (1), `node-http-proxy-traps` (1), `node-http-receivers-that-are-no-messages` (1), `node-stream-proxy-traps` (1) | | Other. | `setTimeout` (3), `bun-test` (2), `leaks-test` (1) | - **After the 422 files had run** I removed four `#[inline]` attributes that an earlier form of change 18 had added. The static checks ran again on the final commits. --- _Generated by [Claude Code](https://claude.ai/code)_ --------- Co-authored-by: Claude <noreply@anthropic.com>
dylan-conway
added a commit
that referenced
this pull request
Nov 4, 2026
…de:fs` and `fs.watch` (#47893) ### What does this PR do? 42 separate changes, one commit each: 14 in the event loop, the I/O crate and spawn, 14 in the rest of `bun_jsc`, and 14 in `node:fs`, `fs.watch` and their neighbours. They follow #47291, #47317, #47338, #47382, #47393, #47414 and #47871 and work the same way: signatures name the type and the lifetime that the value already has, hand-written pointer code becomes plain ownership and borrows that the compiler checks, and what was never needed is deleted. Together: **128 fewer `unsafe` blocks, 14 fewer `unsafe fn`s, 6 fewer `unsafe impl`s and 1 fewer `unsafe extern` block**, +632/−1383 over 64 files. No safe function became `unsafe`, and no wrapper type was added. **[Where the compiled code changes](#where-the-compiled-code-changes) lists every place where the program does different work.** | # | Change | `unsafe` | Diff | |---|---|---|---| | 1 | spawn: the waiter thread's singleton is a plain `static` | −4 blocks, −1 `unsafe impl` | +28/−79, 1 file | | 2 | io: `FilePoll` and `EventLoopCtx` borrow the uws loop as `&Loop` | −1 `unsafe fn` | +42/−97, 7 files | | 3 | spawn, install: pass the uws loop to `register` and `set_as_parent_of` as `&Loop` | 0 | +5/−5, 3 files | | 4 | dns: `on_dns_socket_state` gets the loop from `VirtualMachine::uws_loop_ref` | −1 block | +1/−2, 1 file | | 5 | io, timer: the two loop callbacks that uSockets calls take `&Loop` | −2 blocks | +4/−11, 2 files | | 6 | event_loop: `SpawnSyncEventLoop::init` returns `Option<Box<Self>>` | −1 block | +8/−26, 2 files | | 7 | io: remove `PosixStreamingWriterParent::on_ready` | −1 block, −3 `unsafe fn` | +3/−14, 3 files | | 8 | spawn: `Process::init_posix` returns `RefPtr<Process>` | −1 block | +4/−6, 1 file | | 9 | event_loop: `ConcurrentTask::default` builds its placeholder `Task` with `Task::new` | −1 block | +2/−3, 1 file | | 10 | event_loop: `init_global` fills the `MiniEventLoop` before boxing it | −1 block | +15/−30, 1 file | | 11 | io: `PosixBufferedReader::close_if_final` takes `&mut self` | −1 block, −1 `unsafe fn` | +4/−8, 1 file | | 12 | spawn_sys: one `safe fn kill` for the four crates that send signals | −4 blocks, −1 `unsafe extern` block | +13/−26, 6 files | | 13 | uws_sys: remove the unused `Zeroable` impl for `socket_group::VTable` | −1 `unsafe impl` | +0/−2, 1 file | | 14 | uws_sys: `feed_path` turns its `&CStr` into a `&ZStr` with `ZStr::from_cstr` | −1 block | +1/−3, 1 file | | 15 | jsc: `TranspilerJob::run` borrows its locals for the parse and the print | −6 blocks | +20/−77, 1 file | | 16 | jsc: the guards of `TranspilerJob::run` hold references | −3 blocks | +29/−43, 1 file | | 17 | jsc: `TranspilerJob` owns its path text as a `Box<[u8]>` | −2 blocks | +12/−33, 1 file | | 18 | console: `message_with_type_and_level` takes `&[JSValue]` | −5 blocks | +46/−90, 3 files | | 19 | jsc: only the guards destruct an exception scope | −6 blocks, −2 `unsafe fn` | +38/−113, 3 files | | 20 | jsc: `SourceLocation` is not `Send` or `Sync` | −2 `unsafe impl` | +1/−21, 2 files | | 21 | jsc: three questions about an AOT image are `safe fn` over `FfiSlice` | −3 blocks | +34/−34, 1 file | | 22 | jsc: callbacks take references where C++ passes the address of a live object | −6 blocks | +18/−29, 5 files | | 23 | jsc: `Bun__NodeCompileCache__enable` is typed as what C++ passes | −3 blocks, −1 `unsafe fn` | +7/−12, 1 file | | 24 | jsc: `SavedSourceMap::put_value` returns nothing | −2 blocks | +9/−22, 2 files | | 25 | jsc: remove two exports that nothing calls | −2 blocks, −2 `unsafe fn` | +2/−34, 4 files | | 26 | mimalloc: `mi_is_in_heap_region` is a `safe fn` | −2 blocks | +6/−8, 4 files | | 27 | jsc: five `unsafe` blocks that a better declaration makes unnecessary | −5 blocks | +16/−24, 4 files | | 28 | jsc: `resume_loading_module` prints into the thread-local printer in place | −1 block | +6/−28, 1 file | | 29 | node:fs: `mkdir_recursive_os_path_impl` reborrows `sync_error_buf` | −8 blocks | +13/−49, 1 file | | 30 | node:fs: `NewAsyncCpTask::run_from_js_thread` takes the `Box` it frees | −8 blocks, −1 `unsafe fn` | +44/−67, 3 files | | 31 | jsc: `MarkedArrayBuffer::from_bytes` takes `Box<[u8]>` | −4 blocks | +10/−30, 3 files | | 32 | node:fs: the NUL-terminated paths of `cpSync` and recursive `readdir` borrow their buffers | −5 blocks | +8/−17, 1 file | | 33 | node:fs: recursive `readdir` frees each queued result list as it pops it | −3 blocks | +5/−25, 1 file | | 34 | fs.watch: `PathWatcherManager::mutex` owns the maps it guards | −13 blocks | +103/−134, 1 file | | 35 | fs.watch: `FSWatchTask::deinit` takes the `Box` | −3 blocks, −2 `unsafe fn` | +21/−36, 2 files | | 36 | fs.watch: `FSWatcher::init` returns the JS wrapper | −1 block | +5/−17, 2 files | | 37 | fs.watchFile: three raw-pointer detours in `StatWatcher` setup | −3 blocks | +5/−12, 1 file | | 38 | fs.watch: three single sites in path_watcher.rs | −2 blocks, −1 `unsafe impl` | +13/−30, 2 files | | 39 | node: `Bun__Buffer_fill` and `posix_fadvise` say in their types what they need | −4 blocks | +14/−12, 4 files | | 40 | node:os: `networkInterfaces` drops its counting pass over the `getifaddrs` list | −1 block | +1/−15, 1 file | | 41 | fs.watch (macOS): `FSEventsLoop::mutex` owns the state it guards | −6 blocks, −1 `unsafe fn` | +9/−30, 1 file | | 42 | fs.watch (macOS): the FSEvents task is typed, and three smaller sites | −3 blocks, −1 `unsafe impl` | +10/−32, 3 files | The commits can be reviewed one at a time. Worth knowing before reading on: - **Nothing that users can see changes.** Under `test/` only the inventory of the `vm-thread-door` source lint changes: it loses the entries of four removed `unsafe impl`s (changes 20, 38 and 42). - **Seven changes end an aliasing or contract bug that had no visible effect.** - Changes 2, 3 and 4: the uws loop was borrowed as `&mut Loop` in `FilePoll`, `Process` and the DNS code, while the callbacks of the same loop reach it again. - Changes 15 and 16: the guards of `TranspilerJob::run` kept raw pointers to the job, its logs and its transpiler, and turned them into `&mut` while the body used the same objects. - Change 30: `run_from_js_thread(&mut self)` freed its own allocation while that `&mut self` was live. - Change 31: `MarkedArrayBuffer::from_bytes` took `&mut [u8]` and freed it later. - **Two mutexes own what they guard** (changes 34 and 41): the state of `PathWatcherManager` and of the FSEvents loop sat in `UnsafeCell`s next to the lock. Each lock is taken and released at the same places as before. The waiter thread's singleton needs no cell at all (change 1). - **Ownership is in the types:** what is freed arrives as a `Box` or a `RefPtr` (changes 6, 8, 17, 30, 31, 33, 35 and 42). A watch task's `deinit` can no longer be called on the task that is embedded in the watcher. - **Seven functions that C or C++ calls take references** (changes 5, 22, 23 and 39). Each message names the call sites and what they pass. - **Nine imports are `safe fn`s:** `kill` (change 12), three questions about an AOT image (21), `mi_is_in_heap_region` (26), `Bun__Bindgen__exceptionPresenceMatches`, `GetLocalTime` and `Bun__EncoderStringTable__create` (27), and `posix_fadvise` (39). Each message says what the C side reads and writes. - **Dead code goes:** `PosixStreamingWriterParent::on_ready` (change 7), a `Zeroable` impl (13), two exports and three declarations in `headers.h` (25), and a pass over the `getifaddrs` list whose result was thrown away (40). - **Parts of eleven changes are compiled only for Windows, macOS or FreeBSD:** 2 (`windows_event_loop.rs`, `dns_sd.rs`), 7 (the Windows writer), 12 and 39 (FreeBSD), 27 (`GetLocalTime`), 29 (the Windows path buffer), 30 (an import), 34 (kqueue), 35 (the Windows watch task), 41 and 42 (FSEvents). ### Where the compiled code changes I compared the release assembly (x86-64 Linux, `--emit=asm`) of all 134 crates of the workspace, before and after. For 78 of them the files are the same byte for byte, among them `bun_mimalloc_sys`, which this PR touches. The other 56 I compared function by function, with names demangled and labels normalised. In 45 of the 56 no function differs: what differs is line numbers in the debug information and the paths of the two checkouts. One function of `bun_sql_jsc` holds the commit's own hash, and is not counted below. | Crate | Functions | Differ | Other constants or names only | Same length | Shorter | Longer | Instructions | Branches | Compares | Calls | Panic call sites | |---|--:|--:|--:|--:|--:|--:|--:|--:|--:|--:|--:| | 45 crates, among them `bun_alloc`, `bun_sys`, `bun_spawn_sys`, `bun_uws_sys`, `bun_install`, `bun_http` and `bun_bundler` | 37,921 | 0 | | | | | | | | | | | `bun_http_jsc` | 180 | 34 | 34 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | | `bun_sql_jsc` | 409 | 48 | 48 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | | `bun_install_jsc`, `bun_js_parser_jsc`, `bun_patch_jsc`, `bun_sourcemap_jsc` | 339 | 12 | 12 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | | `bun_io` | 138 | 1 | 0 | 0 | 1 | 0 | −1 | 0 | 0 | 0 | 0 | | `bun_spawn` | 61 | 5 | 3 | 1 | 1 | 0 | −1 | −1 | −2 | −1 | 0 | | `bun_event_loop` | 49 | 2 | 0 | 0 | 1 | 1 | −17 | −5 | −6 | −1 | 0 | | `bun_jsc` | 1,762 | 322 | 308 | 4 | 7 | 3 | −249 | −12 | −18 | −18 | −4 | | `bun_runtime` | 11,209 | 1,186 | 1,166 | 4 | 11 | 5 | −177 | −23 | −21 | −13 | −1 | The last five columns are net, and count the functions that exist on one side only: 8 only before and 2 only after, which were removed, renamed or inlined. **1,571 of the 1,610 functions that differ do so only in a constant or in the name of a callee. Two things account for almost all of them.** - **The fields of the VM move (change 17).** A `TranspilerJob` is 40 bytes smaller, and the VM holds a pool of 64 of them, so every field behind the pool is 2,560 bytes closer to the start: 26,896 becomes 24,336, for example. The functions that read such a field differ in that constant and in nothing else. - **An exception scope is destructed without a hop (change 19).** `TopExceptionScope::destroy` was a single jump to the C++ function. Its callers now call the C++ function. 49 functions of `bun_runtime` differ only in that name, and 34 more in that name and in the offsets. **Checks.** No path makes a check that the source did not already have. These are all the functions in which the listing shows more conditional branches, compares or panic call sites than before, and why: | Function | Listing | Why | |---|---|---| | `NodeFS::mkdir_recursive` | 490 to 497, +1 branch, +1 compare, +1 call, +1 panic site | Change 29. `&working_mem[..first_match]`, the last expression of the function, has a bounds check in the source before and after. The compiler used to prove it away on every path. On the path where the last `mkdir` reports `EEXIST` it no longer does. **One compare and one branch more there, after a system call.** | | `NewAsyncCpTask::<false>::run_from_js_thread`, `__bun_release_task_unrun::handle` | 117 to 129 and 478 to 493, +1 branch, +1 compare, +3 and +2 calls | Change 30. Both called `destroy` (49 instructions), which released the keep-alive and freed the task. The release is inline in both now, and the rest is the drop of the `Box` (38). `destroy` no longer exists. | | `SpawnSyncEventLoop::init` (`bun_event_loop`) | 30 to 44, +1 branch, +3 calls, +1 panic site | Change 6. It makes the box, which its one caller `RareData::spawn_sync_event_loop` made before (42 to 11, −2 branches, −4 calls, −1 panic site). | | The drop of a `TranspilerJob` (`bun_jsc`) | 116 to 122, +1 branch, +1 compare, +1 call | Change 17. It frees the path text, which three other places freed by hand: `run_from_js_thread` (813 to 800), `release_queued_jobs_for_teardown` (95 to 82) and `teardown` (514 to 508) lose one branch, one compare and one call each. | | Change | What differs | Effect | |---|---|---| | 1 | The waiter thread's loop goes from 366 to 365, with one branch, one compare and one call fewer. `WaiterThreadPosix::start` keeps its 171, with one compare fewer. | Same thread, same queue. | | 2 | `FilePoll::unregister` goes from 106 to 105. | Same system calls. | | 4 | `Resolver::on_dns_socket_state` goes from 283 to 279, without an `unwrap` and its panic. | Same polls. | | 7 | The two `on_ready` functions that nothing called go: 51 instructions for `Terminal` and 10 for `FileSink`. | None. | | 10 | `MiniEventLoop::init_global` goes from 261 to 230, with 6 branches, 4 calls and a panic site fewer. | The loop is filled in before it is boxed, not through the box. | | 15, 16, 17 | `TranspilerJob::run` goes from 3,372 to 3,323, with 2 branches, 6 compares and a call fewer. `RuntimeTranspilerStore::transpile` goes from 236 to 237. | Same parse and print. The path text is the same allocation, freed with the job's other fields. | | 18 | `message_with_type_and_level` goes from 427 to 409, with 3 branches, 3 calls and 3 panic sites fewer. The function that C++ calls builds the slice and goes from 8 to 12. `onRejectEntryPointResult` (18 to 17) and `Run::start` (527 to 526) pass one argument fewer. | Same output. | | 19 | Apart from the callers above: `TopExceptionScope::init` (14) and `destroy` (1) go. | One jump fewer per scope. | | 22 | In the test runner's formatter `PropertyIterator::for_each` goes from 274 to 271, with the same branches, compares and calls. The two `Formatter::print_object` of `console.log` keep their 136, and the callback of `URLSearchParams::to_string` its 6: the same instructions in another order. | Same output. | | 25 | `AbortSignal__Timeout__run` (1) and `Zig__GlobalObject__reportUncaughtException` (24) go. | None: nothing called them. | | 27 | `write_diagnostic_filename` keeps its 122: `std::process::id()` is a jump to `getpid`, which was called directly. `takeHeapSnapshot` keeps its 21. | One jump more per CPU or heap profile that is written. | | 28 | `AsyncModule::on_done` goes from 783 to 693, with 3 branches and 4 calls fewer. | The printer is not moved out of its thread local and back. | | 29 | Apart from the row in the table of checks: the other instance of `mkdir_recursive_os_path_impl` keeps its 461 and compares with 4,096 instead of 4,097. | Same directories, same errors. | | 30 | Apart from the rows in the table of checks: `fs.cp`'s entry point goes from 249 to 256, all moves between registers. | Same copy. If the conversion of a result failed, the early return would free the task, where it leaked it, and the keep-alive would stay taken as before. Neither conversion can fail today. | | 33 | `AsyncReaddirRecursiveTask::on_subtask_done` goes from 329 to 226, with 11 branches and 6 calls fewer, and `clear_result_list` from 128 to 105. | The lists were collected in a `Vec` and freed afterwards. Three `reserve` calls, a `memcpy` and a free go. | | 34, 38 | `unlink_watcher_locked` (185) is `Locked::unlink_watcher` (185). **`add_watch`, `add_one`, `walk_and_add` and `remove_watch` get the maps as an argument, which is one argument more for each.** They no longer fetch the maps from the manager. `Linux::add_one`, the only one of the four that is not inline in its caller, goes from 353 to 352. In `FSWatcher::detach` (201 to 215) the compiler loads the map's fields once in front of the loop and keeps them on the stack, which a `&mut` lets it do. The inotify thread keeps its 1,296 and asks for zeroed memory in one call, where it called `malloc` and `memset`. `PathWatcherManager::default` stores its fields in another order. | Each lock is taken and released where it was. | | 35, 36 | `fs.watch`'s entry point goes from 2,029 to 2,021, with a branch, a call and a panic site fewer. | Same watcher. | | 37 | `fs.watchFile`'s entry point goes from 721 to 714. The lazy set-up of the VM's rare data was inline twice and is inline once. | Same watcher. **The VM's ticket for the first `stat` is issued before the watcher's ref is taken, not after.** | | 39 | `Bun__Buffer_fill` goes from 652 to 649, with one compare fewer. | Same bytes. | | 40 | `os.networkInterfaces` goes from 676 to 670, with 2 branches fewer. | One pass over the list fewer. | Changes 3, 5, 8, 9, 11 to 14, 20, 21, 23, 24, 26, 31 and 32 ask for the same operations in the same order, and show up in no listing, or only in the offsets above. What changes 2, 7, 12, 27, 29, 30, 34, 35, 39, 41 and 42 do to code for Windows, macOS or FreeBSD is not compiled on Linux. There, by reading: the Windows view of the path buffer in change 29 covers the whole buffer instead of its first 65,534 bytes, and the FreeBSD helpers of change 34 have one more argument each, like those for Linux. The comparison covers x86-64 Linux only, one crate at a time. The build that ships also inlines across crates when it links. ### The changes in detail #### 1. spawn: the waiter thread's singleton is a plain `static` `INSTANCE` was an `UnsafeCell<WaiterThreadPosix>` behind a hand-written `unsafe impl Sync`, for two fields. `active`, the list of processes being waited for, is touched by the waiter thread alone, so the thread function owns it as a local and passes it to `NewQueue::loop_`, which no longer has to swap it out of a cell and back on every wake-up. `eventfd` is written once in `start()` and read from the other threads and the SIGCHLD handler, so it is an `AtomicI32` with relaxed loads and stores, like the stdio fds in `bun_core::output`. What is left is atomics only, the struct is `Sync` by itself, and `Instance`, `instance()` and `instance_ref()` go. `NewQueue::loop_` dereferences the pointers in the list it is handed, so it and the thread function, its only caller, are private to the module. #### 2. io: `FilePoll` and `EventLoopCtx` borrow the uws loop as `&Loop` Every `Loop` method takes `&self` since its fields are `Cell`s, but `FilePoll::register`/`unregister` and their helpers (on Windows, `unregister` and `deinit_possibly_defer`) still asked for `&mut Loop`, and `EventLoopCtx::loop_mut` and `FilePollRef::uws_loop_mut` made one out of the raw pointer. They only call `fd`, `inc`, `dec`, `add_active`, `sub_active`, `ref_` and `unref`, so all of them take or return `&Loop`. `pub unsafe fn platform_event_loop` existed to put the "no other live `&mut Loop`" rule on the caller. With a shared borrow there is no such rule, so it and `loop_mut` are one private accessor. It stays private because a spawnSync ctx hands out the loop that has no uWS `LoopData`, which the safe `update_date` and `flush_pending_writes` rely on. The two callers outside the crate (the memory-pressure watch, and `dns_sd` on macOS) dereference `loop_()` in the `unsafe` blocks they already have. The doc of `FilePollRef::uws_loop` said that the loop is the process-global one and lives for the program. A spawnSync reader passes the isolated loop, and a Worker's loop is freed, so it says what the callers do pass. #### 3. spawn, install: pass the uws loop to `register` and `set_as_parent_of` as `&Loop` `Process::watch` and `rewatch_posix` turned the raw loop pointer into `&mut Loop` for `FilePoll::register`, and `PackageManager::init` did the same for `EventLoopHandle::set_as_parent_of`. `register` takes `&Loop`, and `set_as_parent_of` only calls `set_parent_raw(&self)`, so the three sites borrow the loop shared. #### 4. dns: `on_dns_socket_state` gets the loop from `VirtualMachine::uws_loop_ref` It turned `event_loop_handle` into `&mut Loop` in an `unsafe` block to pass it to `FilePoll::register` and `unregister`, which take `&Loop`. The arm is POSIX-only, where `event_loop_handle` is the loop that `uws_loop_ref()` borrows, so it calls that. The `unwrap()` of the handle, and its panic, go with the block: `uws_loop_ref()` asserts that the handle is set in debug builds only, as it does for the three other callers in this file. `VirtualMachine::init` sets the handle, through `ensure_waker`, before it creates the global object. #### 5. io, timer: the two loop callbacks that uSockets calls take `&Loop` `Bun__internal_dispatch_ready_poll` and `Bun__internal_ensureDateHeaderTimerIsEnabled` took `*mut Loop` and turned it into `&Loop` in an `unsafe` block. Each of their C callers (`us_internal_dispatch_ready_polls`, `us_internal_enable_sweep_timer`) dereferences `loop` just before the call, so it is non-null and live, which is what `&Loop` says. It stays live for the call: the dispatch loop uses `loop` again afterwards, and the date-header callback only refreshes the date and arms a timer. A reference argument has to stay valid for the whole call, and the poll handler that the first one dispatches re-enters the loop. Nothing there can invalidate it: a grep of `src/` finds no `&mut Loop` (under any of its aliases) in a signature, no `&mut *` or `as_mut()` on a `*mut Loop` or `NonNull<Loop>`, and no `&mut self` method on `Loop` or `InternalLoopData`. #### 6. event_loop: `SpawnSyncEventLoop::init` returns `Option<Box<Self>>` `init` filled a `&mut MaybeUninit<Self>` and returned a `bool`, so `RareData` allocated an uninitialized box first and called `assume_init` on it. The comments gave the reason that the value must not move once the loop's parent is set, but what is stored there is the value of the `event_loop` field, a heap pointer, not the address of the struct. The one thing that does keep that address is the Windows timeout timer, which takes it later. `init` does the boxing itself, so the value is at its final address before anything can point at it, and the caller stores what it gets. A loop that cannot be created no longer costs an allocation. #### 7. io: remove `PosixStreamingWriterParent::on_ready` Nothing calls it: `PosixStreamingWriter` reports progress through `on_write`, and `FileSink` and `Terminal` resume from its `Drained` status. The trait's default method, the one the macro stamps and `Terminal`'s own go. The macro keeps its `on_ready =` argument for the Windows `on_writable` slot, which leaves `FileSink::on_ready` with a caller on Windows only. #### 8. spawn: `Process::init_posix` returns `RefPtr<Process>` It boxed the `Process`, returned the raw pointer, and its only caller wrapped that in `RefPtr::from_raw`. `RefPtr::new` does the same boxing and owns the initial ref from the start. #### 9. event_loop: `ConcurrentTask::default` builds its placeholder `Task` with `Task::new` The placeholder came from `zeroed_unchecked()`. `Task::new` is a `const fn` over the three fields, and tag 0, a null pointer and `ContextId::NONE` are the same values. #### 10. event_loop: `init_global` fills the `MiniEventLoop` before boxing it It boxed a fresh value, turned the raw pointer back into `&mut` in an `unsafe` block, and set `env` and `top_level_dir` through that. Both are plain fields of a value the function still owns, so they are set on the local, which is boxed afterwards. Only the loop's parent needs the heap address; it is set right after the boxing, that is after the env and cwd code, which does not look at the loop. #### 11. io: `PosixBufferedReader::close_if_final` takes `&mut self` It was an `unsafe fn` over `*mut Self` whose whole body was one `(*this).close_without_reporting()`, which passes no callback and so never reaches the parent. It takes `&mut self` like `fill_scratch` and `fill_buffer`, which `read_loop` calls on the lines just before it in the same way. #### 12. spawn_sys: one `safe fn kill` for the four crates that send signals `pifd_from_pid` called `libc::kill` in an `unsafe` block, and so did three places in `bun_runtime`: the Fuzzilli comparison run, twice, and `terminate_process_group` of `bun test --parallel`. `kill(2)` takes two integers by value and answers a bad one with an errno, so it has no precondition for the caller to uphold. `bun_io` and `bun_spawn` said so already, with three private `safe fn` declarations between them (`ParentDeathWatchdog.rs`, `Process::send_signal` and the job-control code of `spawnSync`). Both depend on `bun_spawn_sys`, so the declaration goes there once, into the `extern` block that `ffi` already has, and all seven places call it. `bun_runtime` reaches it as `bun_spawn::ffi::kill`. On FreeBSD the declaration was the only user of the `c_int` import in `ParentDeathWatchdog.rs`, so the import gets the `cfg` of what still uses it. #### 13. uws_sys: remove the unused `Zeroable` impl for `socket_group::VTable` Nothing zero-initializes a `VTable`: the two places that build one (`vtable::make` and the test runner's `Channel`) list all eleven fields. #### 14. uws_sys: `feed_path` turns its `&CStr` into a `&ZStr` with `ZStr::from_cstr` It took the `&CStr`'s length and rebuilt the same string from the raw pointer with `ZStr::from_raw` in a second `unsafe` block. `ZStr::from_cstr` is that conversion. #### 15. jsc: `TranspilerJob::run` borrows its locals for the parse and the print `run` handed `input_file_fd` and `cache` to `ParseOptions` as `&mut *addr_of_mut!(x)`, cast the `Transpiler` copy to a shorter lifetime through a raw pointer, closed the fd from a guard that held a raw pointer to it, and swapped the thread-local printer into a local that a second raw-pointer guard swapped back. None of that is needed. `ParseOptions` asks for `&'b mut` borrows of the fd and the cache, and plain `&mut` borrows do, as in `transpile_source_code_of_any_provenance`. The parse result keeps a `NonNull` to the cache, through which the print stores its output. It is made from that borrow in either form, so the raw form bought nothing. `Transpiler<'a>` is covariant, so the copy takes the arena's lifetime by itself. The fd is the value of its own close guard. Where the watcher adopts it, `run` stores `Fd::INVALID` in the guard, so the `Cell<bool>` that told the old guard not to close is gone. `source_code_printer` is already an exclusive borrow of the thread-local's printer for the whole of `run`, so the print writes into it in place: no swap, no placeholder `BufferPrinter`, no guard. `transpile` logs the `loader` it was given instead of reading it back from the job. #### 16. jsc: the guards of `TranspilerJob::run` hold references Three scope guards in `run` kept raw pointers to the job, to the two logs and to the `macro_context` field of the `Transpiler` copy, and dereferenced them in `unsafe` when they fired, while the body went on using `self` and `transpiler`. Each guard now owns the `&mut` and the body reborrows through it: the dispatch guard holds the job (`this` is a reborrow, so its fields still borrow apart), the log guard holds both logs and `set_log` gets its pointer from the guard, and the macro-context guard holds the `Transpiler` reference. The guards are declared where they were, so they fire in the same order. #### 17. jsc: `TranspilerJob` owns its path text as a `Box<[u8]>` The job stored a `Path<'static>` whose slices pointed into a `Box<[u8]>` leaked with `heap::into_raw`, and `reset_for_pool` rebuilt the box from `path.text` to free it. The job now holds the box itself. `run` builds the `Path` from a borrow of it (`Path::init` only stores the slice), and the box is freed with the other fields by the `drop_in_place` of `store.put()`, which follows both calls of `reset_for_pool`. The slot is 40 bytes smaller. #### 18. console: `message_with_type_and_level` takes `&[JSValue]` It was declared `#[host_call] extern "C"` with an unused `*mut ConsoleObject` and a `(*const JSValue, usize)` pair, which made it an `unsafe fn`. It has no C symbol and is never used as a function pointer: all five callers are Rust, and four of them wrapped one local `JSValue` in `unsafe` to call it. It is now a plain `fn` over a slice. Only `Bun__ConsoleObject__messageWithTypeAndLevel`, the export that C++ calls, builds the slice from the raw pair. C++ passes its stack array there, so the null test of `ffi::slice` is gone too. #### 19. jsc: only the guards destruct an exception scope `TopExceptionScope::destroy` and `ExceptionValidationScope::destroy` were `unsafe fn`s whose callers were the `Drop` of the two guards, plus one hand-written `init`/`destroy` pair in `for_each_property_ordered`. `ExceptionValidationScope::init_at` cast its `MaybeUninit` storage to the inner type and then called `assume_init_mut`. `for_each_property_ordered` uses `top_scope!` like its siblings. `TopExceptionScopeGuard::drop` calls the C++ destructor itself, and the validation guard drops a `TopExceptionScopeGuard` over its inner scope. `ExceptionValidationScope::init_guard_at` writes the wrapper with a zeroed inner scope and constructs that in place, which is what `TopExceptionScope::init_guard_at` does. Both `destroy` and the unused `TopExceptionScope::init` are gone. The `init_at` of both types and `ExceptionValidationScope::init` only served the `init_guard*` constructors and are folded into them. `init_in_place`, the one function that still constructs a scope without a guard, is private to the module, because nothing outside it could destruct such a scope. The construct and destruct calls are the same, on the same addresses. #### 20. jsc: `SourceLocation` is not `Send` or `Sync` It had hand-written `unsafe impl Send` and `unsafe impl Sync`. Nothing needs them: a `SourceLocation` is built by `src!()` or `from_caller` and passed by value straight into the scope constructors on the same thread. It is never stored in a static or a field and never handed to another thread. Both impls are removed, with the sentence in the doc of `intern_location_file` that relied on them and their two entries in the `vm-thread-door` lint inventory. #### 21. jsc: three questions about an AOT image are `safe fn` over `FfiSlice` The `Bun__AOT__*` imports that take bytes took a local `Bytes { ptr, len }` built from a `&[u8]`, so every call sat in an `unsafe` block. Their argument is now `bun_core::ffi::FfiSlice`, which has the same C layout and carries the borrow. `bytes_of` is gone, and `Bytes` is only what C++ hands to Rust: the result of `imageCode` and an argument of three callbacks. `admittedCode`, `admitsUnknownJavaScript` and `loadsCodeOutsideImage` have no precondition beyond a readable slice, so they are `safe fn`, and `admitted` is `&mut u32`. C++ rejects bytes that are shorter than the header or misaligned before it reads the header. What it then reads are a `uint8_t` and a `uint32_t`, for which every bit pattern is valid, and it reads nothing after it returns. The other six stay `unsafe`. `omitsFunctionSource` reads a C++ `bool` from the header, which the check of magic and stamp does not cover. `useImage` makes the bytes executable; its two slices are `FfiSlice<'static>`. `useNoImageOnThisThread` must run before the thread has a VM. `forEachFilePathStartingWith`, `noteFirstLoadInEarlierProcess` and `imagesHoldFile` take raw pointers as well. #### 22. jsc: callbacks take references where C++ passes the address of a live object The trampoline of `MarkedArgumentBuffer::new`, the callback of `URLSearchParams__toString` and `ForEachPropertyCallback` declared raw pointers and dereferenced them in `unsafe` first thing. C++ calls them in `MarkedArgumentBuffer__run` (`callback(ctx, &args)`), in `URLSearchParams__toString` (`callback(ctx, &slice)`), twice in `JSC__JSValue__forEachPropertyImpl` and once in `forEachPropertyOrderedImpl` (`iter(globalObject, arg2, &key, ...)`), and `args`, `slice` and `key` are locals of the calling frame each time. The parameters are now `&mut Ctx<F, R>`, `&mut MarkedArgumentBuffer` and `&EncodedSlice`, like the `&JSGlobalObject` that `ForEachPropertyCallback` already took. In `MarkedArgumentBuffer::new`, `f` is moved out of `ctx` before it runs and `ctx` is a local that nothing else points to. The `cast_fn_ptr` that erases the trampoline's type was there already. #### 23. jsc: `Bun__NodeCompileCache__enable` is typed as what C++ passes The export was an `unsafe extern "C" fn` over raw `BunString` pointers that it turned into a reference, or wrote through, in `unsafe` blocks. It is called only at NodeModuleModule.cpp:946, with null or `&directoryStr`, `&outDirectory` and `&outMessage`, all locals. So the parameters are `Option<&BunString>` and `&mut MaybeUninit<BunString>`, and the function is not `unsafe`. `MaybeUninit::write` drops nothing, like the `ptr::write` it replaces. #### 24. jsc: `SavedSourceMap::put_value` returns nothing It returned a `Result` that was always `Ok(())`: the insert aborts on OOM. Two callers had an `Err` arm that freed the value again in `unsafe`, one ignored the result and one wrapped it in `handle_oom`. It returns `()` now and the four callers just call it. #### 25. jsc: remove two exports that nothing calls `AbortSignal__Timeout__run` has no reference outside its definition: the timer dispatch calls `Timeout::run` directly. `Zig__GlobalObject__reportUncaughtException` was only declared in headers.h and never called. Both `unsafe extern "C" fn`s are removed, with the C++ declaration and `VirtualMachine::report_uncaught_exception`, which had no other caller. Two neighbouring declarations in headers.h go too: `Zig__GlobalObject__fetch` and `Zig__GlobalObject__promiseRejectionTracker` have no definition and no caller. #### 26. mimalloc: `mi_is_in_heap_region` is a `safe fn` It takes any address: it looks it up in the page map behind a range check and never dereferences it, and the page map pointer starts out as a static empty map. The two callers in `bun_jsc` wrapped it in `unsafe` only because of the declaration. It is declared `safe fn` like its neighbours without preconditions. In `mi_free_checked` the assertion moves out of the `unsafe` block, which stays for the frees. #### 27. jsc: five `unsafe` blocks that a better declaration makes unnecessary - `Bun__Bindgen__exceptionPresenceMatches` only reads the VM's pending exception. It is a `safe fn` over `&JSGlobalObject`, which `JsHeap::global_object` already gives. - `JsWrapper::payload_ptr` offsets the `NonNull` cell pointer with `NonNull::byte_add`, and `payload` is its `as_ptr()`, instead of offsetting the raw pointer in one block and wrapping it with `new_unchecked` in another. - `write_diagnostic_filename` asks `std::process::id()` on every platform instead of `libc::getpid()` or `GetCurrentProcessId()`. - `GetLocalTime` (Windows) only writes its out-parameter: `safe fn` over `&mut SystemTime`. - `Bun__EncoderStringTable__create` returns the result of a C++ `new`, which is never null: it is a `safe fn` that returns `NonNull<EncoderStringTable>`. #### 28. jsc: `resume_loading_module` prints into the thread-local printer in place The function swapped the thread-local `BufferPrinter` into a local and restored it from a guard that held two raw pointers, to the thread-local's box and to the local, and swapped a second placeholder in when it fired. It now takes one `&mut` to the thread-local's printer, resets it and prints into it, as the synchronous path in jsc_hooks.rs does. There is no swap, no placeholder and no guard. The buffer is reused as before, also when the print fails. That borrow lives until the function returns. Nothing in between goes through the thread-local: the print only calls back into `SavedSourceMap`, `SourceMapHandlerGetter` and the transpiler cache, and no JavaScript runs. The raw pointer that `source_map_handler` stashes and the writer both derive from that one borrow. #### 29. node:fs: `mkdir_recursive_os_path_impl` reborrows `sync_error_buf` The function kept a `*mut PathBuffer` next to a `&mut OSPathBuffer` cast from it, rebuilt each NUL-terminated parent with `from_raw`, and re-derived a `&mut PathBuffer` from the raw pointer on every error return. None of that needs a raw pointer. `working_mem` is a reborrow of the buffer through `cast_slice_mut` (the way `copy_file_inner` already views `sync_error_buf`, and `os_path_kernel32` its `PathBuffer` argument), the parents borrow `working_mem` through `from_buf`, and each error return uses the buffer only after the last use of both, which the borrow checker accepts. The guard is still dereferenced once. On Windows the alignment `assert!` is the one inside `cast_slice_mut`, and the view covers the whole buffer (49151 units) instead of the first 32767. Only an index panic moves: every caller keeps well-formed paths at 32766 units or fewer. #### 30. node:fs: `NewAsyncCpTask::run_from_js_thread` takes the `Box` it frees `run_from_js_thread(&mut self)` freed its own allocation through `unsafe fn destroy(*mut Self)` while the `&mut self` was still live, and kept the promise as a raw pointer from the start so that it could outlive that. The task is a `Box` that nobody else holds once `subtask_count` is zero, so the places where the pointer arrives (the two dispatch arms, the mini event loop's callback, `release_unrun`) now reclaim it with `heap::take`, and `run_from_js_thread` and `destroy` take `self: Box<Self>`. The promise stays a reference until just before `destroy`. `run_from_js_thread_mini` only forwarded and is folded into its one caller. `create` reads the promise value before it hands the promise over, like `AsyncFSTask::create`, instead of through the scheduled task's pointer. The shell passes a `ParentRef` built from its `&mut self` instead of a nullable raw pointer. Nobody used the pointers `schedule_new` and `create_for_shell` returned. If a result conversion failed, the early return would now free the task where it used to leak it. The keep-alive, which only `destroy` releases, would stay taken as before. Neither conversion can fail today. #### 31. jsc: `MarkedArrayBuffer::from_bytes` takes `Box<[u8]>` `from_bytes` took `&mut [u8]` but owns the bytes: `Drop` frees them, and so does JSC once they are handed over. Every caller had a `Box<[u8]>` one line earlier and turned it into a raw pointer (or leaked it) only to build that `&mut`. The parameter is now the box, stored through `ArrayBuffer::from_owned_bytes`, which writes the same pointer and length. The callers pass what they have. #### 32. node:fs: the NUL-terminated paths of `cpSync` and recursive `readdir` borrow their buffers `cp_sync_inner` and `cp_sync_one_directory` built `src` and `dest` with `OSPathSliceZ::from_raw`, which gives them a lifetime that has nothing to do with `src_buf` and `dest_buf`, although both functions write to those buffers later. `from_buf`, already used for the per-file paths a few lines down, returns the same pointer and length tied to the buffer, so the compiler now checks that the last use comes before the next write. The async twin `cp_async_one_directory` keeps `from_raw`. `cp_async` does not write the two NULs itself as `cp_sync_inner` does, and for an empty `dest` `os_path` returns `ZStr::EMPTY` without touching `dest_buf`, so `from_buf`'s debug assert could fire on a stale pooled buffer. `readdir_with_entries_recursive_async` took the dirent name as bytes, reduced the joined `&ZStr` to bytes, and rebuilt a `ZStr` from either. It now uses `name_assume_z()`, as the `lstatat` call below does, and keeps the joined `&ZStr`. #### 33. node:fs: recursive `readdir` frees each queued result list as it pops it Both loops over the result queue kept the previous node in an `Option<*mut ResultListEntry>` and freed it one iteration late, "because the next value will be read by the iterator". `BatchIterator::next` reads the link of a node before it returns the node, so the node can be reclaimed at once, as the FSEvents task queue already does. Each node is now taken as a `Box` where it is popped: no `Option`, no trailing free after the loop, and no `&mut *val` on the raw pointer. #### 34. fs.watch: `PathWatcherManager::mutex` owns the maps it guards The manager had a bare `Mutex` next to `UnsafeCell`s for the dedup map and the platform dispatch maps. Every access was `unsafe { &mut *cell.get() }` with a comment saying that the caller holds the lock, and the inotify reader laundered an owner's subpath through a raw pointer so that it could call `add_one` later. `mutex` is now a `Guarded<Locked>` holding both maps. Each `lock()` binds the guard and each `unlock()` is a `drop` of it at the same place, so the critical sections and the lock order with the FSEvents loop are unchanged. `add_watch`, `add_one`, `walk_and_add` and `remove_watch` get the platform maps as `&mut Linux` / `&mut Kqueue` instead of fetching them from the manager, and `unlink_watcher_locked` is `Locked::unlink_watcher`. The subpath is a plain borrow of `wd_map`: its last use comes before `add_one` needs the map. That is one argument more for each of the four, on Linux and on FreeBSD. The inotify `add_one` and `walk_and_add` needed the manager for nothing else but the inotify fd, so they take the `Fd` in its place. The kqueue `add_one` still needs the manager for `next_gen`. `PathWatcher`s stay raw pointers, and `platform_fd`, `next_gen` and the `Sync` impl are as they were. #### 35. fs.watch: `FSWatchTask::deinit` takes the `Box` `deinit` was an `unsafe fn(*mut Self)` whose first step was `heap::take`, and the dispatcher dereferenced the same raw pointer once more to call `run`. The heap task has one holder from the moment it is popped (or its post is refused), so the three places where the pointer arrives reclaim the `Box`, and `deinit(self: Box<Self>)` is a safe fn that can no longer be called on the `current_task` embedded in the watcher. The debug assert against that is gone. The order is the same: run, drop the entries, release the watcher's ref, free. `deinit` still releases a ref through an `unsafe` call. That the box holds one rests on module privacy: `ctx` is private, and only the enqueue sites box a task with it set, after `ref_task()`. The Windows task changes the same way, because the dispatch arm is shared. #### 36. fs.watch: `FSWatcher::init` returns the JS wrapper `init` and `create_fs_watcher` returned a `*mut FSWatcher` whose only use was `unsafe { (*result).js_this() }` in `NodeFS::watch`. `init` still holds its `&FSWatcher` at that point, so it reads the wrapper itself and returns the `JSValue`. The `js_this()` accessor existed for that one caller. #### 37. fs.watchFile: three raw-pointer detours in `StatWatcher` setup `init` armed a scope guard that would free the watcher "on the error path", but there is no return between the guard and its `into_inner`, and after `to_js_ptr` the wrapper owns the allocation anyway. The guard is gone. `lazy_scheduler` fetched the `RareData` slot a second time after `StatWatcherScheduler::init(vm)` so that no `&mut` would span the call. `init` only stores the pointer, so the slot it already holds is written directly. `InitialStatTask::create_and_schedule` dereferenced its raw watcher pointer to get a ticket from the VM. Its one caller has the watcher as a reference and passes the ticket in; it is now issued before the watcher's ref is taken instead of after. #### 38. fs.watch: three single sites in path_watcher.rs `watch` boxed a `PathWatcher` with an empty handler map and then added the first handler through the raw pointer it had just made. `PathWatcher::new` now takes the handler and adds it while it still holds the `Box`. The inotify reader allocated its 64 KiB buffer with `new_uninit`, `write_bytes` and `assume_init` on a local `align(4)` newtype. A `Box<[u32; 16 * 1024]>` from `boxed_zeroed` has the same size and alignment, and `read` gets its bytes through `bytes_of_mut`. `unsafe impl Send for PathWatcherManager` had no user: the manager only crosses threads as `&'static PathWatcherManager`, which needs `Sync`. The inventory of the vm-thread-door lint is regenerated. #### 39. node: `Bun__Buffer_fill` and `posix_fadvise` say in their types what they need `Bun__Buffer_fill` took `*const EncodedSlice` and turned it into a reference on its first line. Both C++ callers pass the address of a local, so the parameter is `&EncodedSlice`, as in `Bun__Process__setCwd`. The fn stays `unsafe` for its buffer pointer. `posix_fadvise` takes four scalars and reports a bad fd through its return value, so it joins `fallocate` and `ftruncate` in `bun_sys`'s `safe_libc` block as a `safe fn` with the `libc` crate's signature, and is re-exported from `bun_sys::c` like `fchmod`. Its three callers lose their `unsafe` blocks. #### 40. node:os: `networkInterfaces` drops its counting pass over the `getifaddrs` list The first walk over the list only counted the INET interfaces, and the count was discarded with `let _ =`. The walk and its raw dereference are gone. #### 41. fs.watch (macOS): `FSEventsLoop::mutex` owns the state it guards The loop had a bare `Mutex`, an `UnsafeCell<FSEventsLoopState>` and an `unsafe fn state(&self) -> &mut FSEventsLoopState` whose contract was "the caller holds the mutex". All five users took the lock guard on the line before. `mutex` is now a `Guarded<FSEventsLoopState>`, and each `lock_guard()` + `unsafe { state() }` pair is one `lock()`. The guard is bound where `_guard` was, so every function holds the lock over the same range, and `events_cb`, which only reads the watcher list, gets a shared view. #### 42. fs.watch (macOS): the FSEvents task is typed, and three smaller sites `Task::new<T>` erased `&'static T` and `fn(&T)` to `*mut ()` and `fn(*mut ())` with `cast_fn_ptr`. `T` is always `FSEventsLoop`, so `Task` holds `&'static FSEventsLoop` and `fn(&FSEventsLoop)` and calls one with the other. `ConcurrentTask::auto_delete` was `true` at its only construction site. The field is gone, and `cf_loop_callback` reclaims each popped node as a `Box`, runs it and drops it, in the same order as before. `unsafe impl Send for FSEventsLoop` had no user: the loop only crosses threads as `&'static FSEventsLoop`, which needs `Sync`. The inventory of the vm-thread-door lint is regenerated. `Darwin::init` turned a fresh `Box` into a raw pointer and dereferenced it to get the `&'static` manager, which is never freed. That is `Box::leak`. ### How did you verify your code works? - **Static checks on the final commits:** `cargo fmt --check`, `cargo clippy --workspace` (0 warnings), the source lints and `cargo check --workspace --release` on Linux, and `cargo check --workspace --release` for `x86_64-pc-windows-msvc`, `aarch64-apple-darwin` and `x86_64-unknown-freebsd`, because eleven changes touch code that only those targets compile. - **Assembly:** the comparison above. - **30 test files on the final commits**, on a debug build with ASAN, all at once and each cut off after 50 seconds: the files for spawn (signals, the waiter thread, pidfd, pipes), `Bun.Terminal`, `FileSink`, DNS polls, `fs.cp`, `mkdir`, `opendir`, `fs.promises`, recursive `readdir`, `fs.watch`, `fs.watchFile`, `node:os`, the console, `URLSearchParams`, the shell's `cp` and `mkdir`, the transpiler cache, `node:module` and source maps. - 28 files pass completely, 676 tests. - `spawn_waiter_thread` fails on this build and on a debug build of the base alike: it allows a child 750 ms of CPU time, and a debug build takes about 1,050 ms on both. - `test/cli/test/parallel.test.ts` was cut off. - **After the assembly comparison and the 30 files had run,** a rebase brought in one more commit of `main` (#47836). It left all 42 patches here as they were. The static checks ran after it. - **409 test files before an earlier rebase.** That rebase brought in 31 commits of `main`. In it I adapted five of the commits here to `main`'s new code (1, 16, 17, 21 and 23) and dropped one, and a review after it changed code or comments in four (2, 12, 19 and 35). The 409 files ran on a debug build with ASAN of the commits as they were, ten at a time, and the files that failed also on a debug build of their base. They are all of `test/js/bun/test`, `bun/spawn`, `bun/shell`, `bun/resolve`, `bun/sourcemap`, `bun/console`, `test/cli/run`, `node/fs`, `node/process`, `node/child_process`, `node/module`, `node/watch` and `web/console`, and 61 more files that reach the event loop, the console, `fs.watch` or the transpiler store. - 377 files pass completely, 7,926 tests. - 28 files fail in the same tests on both builds (the number of failing tests in brackets; 0 means that the file does not load): `glob-on-fuse` (1), `run-file-on-fuse` (1), `run-crash-handler` (4), `bun-write-leak` (1), `fetch-abort-slow-connect` (2), `plugins` (0), `bun-write` (1), `watch` (2), `require-cache` (8), `resolve` (1), `shell-pipe-read-fault` (11), `shell-write-fault` (1), `spawn-pipe-start-error` (21), `spawn-stdio-syscall-error` (17), `shell-load` (1), `spawn-pipe-leak` (3), `pretty-format-overflow` (1), `spyMatchers` (0), `leak` (7), `snapshot` (1), `readdir-malformed-dirent` (1), `inspect-error-leak` (1), `process-nexttick-is-native-code` (1), `stdin-pause-pty` (1), `dlopen-duplicate-load` (17), `test-process-stdout-async-iterator` (2), `fs` (2), `process` (3). - 3 files fail in fewer tests than on the base, all on time limits with ten files running: `spawn_waiter_thread`, `run-aot` and `stdin-fixtures`. - `spawn-maxbuf` failed once in one test: a `sleep 1` took 1,737 ms against a bound of 1,500. Alone it passes 3 of 3 times on both builds. - **After the 409 files had run** I removed one `#[inline]` attribute from change 1. --- _Generated by [Claude Code](https://claude.ai/code)_ --------- Co-authored-by: Claude <noreply@anthropic.com>
dylan-conway
added a commit
that referenced
this pull request
Nov 4, 2026
…ch`, `Response` and the S3 client (#47895) ### What does this PR do? 29 separate changes, one commit each, in `Blob`, `Bun.write`, `FileSink`, `fetch`, `Response`, the S3 client and the streams. The last three answer the checks and the reviews of the first 26 on this PR: a lint finding (27), and two older bugs beside lines that this PR touches (28 and 29). They follow #47291, #47317, #47338, #47382, #47393, #47414, #47871 and #47893 and work the same way: signatures name the type and the lifetime that the value already has, hand-written pointer code becomes plain ownership and borrows that the compiler checks, and what was never needed is deleted. Together: **131 fewer `unsafe` blocks, 11 fewer `unsafe fn`s and 3 fewer `unsafe impl`s**, +819/−1567 over 48 files. No safe function became `unsafe`, and no wrapper type was added. **[Where the compiled code changes](#where-the-compiled-code-changes) lists every place where the program does different work.** | # | Change | `unsafe` | Diff | |---|---|---|---| | 1 | webcore: a new `Blob` goes to JS by value (`BlobExt::into_js`) | −20 blocks | +51/−147, 10 files | | 2 | webcore: a file read's completion stays a `Box` from creation to `run` | −15 blocks, −7 `unsafe fn` | +105/−216, 4 files | | 3 | webcore: `FileSink`'s entry points take the `RefPtr` their callers hold | −6 blocks | +84/−108, 5 files | | 4 | webcore: `Bun.write`'s promise travels as a `JSPromiseStrong`, not a boxed `WriteFilePromise` | −6 blocks, −1 `unsafe impl` | +70/−128, 2 files | | 5 | webcore: the S3 tasks are set up before `into_raw` and borrow from the caller's `Blob` | −5 blocks | +32/−69, 2 files | | 6 | streams: `WritablePending::promise` returns the `JSValue` every caller wants | −4 blocks | +15/−28, 4 files | | 7 | webcore: `FormDataContext` names the lifetime its entries have | −3 blocks | +22/−40, 1 file | | 8 | webcore: `WriteFileWaitFromLockedValueTask` settles through the `JSPromiseStrong` it owns | −3 blocks | +33/−49, 2 files | | 9 | node:fs: `AsyncFSTask::schedule` for callers that have no `Binding` | −3 blocks | +14/−22, 4 files | | 10 | webcore: replace two drop guards over raw pointers | −2 blocks | +19/−24, 2 files | | 11 | webcore: leak a `Box` with `heap::release`, borrow a `JsCell` with `as_mut` | −5 blocks | +22/−49, 6 files | | 12 | Blob: finish a value before it is boxed; `to_form_data_with_bytes` takes `&[u8]` | −5 blocks, −2 `unsafe fn` | +20/−52, 1 file | | 13 | Blob: `Bun.file(path).formData()` frees the bytes it read | 0 | +30/−2, 2 files | | 14 | webcore: functions exported to C++ take references and `Option<Box<T>>` | −14 blocks | +20/−54, 5 files | | 15 | fetch: `FetchTasklet::on_body_received` needs no raw pointer | −6 blocks | +28/−67, 1 file | | 16 | fetch: `FetchTasklet::get` borrows from a `&'static mut` tasklet | −3 blocks | +9/−27, 1 file | | 17 | s3: the request setup keeps the `&mut AsyncHTTP` that `MaybeUninit::write` returns | −5 blocks | +13/−24, 2 files | | 18 | s3: multipart callbacks borrow from their ref guard; `root` is set through the reference | −4 blocks | +6/−10, 2 files | | 19 | s3: `get_sign_error_message` returns `&'static str` | −1 block | +27/−51, 2 files | | 20 | webcore: remove an exported function and two `unsafe impl Send` that nothing uses | −1 block, −1 `unsafe fn`, −2 `unsafe impl` | +16/−36, 3 files | | 21 | webcore: `Response::to_js` takes the `RefPtr` whose ref the wrapper adopts | −9 blocks, −1 `unsafe fn` | +46/−83, 4 files | | 22 | webcore: drop five detours through raw pointers that the signatures do not need | −5 blocks | +20/−55, 5 files | | 23 | fetch: `S3StreamWrapper` keeps the URL string it needs, not a URL that borrows its sibling | −2 blocks | +8/−25, 1 file | | 24 | fetch: `url` and `proxy` borrow `url_proxy_buffer` | −1 block | +7/−23, 1 file | | 25 | webcore: two `&mut` to a wrapper's payload become `&` | 0 | +10/−10, 2 files | | 26 | http: `ThreadSafeStreamBuffer` is a `Guarded`, and its methods take `&self` | −3 blocks | +73/−145, 9 files | | 27 | webcore: `ReadFile::create` takes the `Blob` it reads | 0 | +8/−22, 2 files | | 28 | HTMLRewriter: `RewriterPipe::init` clones the headers before it allocates the pipe | 0 | +3/−3, 1 file | | 29 | Bun.Image: `.blob()` tells the GC the size of its bytes | 0 | +11/−1, 2 files | The commits can be reviewed one at a time. Worth knowing before reading on: - **Change 13 fixes a leak, with a test.** Every `Bun.file(path).formData()` leaked a buffer the size of the file. - **Change 29 fixes what the GC is told, with a test.** A `Blob` from `.blob()` of a `Bun.Image` counted as 440 bytes, whatever it held. - **Two more leaks and a dangling borrow end, on paths that are rarely taken.** - Change 1: `Archive.files()` leaked a `Blob` when the conversion of a name failed. - Change 5: on a signing error, `list_objects` freed its options while the callee still held a reference to them. - Changes 21 and 28: `RewriterPipe::init` leaked a `Response` with both refs, and the pipe, when the clone of the headers failed. - **Seven changes end an aliasing or contract bug that had no visible effect.** - Change 1: `BlobExt::to_js(&self)` was a safe function that was only right for a pointer from `Blob::new`. - Change 3: a `&mut FileSink` was live while the pump ran JS that reaches the sink through another pointer. - Change 15: the same for a `&mut Response` in `FetchTasklet`. - Change 21: `make_maybe_pooled` was a safe function that dereferenced whatever pointer it was given. - Change 25: `fetch` and `new Request` made a `&mut` to a `Request` and a `Response` that script holds. - Change 10: a guard wrote into `self` through a raw pointer while a `&mut self` method ran. - Change 26: the JS thread and the HTTP thread each made a `&mut ThreadSafeStreamBuffer` from their shared pointer before they had the lock. - **Ownership is in the types:** what is handed over arrives as a value, a `Box` or a `RefPtr` (changes 1, 2, 3, 4, 8, 14 and 21). The messages of changes 3 and 21 follow the ref count on each path. - **Three changes cost a little:** nine callers copy a 120-byte `Blob` once more on its way to JS (change 1), the file-copy loop makes one store more per system call (change 10), and a read of a file calls code that was inline in seven places (change 27). All three are described below. - **Fewer allocations:** one per `Bun.write` (change 4), a clone of the credentials per S3 read (5), and a boxed copy of the URL and a second parse per streamed upload to `s3://` (23). - **Twelve functions that C++ calls take references or `Option<Box<T>>`** (change 14). The message names the call sites and what they pass. - **Dead code goes:** an exported C symbol named `c` and two `unsafe impl Send` (change 20), and an arm of `get_current_response` that could not be taken (15). - **Parts of four changes are compiled only for Windows:** 2 (`ReadFileUV`), 3 (the Windows arm of `Blob.writer()`), 4 (`WriteFileWindows`) and 11 (`CopyFileWindows`, `ReadFileUV`). ### Where the compiled code changes This comparison covers the first 26 changes. Changes 27 to 29 follow at the end of the section. I compared the release assembly (x86-64 Linux, `--emit=asm`) of all 134 crates of the workspace, before and after. For 113 of them the files are the same byte for byte, among them `bun_s3_signing`, which this PR touches. The other 21 I compared function by function, with names demangled and labels normalised. In 19 of the 21 no function differs: what differs is line numbers in the debug information, the paths of the two checkouts and, in one function of `bun_sql_jsc`, the commit's own hash. | Crate | Functions | Differ | Other constants or names only | Same length | Shorter | Longer | Instructions | Branches | Compares | Calls | Panic call sites | |---|--:|--:|--:|--:|--:|--:|--:|--:|--:|--:|--:| | 19 crates, among them `bun_jsc`, `bun_http_jsc`, `bun_install`, `bun_bundler` and `bun_core` | 14,372 | 0 | | | | | | | | | | | `bun_http` | 376 | 8 | 8 | 0 | 0 | 0 | −30 | −1 | −1 | −4 | −1 | | `bun_runtime` | 11,207 | 135 | 27 | 44 | 39 | 25 | −584 | −96 | −90 | −98 | −19 | The last five columns are net, and count the functions that exist on one side only: 22 only before and 16 only after, which were removed, renamed or inlined, or are no longer inlined. Eleven more functions of `impl BlobExt for Blob` have the same code under another mangled name, because change 2 puts an `impl` in front of theirs. 17 of the 27 functions in the fourth column differ only in such a name. **Two of the 26 changes cost something.** - **A `Blob` on its way to JS is copied once more in nine places (change 1).** Every caller used to move its `Blob` (120 bytes) to the heap inline. `into_js(self)` does that now, in a function of its own (68 instructions). A caller that has lent its local `Blob` out before (`&blob`) first copies it into the argument slot. That is 8 loads and 8 stores more per call of `Bun.file()`, `blob.slice()`, `Request.blob()`, `Response.blob()`, `Body::Value::resolve`, `S3Client.file()` (static and on an instance), `construct_s3_file` and per entry of `Archive.files()`. Each of these also allocates the `Blob` and a JS object. In the other nine callers the `Blob` is copied as often as before. 14 of the 18 callers are between 14 and 50 instructions shorter, most of them with 3 or 4 branches, 4 calls and a panic site fewer. - **The copy loop stores its total once per system call (change 10).** The guard that wrote `read_len` on every exit is a wrapper around the loop now, and the loop adds to a `&mut` total. The compiler inlines the wrapper and not the loop, so the total lives in memory: one store more after each `copy_file_range`, `sendfile` or `splice`. The six instances of the loop go from 171–354 to 181–366 instructions, and `CopyFile::run` from 979 to 993. **Checks.** No path makes a check that it did not make before, apart from the bug fix. These are all the functions in which the listing shows more conditional branches, compares or panic call sites than before, and why: | Function | Listing | Why | |---|---|---| | `ToFormDataWithBytesFn::call`, and the closure of `NewReadFileHandler` that calls it | 12 to 30 and 55 to 65, +1 branch and +1 call each | **The bug fix (change 13):** they free the bytes that were read. | | `TextEncoder__encode16` | 1 to 187 | It was a jump to `encode16_impl` (187), whose other caller was the export that nothing used (change 20). The two are one function now. | | `ReadFileCompletionFns::of::cancel` for the read handlers | 1 to 27, +6 branches, +4 calls | It was a jump to the drop of the boxed handler (27), which the compiler now puts here (change 2). | | `S3Client.file()` (static), `construct_s3_file` | 97 to 119 and 6 to 40, +2 and +1 branches | `construct_internal_js` (61) is inline in its two callers (change 1). | | `Archive.files()`'s `run_from_js` | 191 to 199, +3 branches, −2 calls, −1 panic site | Change 1. It drops the `Blob` on the path where the conversion of a name fails, where it leaked it. | | `Blob__onStructuredCloneDeserialize` | 561 to 561, +1 branch, +1 compare, −4 calls | Changes 1 and 10. The guard's drop (41) goes, and the `Blob` is dropped at the two exits that need it. Four boxes are not made. | | `WriteFileWaitFromLockedValueTask::then_wrap` | 296 to 313, +3 branches, +4 calls | Change 8. It read the promise once at the top and reads it in each of the four arms, so once per path as before. It called the drop of the box at four places, and that drop is inline at one. | | `FetchTasklet::queue` | 562 to 566, +1 branch, +1 compare, +1 call, +1 panic site | Change 26. `ThreadSafeStreamBuffer::new` (21, in `bun_http`) is gone. The same allocation, with its test, is inline here. | | `Blob::from_url_search_params` | 130 to 146, +1 panic site | Change 22. `Store::init` is inline: the same allocation with its test. One free fewer. | | `FileSink::pipe_stream` | 451 to 452, +1 branch, +3 compares, −1 call | Change 3. It borrows the caller's `RefPtr`: one load more, and one increment, one decrement and the call of `destroy` fewer. The compiler duplicates a test at three exits. | | `Blob.writer()` | 766 to 771, +1 branch, +1 call | Change 3. One increment fewer. The compiler duplicates one exit, with its free. | | Change | What differs | Effect | |---|---|---| | 2 | The six `run` functions of the read handlers go from 229 to 215, with 2 branches fewer. For `Bun.Image`, `cancel` goes from 53 to 45, `run` from 50 to 42, `on_read_bytes` from 22 to 13 the task's `done` from 65 to 57 and `Image::schedule` from 570 to 565, with 4 branches fewer. | Same reads. | | 3 | Apart from the two rows above: `end_from_js` goes from 432 to 396 with 6 branches fewer, `assign_to_stream` from 424 to 417, `Subprocess::get_stdin` from 107 to 101, `FileSink::to_js` from 11 to 10. `pipe_readable_stream_to_blob` and `spawn_maybe_sync`, which has `Writable::init` inline, are 1 longer. | The ref count goes up and down less often. The message follows it on each path. | | 4 | `write_file_with_source_destination` goes from 1,832 to 1,792 (with 1), `WriteFile::then` from 108 to 99 with 4 calls fewer. One closure of the job is 1 shorter and one 4 longer. | One allocation fewer per `Bun.write`. | | 5 | `list_objects` keeps its 517, and its `resolve` goes from 505 to 509. The drop of the boxed wrapper (116) is the drop of the options (110). `S3BlobDownloadTask::init` keeps its 154, and its callback goes from 147 to 153. | One clone of the credentials fewer per S3 read. | | 6 | `Writable::to_js` goes from 82 to 92 and the two `flush_from_js` from 72 to 77 and from 129 to 137: they call `Strong::value` where they called `Strong::get`, and keep the new handle on the stack for that. | Same promise. | | 9 | `AsyncFSTask::schedule` is a function of its own for `stat`, `fstat` and `unlink` (140, 127 and 138), and `create` forwards to it. `Blob.stat()` goes from 163 to 125 with 5 branches and 6 calls fewer, and `unlink` from 131 to 127. `run_async` goes from 156 to 163 for `fstat` and from 223 to 224 for `unlink`. For the 37 other operations `schedule` is inline in `create`, which keeps its length and uses other registers. | `Blob.stat()` no longer sets up the VM's `NodeFS` on first use, and `unlink` no longer takes a path buffer from the pool for a `Binding` that nothing read. | | 12, 13 | `to_form_data` goes from 228 to 201, with 5 branches fewer. `to_form_data_with_bytes` (112) has no type argument. | Same form data. | | 14 | `Body::Value::from_js` goes from 369 to 351, `Blob__fromMmapWithType` from 133 to 129. | Same values. | | 15, 16 | `FetchTasklet::on_progress_update` goes from 1,619 to 1,560, with 11 branches and 10 calls fewer. `on_resolve` keeps its 310. | Same body. | | 21 | `Response::make_maybe_pooled` (31) is `Response::to_js` (30). `HTMLRewriter::begin_transform` goes from 1,406 to 1,405. | Same wrapper. | | 21, 23, 24, 25 | `fetch_impl::<false>` goes from 4,666 to 4,629, with 5 branches, 7 calls and 2 panic sites fewer. Two `URL::parse`, a `memcpy`, a `memmove`, a shrink of the buffer and two bounds checks go. | Same request. | | 23 | `s3_stream_wrapper_resolve` goes from 181 to 165, with 4 branches and 5 calls fewer. **The URL's string is made when the upload starts, not when it ends.** | One boxed copy of the URL and one parse fewer per streamed upload to `s3://`. | | 26 | In `bun_http`, `ThreadSafeStreamBuffer::new`, `acquire` and `release` go (21, 7 and 2), `report_drain` (9) belongs to the guarded state, and 8 functions differ in the offsets of its fields. `HTTPClient::flush_stream::<false>`, which is instantiated in `bun_runtime`, goes from 126 to 129. | The lock is taken and released where it was. | Changes 7, 11, 17, 18 and 19 ask for the same operations in the same order. Where they show up at all, it is in registers. What changes 2, 3, 4 and 11 do to code for Windows is not compiled on Linux. **Changes 27 to 29** I compared in the same way, the first 26 commits against all 29. 133 of the 134 crates give the same file byte for byte. In `bun_runtime` 10 of 11,205 functions differ, and together they have 599 instructions, 32 branches, 33 compares, 33 calls and 18 panic call sites fewer: | Change | What differs | Effect | |---|---|---| | 27 | `ReadFile::create` was inline in its seven callers, which call it now. `to_string`, `to_json`, `to_json_source`, `to_form_data` and the two `to_array_buffer_view`, which have `do_read_file` inline, are 88 to 90 shorter, and `Image::schedule` goes from 565 to 486. `create` goes from 104 to 107, with 1 branch, 1 compare and 1 panic site more: the `expect` on the store is there now. | **One call more per read of a file.** The same work otherwise. | | 28 | `HTMLRewriter::begin_transform`, which has `init` inline, goes from 1,405 to 1,415, with 1 branch more and 1 call fewer. | The exit for a failed clone drops the `Rc` of the context, which it leaked with the pipe. | | 29 | `PipelineTask::then` keeps its 741 and calls `into_js` (68) where it called `JsClass::to_js`. | **The bug fix:** the size is computed and the GC is told. | The comparisons cover x86-64 Linux only, one crate at a time. The build that ships also inlines across crates when it links. ### The changes in detail #### 1. webcore: a new `Blob` goes to JS by value (`BlobExt::into_js`) `BlobExt::to_js(&self)` was a safe fn that cast `&self` to `*mut Blob` and gave that pointer to the JS wrapper, which is only right for a pointer that came from `Blob::new`. Every caller therefore wrote `let ptr = Blob::new(blob); unsafe { BlobExt::to_js(&*ptr, global) }`, and some set fields through the raw pointer in between. `into_js(self)` takes the `Blob` by value, computes the estimated size, boxes it with `Blob::new` and hands the pointer to the wrapper. Callers set their fields on the value first. `on_structured_clone_deserialize` keeps a `Blob` value to the end, so its error paths drop it instead of running two `deinit` guards, one of them over a raw pointer. `construct_s3_file_internal` had one caller and is gone. `Archive.files()` now drops the `Blob` instead of leaking it when `name.to_js` fails. #### 2. webcore: a file read's completion stays a `Box` from creation to `run` `do_read_file` and `Image`'s `BlobReadChain` boxed their handler, turned it into a raw pointer at once, wrote its promise through the pointer, and each `unsafe fn run(ctx: *mut Self, ..)` / `on_read_bytes(this: *mut Self, ..)` took the box back by hand. The read buffer travelled the same way: a `*mut [u8]` that both consumers turned back into the `Box<[u8]>` it was. `ReadFileCompletion::run`/`cancel` and `ReadBytesHandler::on_read_bytes` take `self: Box<Self>`, `read_bytes_to_handler` takes `Box<H>` and is safe, and `ReadFileRead::buf` is a `Box<[u8]>`. The only raw pointer left is the erased `ctx` of `ReadFileCompletionFns`: `of` makes it from the box and its two shims take the box back, so its fields are private. Erasing the type also erases its lifetimes, so the handler types are bound by `'static`. `NewReadFileHandler` and `BlobReadChain` held a `&'a JSGlobalObject` from the call that started the read; they hold a `BackRef<JSGlobalObject>`. A `ReadBytesHandler` is a `ReadFileCompletion` through one blanket impl, which replaces `Adapter`, `InternalReadFileFn` and `NewInternalReadFileHandler`. `NewReadFileHandler::run` moves the blob out of the box instead of swapping a default one in. The box is still freed before the promise is settled. #### 3. webcore: `FileSink`'s entry points take the `RefPtr` their callers hold `to_js`, `to_js_for_subprocess`, `pipe_stream` and `assign_to_stream` took `&mut self`. Every caller holds a `RefPtr<FileSink>`, which has no `DerefMut`, so they wrote `unsafe { (*sink.as_ptr()).pipe_stream(..) }` or went through `Writable::pipe_sink_mut`. Inside, the functions turned `&mut self` back into the raw pointer they hand to the controller, the JS wrapper and the pump's reactions, and took a guard ref with `unsafe { RefPtr::init_ref(..) }`. That `&mut FileSink` was also live while the pump ran JS that reaches the sink through the controller's pointer. `pipe_stream` and `assign_to_stream` take `this: &RefPtr<FileSink>`: the pointers come from `as_non_null()` / `as_ptr()`, and the fields, all `Cell`/`JsCell`, are read through `Deref`. The borrow of the caller's `RefPtr` keeps the sink alive for the call, so the two guard refs go. `to_js` and `to_js_for_subprocess` take the `RefPtr` by value. The wrapper used to take a ref of its own and every caller dropped its ref right after the call; the caller's ref is the wrapper's. `JSSink::create_object` takes the `NonNull` it stores. `pipe_sink_mut` is deleted. #### 4. webcore: `Bun.write`'s promise travels as a `JSPromiseStrong`, not a boxed `WriteFilePromise` `write_file_with_source_destination` boxed a `WriteFilePromise`, turned the box into a raw pointer, and two lines later took it back with `unsafe`. The job carried the box, `WriteFile::then` leaked it again to pass it as `*mut c_void` to a fn pointer that was always `WriteFilePromise::run`, and `run` needed four `unsafe` blocks to take it apart. The struct also needed a hand-written `unsafe impl JsAffine` for its `*const JSGlobalObject`. The job's JS half is `(JSPromiseStrong, BackRef<JSGlobalObject>)`, both `JsAffine` already, stored in the job itself: one allocation fewer per write. `settle` is `run`'s body on `&mut JSPromise` and `&JSGlobalObject`. `WriteFilePromise` and `WriteFileOnWriteFileCallback` are deleted. The global stays the one the promise was made in. `then`'s `cx.global()` is the VM's own global, which is a different one for a write started in a `ShadowRealm`. `WriteFileWindows` holds the same two values in place of the callback and its context, and takes the promise out before `deinit`. `create_with_ctx` becomes `create`, which only forwarded to it. #### 5. webcore: the S3 tasks are set up before `into_raw` and borrow from the caller's `Blob` `S3BlobDownloadTask::init` leaked its box first and then went through the raw pointer twice: to ref the keep-alive, and to read the path and credentials out of the dupe's store. `read_bytes_to_handler` stashed the path as a raw slice and cloned the credentials for the same reason. The dupe shares its `Store` with the `&Blob` both functions are given, so they read it from there, finish the box while it is still a `Box`, and then leak it. The credentials clone goes with it. The downloaded bytes went `*mut [u8]` -> `&mut [u8]` -> `*mut [u8]` on their way to `ReadFileToJs::call`: `S3ReadHandler` takes the raw slice, under that function's contract. `call_handler` only forwarded to the handler and is written out in its one caller. `list_objects` kept its options in the leaked `Wrapper` only to borrow them back through the pointer. `resolve` never reads them and `s3_client::list_objects` uses them only to build the query string, so they stay a local, freed on return. On a signing error the callback runs inside that call and frees the wrapper, which used to free the options the callee was holding a reference to. #### 6. streams: `WritablePending::promise` returns the `JSValue` every caller wants It returned `*mut JSPromise`, made from the strong's value with `as_promise()` and two `unwrap`s. All nine callers turned the pointer straight back into that value: `FileSink` with `unsafe { (*promise).to_js() }`, the others with `JSPromise::opaque_ref(p).to_js()` and its null check. It returns `strong.value()`. For a new promise the value is read before the strong moves into `self.future`, so the second `match` with its `unreachable!()` arm goes too. #### 7. webcore: `FormDataContext` names the lifetime its entries have `on_entry` and `push_string_slice` took their slices with an anonymous lifetime and stretched three of them with `detach_lifetime` to put them into the joiner. The names, string values, file names and blobs all belong to the `DOMFormData` that `from_dom_form_data` borrows until after `joiner.done()`, which is the lifetime the context already has. Both functions take `'a` now, so the pushes type-check as they are, and the claim sits on the derefs in the `DOMFormData__forEach` callback. `FormDataEntry::File` holds `&Blob`: `resolve_size` takes `&self`. #### 8. webcore: `WriteFileWaitFromLockedValueTask` settles through the `JSPromiseStrong` it owns `then` took a raw `*mut JSPromise` out of `this.promise` on entry, freed `this` in three of the arms and then settled through the pointer. It owns the box, so each arm asks `this.promise` for the promise where it settles it, and the box is freed when `then` returns. The arm that waits for another callback does not look the promise up at all. `write_file_internal` reads the promise's value while the task is still a `Box`, and both sites leak it with `into_raw_nn`. #### 9. node:fs: `AsyncFSTask::schedule` for callers that have no `Binding` `AsyncFSTask::create` takes a `&Binding` only because `run_async` calls every `create` through one function pointer type; it never reads it. To have one to pass, `Blob::stat` cast the VM's `NodeFS` pointer to a `&Binding` in three places, and `unlink` built a whole `Binding::default()`. The body of `create` is `schedule`, without that parameter, and `create` forwards to it. The four callers outside `node:fs` call `schedule`, and `Binding` no longer derives `Default`. #### 10. webcore: replace two drop guards over raw pointers `write_file_with_empty_source_to_destination` took `&mut Blob` and kept a `*mut Blob` for the guard that detaches it, because a guard over the `&mut` would have blocked the reads in between. Everything it calls on the blob takes `&self`, so it takes `&Blob` and the guard holds a copy of that. `CopyFile::do_copy_file_range` counted into a local and had a guard copy it to `self.read_len` through two raw pointers on the way out, one of them into `self` while `self.fallback_read_write(..)` ran. Its body moves to `copy_file_range_loop`, which counts through a `&mut SizeType`. `do_copy_file_range` owns the counter and stores it in `read_len` after the call, on success and on failure: one store, as the guard did. #### 11. webcore: leak a `Box` with `heap::release`, borrow a `JsCell` with `as_mut` `NewSource::new_mut`, `CopyFileWindows::init` and `ReadFileUV::start_with_ctx` turned a fresh `Box` into a raw pointer and dereferenced it at once to get a `&mut`. That is `Box::leak`, which `heap::release` spells for an allocation that is freed elsewhere. `s3::client::readable_stream` did the same by hand on top of `NewSource::new`. It calls `new_mut`, and `new`, which has no other caller, is deleted. `FileSink::source` has `&mut self`, so it can use the safe `JsCell::as_mut` instead of `get_mut`. #### 12. Blob: finish a value before it is boxed; `to_form_data_with_bytes` takes `&[u8]` `from_url_search_params` stored the MIME type through the new store's raw pointer, and `jsdom_file_construct` and `Blob__fromMmapWithType` set `Cell` fields through the pointer `Blob::new` had just returned. Each of them still owns the value one line earlier, so the store is built with its MIME type and the fields are set before `Blob::new`. `to_form_data_with_bytes` only reads its bytes and never looked at its const parameter. It is a safe function over `&[u8]`; `to_form_data` passes `shared_view()`. #### 13. Blob: `Bun.file(path).formData()` frees the bytes it read A file read hands its buffer to `ReadFileToJs::call` as a leaked `Box<[u8]>` with `Lifetime::Temporary`, and the callee has to free it. `ToFormDataWithBytesFn::call` ignored the lifetime, so every `Bun.file(path).formData()` leaked a buffer the size of the file. It holds the buffer in a `TemporaryBytes` for that lifetime, as `to_string_with_bytes` and `to_json_with_bytes` do. The parser copies everything the `FormData` keeps. The S3 path passes `Lifetime::Clone` and still only lends its bytes. #### 14. webcore: functions exported to C++ take references and `Option<Box<T>>` Twelve exported functions took raw pointers and turned them into references, or into a `Box`, in their first lines. None has a Rust caller, and the functions next to them already take `&JSGlobalObject`, `&ReadableSinkController` and `&mut bool`. - The four `TextEncoderStreamEncoder__*` steps take `&TextEncoderStreamEncoder`. Both fields are a `Cell` and a `RefCell`, so the reference may span the chunk's string coercion, which runs user JS. The pointer was already read before the coercion and used after it. - Both `destroyForStream` functions take `Option<Box<T>>` and drop it: the same null test and the same free. - `TextDecoder__createForStream` takes `&mut bool` and `&mut EncodingLabel`. - `JSS3File__bucket`, `__presign` and `__stat` take `&Blob` and `&JSGlobalObject`; `Body__jsonSource` and `Response__createFromParts` take `&JSGlobalObject`. The callers: `encodeAndEnqueue` (JSTextEncoderStream.cpp) makes all four encoder calls with `m_encoder`, under `runNativeArm`, which defers `nativeTransformReleaseState` until the arm returns; the destroy functions get `std::exchange(p, nullptr)` from the finalizers of the two stream cells and from `nativeTransformReleaseState`, and the possibly null `decoder` from the error path of `JSTextDecoderStreamConstructor::construct`, which is also the one caller of `createForStream` and passes the addresses of `bool utf8FastPath = false` and `uint8_t encoding = 0` (`Utf8`); `getterS3File_bucket`, `functionS3File_presign` and `functionS3File_stat` (JSS3File.cpp) pass `wrapped()`, a `void* const` set at construction, of a checked downcast; the last two are entries of the generated table of direct functions, which declares their first parameter as the realm, and are reached through it by compiled code and by `functionCallingDirectly` (InternalForTesting.cpp), which passes the `JSGlobalObject*` of its own host call there. #### 15. fetch: `FetchTasklet::on_body_received` needs no raw pointer Three raw pointers in one function, none of them needed: - `body` was kept as `*mut BodyValue` "because borrowck can't see through the accessors". `Response::get_body_value` and `get_fetch_headers` both take `&self`, so the `&mut BodyValue` can simply be kept. No JS runs between the place it is taken and `BodyValue::resolve`, which already got it as `&mut`. - The buffer reset ran in a `scopeguard::defer!` that reached `scheduled_response_buffer` through a raw pointer taken before the `&mut self` calls of the body. The body is now `deliver_body`, and `on_body_received` resets the buffer after it returns, whatever it returns: every `return` and every `?` of the body ends up there, after the body's own locals are dropped, which is when the guard ran. Panics abort, so there is no other way out. - `current_response_mut` made a `&mut Response` of any lifetime out of `&self`, and it was live while `ByteStream::on_data` ran JS that reaches the same `Response`. Every method called on it takes `&self`. Both places read `self.native_response.get().as_deref()`, as `on_response_finalize` does: a `&Response` that borrows one field, so `scheduled_response_buffer` can still be borrowed mutably next to it. `get_current_response` also fell back to the wrapper behind the weak `response` handle. That arm could not be taken. `on_resolve` is the only place that fills the handle, and it sets `native_response` right after. `clear_data` and `abandon_response_body`, the only places that set `native_response` to `None`, clear the handle on the line before. So there is no wrapper to find when `native_response` is `None`, and the arm is gone. #### 16. fetch: `FetchTasklet::get` borrows from a `&'static mut` tasklet `get` turned the fresh tasklet pointer into a reference of a lifetime it picked, borrowed `request_headers.buf`, `request_body` and `unix_socket_path` from it and widened each borrow to `'static` with `Interned::assume`. The reference can be `'static` itself, which is what the three calls claimed one by one. After those borrows the function only names single fields of the tasklet, so the borrow checker accepts the writes to `http` and the other fields next to them. The one `unsafe` deref carries the reason: the three fields are not touched until `clear_data`, which drops the `AsyncHTTP` first. #### 17. s3: the request setup keeps the `&mut AsyncHTTP` that `MaybeUninit::write` returns The three functions that start an S3 request wrote `task.http` and, a few lines down, asked for the same reference again with `assume_init_mut()`. `MaybeUninit::write` already returns it. `execute_simple_s3_request` held a shared `&'static` task and went back to the raw pointer for everything that needs `&mut`: `signal_store.to()`, the write of `http`, `http_ticket` and `context`. It takes `&'static mut` like `list_objects`, which builds the same `S3HttpSimpleTask`, and like `download_stream`, which sets up its `S3HttpDownloadStreamingTask` the same way: the `'static` loans are on `sign_result.url`, `headers.buf`, `body` and `proxy_url`, and the fields it writes are others. Only `arm_owner`, which wants the raw pointer, is still `unsafe`. #### 18. s3: multipart callbacks borrow from their ref guard; `root` is set through the reference `single_send_upload_response` and `start_multi_part_request_result` adopt the request's ref into a `RefPtr` guard and then dereferenced the raw pointer a second time, with a comment promising that the reference does not outlive the guard. `&*guard` is the same pointer, and the borrow checker holds it to that promise. `writable_stream` and `upload_stream` dereferenced the fresh task pointer once to set `root`, a `Cell`, and once more for the shared reference they keep. They take the reference first and set `root` through it. #### 19. s3: `get_sign_error_message` returns `&'static str` The six messages were byte-string literals, and `error_jsc.rs` turned them back into `&str` with `from_utf8_unchecked` to format them. They are string literals instead, and `get_sign_error_code_and_message`, the one user that wants bytes, calls `as_bytes()`. #### 20. webcore: remove an exported function and two `unsafe impl Send` that nothing uses - `TextEncoder.rs` exported a C symbol named `c`, a copy of `TextEncoder__encode16`. No C, C++, Rust or TypeScript source declares or calls it, and none of the linker lists (`symbols.txt`, `symbols.def`, `linker.lds`, `linker-freebsd.lds`) names it. `encode16_impl`, which the two shared, has one caller left and is written into it, the way `TextEncoder__encode8` is written. - `CompressionStreamCoder` and `AsyncInput` were declared `Send` by hand, and no bound asks for it. Both are used only in their own file, as fields of `CompressionAsyncCtx`, which has an `unsafe impl Send` of its own: it needs one either way, because `RefPtr<T>: Send` wants `T: Sync` too. That impl's comment takes over the reason the coder's impl gave, and `AsyncInput::slice`, whose SAFETY comment pointed at the other one, the reason that one gave. The inventory of the `vm-thread-door` lint is regenerated. #### 21. webcore: `Response::to_js` takes the `RefPtr` whose ref the wrapper adopts `Response::to_js` was an `unsafe fn` over a raw pointer whose contract was "the caller owns a ref, which the wrapper adopts". That is what a `RefPtr<Response>` passed by value means. It gives the wrapper the pointer, sets `js_ref` and the body's stream ref through the `RefPtr` it still holds, and lets go of the ref with `into_raw` at the end. `make_maybe_pooled`, a safe function that dereferenced whatever pointer it was given, is gone. `Response::clone` returns a `RefPtr`, and the callers build theirs with `RefPtr::new`, which is `Box::new` plus `into_raw`. The count, per path: - `JsClass::to_js`, `do_clone`, `construct_json_response`, `redirect`, `error`, `from_parts`, `data_url_response`, the `file:` branch of `fetch_impl` and both arms of `S3StreamWrapper::resolve`: it starts at 1 and the wrapper adopts that ref. No increment and no decrement, before and after. - `FetchTasklet::on_resolve`: it starts at 1 and is incremented once, before and after. The tasklet keeps the ref it made and the wrapper gets the clone, so the increment comes before `to_js` instead of after it. `native_response` is still set before `attach_abort_signal`, which still wants the raw pointer. - `RewriterPipe::init`: it starts at 1 and is incremented once for the pipe, before and after; the path that finds no cell still drops the pipe's ref with the pipe. `RewriterPipe::init` clones the headers of the original before it makes the `Response`, not after, so that no `RefPtr` is live across the `?` on that clone. The clone runs no script and touches neither the pipe nor the new `Response`. When it fails, the `Response` is not allocated, where it was allocated and leaked with both refs. That path still leaks the pipe. (Change 28 ends that.) #### 22. webcore: drop five detours through raw pointers that the signatures do not need - `Request::ensure_fetch_headers` kept the content type as `*const [u8]` "across the field borrow". `body_value()` and `headers_mut()` both take `&self`, so it can stay a `&[u8]`. The deref of the `ByteBlobLoader` pointer stays. - `Request::construct_into` did the same with `ct_ptr`. `ct` is already live across `req.headers_mut()` in the condition above, and `StringView` has no drop glue, so the temporary does not reach the `&mut req` of `bail!`. - `Response::write_format` took `&mut Body` out of the `JsCell` for `Body::write_format`, which takes `&self`. - `Body.rs` had a shim that turned the `&mut DOMFormData` of `DOMFormData::from_js` into a raw pointer, which its one caller turned back. - `URLSearchParams::from_js` handed out a `NonNull`, a second shim made it a raw pointer, and the same caller made it a `&mut`. `from_js` was a safe function already and that was its only use. It returns `Option<&'a URLSearchParams>`, as `DOMFormData::from_js<'a>` does, but shared: the type is an opaque zero-sized handle, and `to_string` only calls the C++ `toString() const`. #### 23. fetch: `S3StreamWrapper` keeps the URL string it needs, not a URL that borrows its sibling For a streamed upload to `s3://`, `fetch()` boxed `url_proxy_buffer`, parsed the URL a second time out of the box through a lifetime eraser, took the proxy href out of it through another, and stored the parsed URL next to the box it borrows from. `resolve` only ever read `url.href`, to make the `Response`'s URL string from it, and `upload_stream` copies the path and the proxy href before it returns. The wrapper holds that `BunString`, and `upload_stream` gets `url.s3_path()` and `proxy.href` of the URLs that are already parsed. The same bytes as before: `URL::parse` keeps its whole input as `href`. The string is made when the upload starts instead of when it ends, once and on the JS thread in both cases, and moved into the `Response`. The `into_boxed_slice` and the second parse are gone, and the buffer is freed when `fetch()` returns. On the four early returns of `upload_stream` (a disturbed or invalid stream, a `Bytes` or `File` source that already failed) the string is made although the callback never runs. Those paths leaked the wrapper before and still do. #### 24. fetch: `url` and `proxy` borrow `url_proxy_buffer` `fetch_impl` parsed its two URLs through `parse_url_detached!`, a macro that erased the lifetime of the slice, because `url_proxy_buffer` is replaced twice while the two variables exist. At each replacement both are only overwritten next, never read, and dropping a `URL<'a>` does not touch `'a`, so a plain borrow is accepted once the statements are in the order "build the new buffer, store it, parse from it". - The `proxy` option: the buffer is stored before the two parses, not after. - The signed S3 URL with a proxy: the old buffer was moved aside so that `proxy_.href` stayed readable and dropped by hand at the end. It is read before the new buffer is stored, which drops the old one. - `has_proxy` is read before the S3 branch instead of after it. Otherwise `proxy` is live across the arm that replaces the buffer when there is no proxy. The branch never changes whether there is one. - That leaves nothing that reads the `proxy` the S3 branch parsed again from the new buffer, so that parse is gone. The old buffers are freed before the parses instead of after them. Nothing else differs. The eraser behind `FetchOptions`, which moves the buffer together with the URLs that borrow it, stays. #### 25. webcore: two `&mut` to a wrapper's payload become `&` `fetch_impl` reborrowed the `Request` it was given as `&mut` at six places, and `Request::construct_into` took a `&mut Response` out of its argument. Script holds the same objects, and every method called on either takes `&self`, like the `&Request` that `construct_into` takes a few lines above. `fetch_impl` keeps the raw pointer and a fresh reborrow per use, instead of one `&Request` for the whole function: option getters run between the uses, and script can call `server.upgrade(request)` there, which writes `request_context`, a plain field. #### 26. http: `ThreadSafeStreamBuffer` is a `Guarded`, and its methods take `&self` The buffer and its drain callback sat next to a bare `Mutex`, and every method, `lock` included, took `&mut self`. So the JS thread (`stream_buffer_mut`) and the HTTP thread (`from_attached`) each made a `&mut ThreadSafeStreamBuffer` out of their shared pointer before they had the lock: two `&mut` to one object on two threads. The two fields are a `State` inside `bun_threading::Guarded`, which hands out `&mut State` only under the lock. `lock` and `clear_drain_callback` take `&self`, `report_drain` is a method of the locked `State`, and `from_attached` and `Stream::buffer_ref` return `&Self`. `StreamBufferGuard`, `acquire`, `release` and `set_drain_callback` are gone. - The three HTTP-thread users (`write_to_stream`, and `drain_send_body` of h2 and h3) took the lock with `acquire()` and gave it back with `release()` on each path. Each holds one guard and has a `drop(locked)` at every place a `release()` was, always before the `detach()` that can free the buffer. Nothing runs under the lock that did not, or outside it that was inside. - `FetchTasklet` borrows the buffer from its `request_body_streaming_buffer` field. That is a borrow of one field, so `self.http.as_mut()` next to it stays legal, which is what the accessor's invented lifetime was for. The two guards there live as long as before. - The drain callback is an argument of `new`, so `FetchTasklet::get` no longer writes it through the fresh raw pointer. Same mutex, same lock and unlock calls. `report_drain` loses its debug assertion that the lock is held: it can only be reached through the guard. The struct, of which there is one per streamed request body, goes from 56 to 64 bytes in a release build for x86_64 Linux (`size_of`): the 4-byte mutex and the 4-byte count no longer share a word, because the padding inside `Guarded<State>` cannot be reused. A debug build has 72 bytes before and after. `mordant.toml` no longer lists `StreamBufferGuard` among the lock guards, and `mordant-baseline.toml` loses the finding that `from_attached` was. #### 27. webcore: `ReadFile::create` takes the `Blob` it reads `do_read_file::<F>` is compiled once for each of its six `F`, and `do_read_file_internal::<C>` once for each `C`. Both made their `ReadFile` in the same way: clone the blob's store, read its offset and its size, call `ReadFile::create`, and unwrap a `Result` that is never `Err`. None of that depends on the type parameter, and mordant's `generic_body_not_generic` reports it in `do_read_file`. `create` takes `&Blob`, does these steps itself and returns the `ReadFile`. It has no other caller. The store is still cloned before `do_read_file` makes its promise. #### 28. HTMLRewriter: `RewriterPipe::init` clones the headers before it allocates the pipe The `?` on `clone_init_headers` stood after the pipe was allocated and its lol-html rewriter built. Nothing owns the pipe's first ref until its wrapper cell exists, so that exit leaked the pipe, the rewriter and the pipe's `Rc` of the context. The exit for a failed wrapper, further down, releases the ref by hand. The clone needs only `original` and the global object, runs no script, and touches nothing that `init` builds. It is now the first statement, so its `?` has nothing to release. No test. `WebCore__FetchHeaders__cloneThis` fills a new, empty `FetchHeaders`, and for an empty one `FetchHeaders::fill` copies three vectors and returns no exception. So the `?` is taken only when an exception is pending for another reason, such as a termination. #### 29. Bun.Image: `.blob()` tells the GC the size of its bytes `PipelineTask` handed its `Blob` to JS with `JsClass::to_js`, which boxes the value and makes the wrapper. It does not call `calculate_estimated_byte_size`, so `reported_estimated_size` stayed 0 and the GC counted the wrapper alone: 440 bytes for a `Blob` that holds a 14 KB PNG. It calls `BlobExt::into_js`, as `Bun.file()`, `blob.slice()` and `Response.blob()` do. `into_js` computes the size first. Its test for an S3 store is false here, because `Blob::init` makes a store of bytes. The new test compares `estimateShallowMemoryUsageOf(blob)` with `blob.size`. ### How did you verify your code works? - **Static checks on the final commits:** `cargo fmt --check`, `cargo clippy --workspace` (0 warnings), the source lints and `cargo check --workspace --release` on Linux, and `cargo check --workspace --release` for `x86_64-pc-windows-msvc` and `aarch64-apple-darwin`, because four changes touch code that only Windows compiles. - **Assembly:** the comparison above. - **The test of change 13** fails on a debug build of the base (memory grows by 131 MiB, the bound is 64) and passes on a debug build of these commits. - **The test of change 29** fails on a debug build without that change (the estimate is 440, and the `Blob` holds 14,578 bytes) and passes with it. - **12 test files on the final commits,** for changes 27 to 29, on a debug build with ASAN, all at once: `image.test.ts`, `image-adversarial`, `html-rewriter.test.js`, `blob.test.ts`, `blob-write`, `blob-file-name-ownership`, `body.test.ts`, `body-clone`, `bun-write`, `FormData.test.ts`, `FormData-file-error-leak` and `readable-stream-blob-consumed`. All pass completely, 1,733 tests. - **30 test files on the first 26 commits**, on a debug build with ASAN, all at once and each cut off after 50 seconds: files for `Blob`, `Body`, `fetch`, `Bun.write`, `FileSink`, the S3 client, streams, `TextDecoderStream`, `FormData` and `Bun.Image`. - 27 files pass completely, 2,130 tests. None fails. - 3 files were cut off: `direct-readable-stream`, `body-stream` and `streams.test.js`. - **After the assembly comparison and the 30 files had run,** a rebase brought in one more commit of `main` (#47836). It left all 26 patches here as they were. The static checks ran after it. - **277 test files before an earlier rebase**, which brought in 31 commits of `main` and left all 26 patches here as they were. They ran on a debug build with ASAN, six at a time, and the files that failed also on a debug build of the base. They are all of `test/js/web/fetch`, `web/workers`, `web/streams`, `web/encoding`, `web/html`, `web/request`, `web/url`, `web/abort`, `test/js/workerd`, `bun/s3`, `bun/spawn`, `bun/image`, `bun/io`, `bun/stream`, `bun/archive-golden` and `node/fs`, 28 files of `bun/http`, 10 of `bun/util` and 4 more. - 260 files pass completely, 18,895 tests. - 12 files fail in the same tests on both builds (the number of failing tests in brackets): `proxy` (it needs `axios`, which I did not have), `bun-write-leak` (1), `spawn_waiter_thread` (1), `spawn-pipe-leak` (3), `streams-string-limit` (1), `fetch-tcp-stress` (4), `streams-leak` (1), `fs.test.ts` (1), `request-clone-leak` (1), `abort-signal-leak-read-write-file` (1), `serve-body-leak` (1) and `fetch-leak` (2). - 3 files fail in fewer tests than on the base, on time limits with six files running: `fetch-abort-slow-connect` (2, base 3), `worker-terminate-funnels` (1, base 9) and `worker.test.ts` (1, base 2). - `spawn-stdin-pipe-fd-leak` failed once with six files running (25 descriptors still open, the bound is fewer than 25). Alone it passes 6 of 6 times, and 5 of 5 on the base. - `fetch.test.ts` fails in "should allow to follow redirect if connection is closed". Alone that test fails 3 of 3 times on both builds. - **After the 277 files had run** I removed one `#[inline(always)]` attribute from change 9. --- _Generated by [Claude Code](https://claude.ai/code)_ --------- Co-authored-by: Claude <noreply@anthropic.com>
dylan-conway
added a commit
that referenced
this pull request
Nov 4, 2026
…ler and the transpiler (#47901) ### What does this PR do? 27 separate changes, one commit each: 15 in `bun_threading`, `bun_alloc` and a few other low-level crates, and 12 in the bundler and the transpiler. They follow #47291, #47317, #47338, #47382, #47393, #47414, #47871, #47893 and #47895 and work the same way: signatures name the type and the lifetime that the value already has, hand-written pointer code becomes plain ownership and borrows that the compiler checks, and what was never needed is deleted. Together: **98 fewer `unsafe` blocks, 26 fewer `unsafe fn`s and 16 fewer `unsafe impl`s**, +521/−1440 over 54 files. No safe function became `unsafe`, and no wrapper type was added. **[Where the compiled code changes](#where-the-compiled-code-changes) lists every place where the program does different work.** | # | Change | `unsafe` | Diff | |---|---|---|---| | 1 | threading: `Semaphore` and `Channel` keep their state in a `Guarded` | −8 blocks, −3 `unsafe impl` | +38/−111, 3 files | | 2 | threading: `Task::from_node` is a cast, and `Batch::pop` reads its own `len` | −5 blocks, −1 `unsafe fn` | +7/−11, 1 file | | 3 | threading: `IntrusiveWorkTask` and `OwnedTask` are safe traits | −5 `unsafe impl` | +11/−22, 2 files | | 4 | alloc: `secure_zero` takes `&mut [u8]` | −4 blocks, −1 `unsafe fn` | +14/−24, 3 files | | 5 | collections: drop `VecExt::from_bump_slice` and `VecExt::writable_slice` | −5 blocks, −4 `unsafe fn` | +19/−104, 8 files | | 6 | sema: the stand-ins that ignore their arguments are safe `extern "C" fn`s | −11 `unsafe fn` | +11/−14, 2 files | | 7 | alloc, opaque: remove dead code that carried `unsafe` | −2 blocks, −1 `unsafe fn`, −6 `unsafe impl` | +5/−186, 7 files | | 8 | alloc: `WTFStringImplStruct::m_ref_count` is an `AtomicU32` | −1 block | +8/−27, 1 file | | 9 | alloc: `Mutex::lock` returns a guard that borrows the mutex | −1 block | +20/−40, 1 file | | 10 | alloc: use the reference `MaybeUninit::write` returns, and index a checked slot | −3 blocks | +4/−8, 1 file | | 11 | alloc: `Zone::init` takes `&'static CStr` | −1 block, −2 `unsafe fn` | +10/−22, 4 files | | 12 | threading: a worker's `Thread` is only ever borrowed shared | −4 blocks | +52/−62, 1 file | | 13 | threading: `Bun__lock` takes `&ReleaseImpl` | −1 block, −1 `unsafe fn` | +2/−3, 1 file | | 14 | ptr: `DebugData::deinit` takes `&self` | −3 blocks | +10/−17, 1 file | | 15 | sql: `PostgresSQLConnection::drop` zeroes `options_buf` with `secure_zero` | −1 block | +2/−5, 1 file | | 16 | bundler: `zig__ModuleInfoDeserialized__deinit` takes the `Box` that it drops | −3 blocks, −1 `unsafe fn` | +3/−19, 1 file | | 17 | transpiler: `print_with_source_map` takes `Option<&mut ModuleInfo>` | −1 block | +6/−24, 3 files | | 18 | bundler: five `unsafe` blocks that plain borrows replace | −5 blocks | +40/−97, 10 files | | 19 | bundler: `BundleThread::enqueue` takes `&self`; `generation` is a local of the thread | −7 blocks | +23/−49, 1 file | | 20 | bundler: `enqueue_entry_points_dev_server` borrows the transpiler where it uses it | −2 blocks | +28/−34, 1 file | | 21 | bundler: `process_html_import_files` borrows four fields of the parse graph | −5 blocks | +20/−41, 2 files | | 22 | transpiler: `Linker` takes what it reads as parameters instead of pointing into its `Transpiler` | −3 blocks | +35/−160, 7 files | | 23 | transpiler: `Transpiler::init` builds its value with a struct literal | −2 blocks | +21/−57, 3 files | | 24 | bundler: three worker contexts that only read the linker hold `&LinkerContext` | −3 blocks, −2 `unsafe impl` | +21/−52, 5 files | | 25 | bundler: `LinkerContext::load` and `link` take the fields they use, not `*mut BundleV2` | −12 blocks, −1 `unsafe fn` | +56/−134, 4 files | | 26 | bundler: the back-pointers of the tasks name the bundle's lifetime | −7 blocks | +29/−70, 5 files | | 27 | threading: `ThreadPool::each_ptr` hands each task a `&mut V` | −9 blocks, −3 `unsafe fn` | +28/−49, 4 files | The commits can be reviewed one at a time. Worth knowing before reading on: - **Nothing that users can see changes.** No test is added or changed. - **Five changes end an aliasing or contract bug that had no visible effect.** - Change 12: a worker's `pop(&mut self)` was not exclusive, because other workers read the same `Thread` through the pool's list while it ran. - Change 18: `is_done` held a `&mut Graph` and a `&mut BundleV2` that contains it at the same time, and `scan_css_imports` was a safe function that dereferenced the raw pointer it was given. - Change 21: `process_html_import_files` made a `&mut Graph` over the whole graph while the source map workers read it. - Change 26: `ParseTask::init` was a safe function that read through a `*mut BundleV2`. - Change 8: `WTFStringImplStruct::ref_count()` read a counter without an atomic while other threads may change it. - **Four functions stay `unsafe fn`, with a smaller contract** that their doc comments state: `ThreadRegistration::new` (change 12), `BundleThread::enqueue` (19), `do_step5` (24) and `LinkerContext::load` (25). `LinkerContext::link` is safe now. - **Locks own what they guard:** `Semaphore` and `Channel` use the existing `Guarded` (change 1), and `bun_alloc::Mutex::lock` returns a guard with a real lifetime instead of a transmuted `'static` one (9). - **The thread pool vouches for what only it can know:** `each_ptr` gives each task a `&mut V` (change 27), and three contexts hold `&LinkerContext` (24). - **`Linker` no longer points into the `Transpiler` that holds it** (change 22), so nothing has to be re-seated after a move, and `Transpiler::init` is a struct literal (23). - **Two functions that C or C++ calls have typed parameters:** `Bun__lock(&ReleaseImpl)` (change 13) and `zig__ModuleInfoDeserialized__deinit(Box<ModuleInfoDeserialized>)` (16). Each message names the call sites and what they pass. - **Dead code goes:** `MaxHeapAllocator`, `FfiLayout` and three `unsafe impl Send`/`Sync` that no bound asks for (change 7), two `VecExt` methods (5), `Transpiler::init_in_place` (23), and three accessors of `Linker` with `reseat_self_refs` (22). - **Parts of four changes are compiled only for Windows, macOS or FreeBSD:** 7 (two arms of `MAX_ALIGN_T`), 11 (the macOS arm of `Zone::init`), 13 (the lock implementations behind `Bun__lock`) and 19 (one line of `thread_main`). ### Where the compiled code changes I compared the release assembly (x86-64 Linux, `--emit=asm`) of all 134 crates of the workspace, before and after. For 42 of them the files are the same byte for byte. The other 92 I compared function by function, with names demangled and labels normalised. In 77 of the 92 (43,319 functions) no function differs: what differs is line numbers in the debug information and the paths of the two checkouts. 15 crates differ: | Crate | Functions | Differ | Other constants or names only | Same length | Shorter | Longer | Instructions | Branches | Compares | Calls | Panic call sites | |---|--:|--:|--:|--:|--:|--:|--:|--:|--:|--:|--:| | `bun_alloc` | 24 | 0 | | | | | +11 | 0 | 0 | +2 | 0 | | `bun_threading` | 81 | 4 | 1 | 1 | 1 | 1 | 0 | −3 | −2 | 0 | 0 | | `bun_core` | 1,159 | 1 | 0 | 0 | 0 | 1 | +1 | 0 | 0 | 0 | 0 | | `bun_resolver` | 509 | 18 | 0 | 9 | 4 | 5 | +2 | +3 | +3 | +1 | 0 | | `bun_sql_jsc` | 409 | 7 | 5 | 0 | 2 | 0 | −43 | −12 | −8 | +3 | 0 | | `bun_jsc` | 1,758 | 165 | 154 | 3 | 5 | 3 | −4 | 0 | 0 | 0 | 0 | | `bun_bundler` | 1,981 | 54 | 31 | 2 | 20 | 1 | −144 | −15 | −14 | −10 | +1 | | `bun_runtime` | 11,205 | 544 | 487 | 16 | 32 | 9 | −358 | −44 | −44 | +13 | −1 | | `bun_standalone_graph`, `bun_sourcemap_jsc`, `bun_lint` | 4,143 | 4 | 0 | 0 | 3 | 1 | −6 | 0 | 0 | 0 | 0 | | `bun_http_jsc`, `bun_install`, `bun_install_jsc`, `bun_js_parser_jsc` | 3,141 | 14 | 11 | 3 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | The last five columns are net, and count the functions that exist on one side only: 7 only before and 8 only after, which were removed, renamed or inlined, or are no longer inlined. The table leaves out one function of `bun_sql_jsc` that holds the commit's own hash. Nearly all of the 689 functions that differ only in constants address a field that moved: the `Transpiler`, which the `VirtualMachine` holds by value, is 24 bytes smaller (change 22). **What costs something.** No change adds an allocation, a lock, a reference count or a test to the source. The compiler's choices differ in these places: - **`String::clone` is called at 17 places where it was inlined (change 8).** A `ref` is the same load, test and `lock add` as before, but the inliner weighs it differently. - `String::create_if_different` (52 instructions) was called at 16 places in the module loader and had `String::clone` inlined. It is inlined at those 16 places now and calls `String::clone` (18 instructions), so each path makes as many calls as before. - At 15 other places in `bun_runtime`, and at one each in `bun_jsc` and `bun_bundler`, `String::clone` is a call and a return more than before. They are in `transpile_source_code_of_any_provenance::<false>` and `transpile_source_code_inner::<true>` (6 together), `__bun_fetch_builtin_module` (3), the three instances of `resolve_without_on_resolve`, `Blob::get`, `realpath_inner`, `Printer::print_dom`, the debug server's `fetch` and `generate_chunks_in_parallel`. - `transpile_source_code_of_any_provenance::<false>` goes from 4,573 to 4,651 instructions for it (below), and `transpile_source_code_inner::<true>` from 2,682 to 2,687 with 3 branches fewer. The other functions named here are between 13 and 51 shorter, except `realpath_inner` (below). - **`free_sensitive` is a call into `bun_alloc` (change 4).** Without its type parameter it is compiled once there (20 instructions), and `Sensitive::drop` calls it (21 to 6). The four closures of `PasswordJob`, `Bun.password.hash` and `Bun.password.verify` are between 4 and 41 shorter. - **`Semaphore::wait` goes from 54 to 61 (change 1),** with the same branches, compares and calls: one `lea` before its wait loop, because `cond` is not at offset 0, and one more callee-saved register to save and restore. - **The one `Channel` is 8 bytes larger (change 1),** 1,072 instead of 1,064, from padding. `render_markdown_file`, which has it on its stack, goes from 1,007 to 1,013. - **`generate_symbol_import_and_use` goes from 257 to 269 (change 5),** with the same 29 branches, 3 compares more and one call fewer. Its loops are vectorised and unrolled as before with other induction variables, and the panic for a slice index out of range is gone. **Checks.** The source makes no check that it did not make before. These are all the other functions in which the listing shows more conditional branches, compares or panic call sites than before, and why: | Function | Listing | Why | |---|---|---| | `Resolver::dir_info_cached_miss` | 1,231 to 1,253, +5 branches, +5 compares, +2 calls | Change 9. The compiler lays out the block that appends two strings to the `BSSStringList` twice, so `append` has 4 call sites instead of 2. A path runs one of the copies. | | `NodeFS::realpath_inner` | 615 to 631, +1 branch, +1 compare, +1 call | Change 8. `Utf8WithString::clone` (78), which only this function called, is inlined here as its two halves. | | `SystemError::clone`, `Blob__setAsFile`, `Request`'s constructor | 161 to 165, 61 to 66 and 1,391 to 1,389, +1 branch and +1 compare each | Change 8. The inlined `String::clone`s test their tags in another order, and one test is duplicated. | | `fetch_impl::<false>` | 4,629 to 4,626, +3 branches, +3 compares, same calls | Change 8. It has a `String::clone` inline. Its frame is 64 bytes smaller, its registers are allotted differently, and three tests are laid out twice. | | `transpile_source_code_of_any_provenance::<false>` | 4,573 to 4,651, +1 branch, +1 compare, +20 calls | Change 8. It has ten copies of `String::create_if_different` inline, each with three call sites where there was one. | | The main function of the bundle thread | 540 to 566, +2 branches, +2 compares, +1 call | Change 19. One exit is laid out twice, so `mi_heap_destroy` has 5 call sites instead of 4. | | `BundleV2::finish_from_bake_dev_server` | 3,953 to 3,963, +2 panic call sites, no branch or compare more | Change 25. It calls `load`. Two bounds checks that shared a call of `panic_bounds_check` with others have their own. | | `Transpiler::init`, `Transpiler::transform` | 2,035 to 2,010 and 3,865 to 3,862, −4 and −1 branches, +1 panic call site each | Changes 22 and 23. One more copy of the exit for a failed allocation. | | Change | What differs | Effect | |---|---|---| | 1 | Apart from the above: `Semaphore::post` (39) and `RawRwLock::unlock_shared` (46), which has it inline, differ in field offsets. | Same locks, waits and signals in the same order. | | 5 | `get_ast` goes from 4,054 to 4,044, with 2 branches and 2 calls fewer. | Two arena allocations fewer per sqlite import. | | 7 | `MaxHeapAllocator::drop` (9) goes. `write_output_files_to_disk` goes from 2,794 to 2,770 and loses its 6 call sites. | That `drop` never had anything to free. | | 8 | Apart from the above: `String::thread_isolated_copy` goes from 73 to 74. In `bun_jsc`, `dupe_with_content_type` goes from 113 to 103, `PathLike::clone` from 254 to 247, `run_from_js_thread` from 800 to 801 and the two `collect_row` from 602 to 612. `handle_request_for` of the two debug servers goes from 348 to 316, and `NodeFS::readdir` from 5,732 to 5,739. | Same counts on the same strings. | | 9 | An out-of-line `slot_at` (28) stands where the out-of-line `at_index` of the `DirInfo` map (26) stood. Apart from the row above, 4 functions of `bun_resolver` are shorter (`load_node_modules` by 40, three by 1), 8 keep their length and 4 are longer (by 1, 2, 8 and 10): the slot's address is computed in another order, and registers shift around it. | The locks are taken and released at the same places. | | 10 | `OverflowList::append` (128), in `bun_resolver`, `bun_jsc` and `bun_install`, spells one test `> 2047` instead of `>= 2048`. | None. | | 12 | `ThreadRegistration::drop` goes from 68 to 64 and loses a null test. `Thread::run`, which has the steal loop inline, is the same. | Same loads and stores with the same orderings. | | 13 | `Bun__lock` is the same, and `Mutex::lock` (3) is emitted as an alias of it. | None. | | 15 | `PostgresSQLConnection::drop` goes from 94 to 73 and the drop of its box from 381 to 359, with 6 branches fewer each. | **The byte loop of volatile stores is one `memset`,** which a `black_box` and a compiler fence keep. | | 17 | `print_with_source_map` keeps its 1,217 with 2 branches fewer. `print_with_source_map_maybe::<false>` goes from 1,008 to 999, and the two `print_ast_esm_ascii_not_bun_cold` from 86 to 78. | Same output. | | 18, 24, 27 | `generate_chunks_in_parallel::<false>` goes from 7,546 to 7,524 with 3 branches fewer, `find_all_imported_parts_in_js_order` from 3,258 to 3,255. `scan_imports_and_exports` keeps its 8,343. One instance of `each_impl::call` (13) has another type argument. | Same tasks on the same threads. | | 20 | `start_from_bake_dev_server`, which has `enqueue_entry_points_dev_server` inline, goes from 346 to 341 with 2 branches fewer. | Same entry points. | | 22, 23 | Five functions that set the `Linker`'s pointers, or set them again after a `Transpiler` had moved, are 3 to 6 shorter: `configure_linker_with_auto_jsx`, `Worker::get`, `transpiler_for_target`, `initialize_client_transpiler` and `for_worker`. `Linker::link::<false, true>` keeps its 500, and `transpile_file::<false>` goes from 839 to 855 with one branch fewer. | The `Transpiler` goes from 3,040 to 3,016 bytes. | | 25 | `BundleV2::link` (39) is new: it splits the borrow and calls `LinkerContext::link`, which goes from 2,617 to 2,562 with 2 branches, 2 calls and a panic site fewer. `load` keeps its 1,996 with one branch fewer. Of the four callers of `link`, three are 5 shorter and `bundle_pages` 11. | Same linking. | Every function that differs and is not named above differs by 4 instructions or fewer. Changes 2, 3, 6, 11, 14, 16, 21 and 26 do not show up in this comparison. What changes 7, 11, 13 and 19 do to code for Windows, macOS and FreeBSD is not compiled on Linux. The comparison covers x86-64 Linux only, one crate at a time. The build that ships also inlines across crates when it links. ### The changes in detail #### 1. threading: `Semaphore` and `Channel` keep their state in a `Guarded` `Semaphore` had a `Mutex` next to an `UnsafeCell<usize>`, and `Channel` a `Mutex` next to an `UnsafeCell<LinearFifo>` and a `Cell<bool>`. Every access to an `UnsafeCell` was an `unsafe` dereference whose reason was "the mutex is held", and three `unsafe impl Send`/`Sync` repeated it for the types. `Guarded<T>` is that pair as one type: the same `bun_threading::Mutex`, the value behind its lock guard, and `Condition::wait_guarded` waits on the guard's own mutex. The lock is taken on entry, given up and retaken inside the wait, and released on return, as before, and the signals are still sent while it is held. `Channel::read_item` was the only reader. It went through `read_all` and `read_items` with a `[MaybeUninit<T>; 1]` cast to `&mut [T; 1]`. It takes the `Option<T>` of `LinearFifo::read_item` instead, which is `None` exactly when the fifo is empty, so the separate length test, the two helpers and the `debug_assert` on the count are gone. `Send` and `Sync` are derived. For `Channel<T, B>` they ask for `B: Send` as well as `T: Send`; the one user has a `StaticBuffer<u32, 256>`. The fields of `Semaphore` are laid out in another order. In the release assembly `post` differs in field offsets. `wait` goes from 54 to 61 instructions with the same branches, compares and calls: one `lea` before its wait loop, because `cond` is not at offset 0, and one more callee-saved register to save and restore. `Channel<u32, StaticBuffer<u32, 256>>` grows from 1064 to 1072 bytes in Linux and macOS release builds. No field is added: `State` and `Guarded` are each padded to a multiple of 8, where the flat struct was padded once. The bytes that `init_static` zeroes are no longer one run, so it has one store more. The one user makes one channel per call of `prefetch_remote_images`, a cold path. In a scratch crate `write_items` differed in field offsets only, and `read_item` made the same calls in the same order. The two `[bun_threading]` entries of `mordant-baseline.toml` counted the removed impls. They are deleted, and the section with them. #### 2. threading: `Task::from_node` is a cast, and `Batch::pop` reads its own `len` `Task::from_node` was an `unsafe fn` around `from_field_ptr!(Task, node, ..)`. `node` is the first field of the `repr(C)` `Task`, which `node_of` already asserts at compile time for the opposite direction, so the subtraction is by zero. It is a safe pointer cast with the same assertion, and its three callers lose their blocks. `Thread::run` and `drain_idle_events` still dereference the result in `unsafe`. `Batch::pop` only stores it in `self.head`, and the next `pop` dereferences it. `Batch::pop(&mut self)` viewed `self.len` as an `AtomicUsize` to load it. `&mut self` is exclusive, so it is a plain read. The release assembly of `bun_threading` is the same before and after. #### 3. threading: `IntrusiveWorkTask` and `OwnedTask` are safe traits Both were `unsafe trait`s without a promise of their own. `IntrusiveWorkTask` said its contract is inherited from its supertrait `IntrusiveField<Task>`, which stays `unsafe` and which `intrusive_field!` implements with `offset_of!`. The trait only adds two provided methods that forward to the supertrait. `OwnedTask::__callback` passes what `from_task_ptr` returns to `Box::from_raw`, so a type must not be able to replace that method in safe code: there is one blanket impl for every `IntrusiveField<Task>` instead of an impl per type, and both arms of `intrusive_work_task!` expand to `intrusive_field!` alone. `OwnedTask` said that `run` executes on a worker thread, which is the `Send` bound. `schedule_owned` gives the box away and relies on nothing else that an implementor writes: a replaced `__callback` can only leak the box unless it has `unsafe` of its own. The `unsafe impl Send` in `owned_task!` stays. No code changes: the same methods are called on the same types. #### 4. alloc: `secure_zero` takes `&mut [u8]` `secure_zero(p: *mut u8, len: usize)` was an `unsafe fn`, and three of its four callers took apart bytes they held safely to call it: a `&mut [u8]` (`Data::zdeinit`), a `&mut Box<[u8]>` (`zero_sensitive`) and a `Box<[T]>` (`free_sensitive`). It takes the slice and is safe. `<[u8]>::fill` is specialised to `write_bytes`, in debug builds too, so it is still one `memset`, followed by the same `black_box` and fence. `Data::zdeinit` passes the slice it has, and `zero_sensitive` its box, which coerces. `free_sensitive` was generic over `T: Copy` and cast to bytes; its one caller has a `Box<[u8]>`, so it takes that. `free_sensitive_cstr` has only a C string, and builds the slice inside the `unsafe` block it already had. Without its type parameter `free_sensitive` is compiled once, in `bun_alloc` (20 instructions in release mode), and `Sensitive::drop` in `bun_runtime` calls it where it held a copy (21 to 6 instructions). The release assembly of `bun_sql` and `bun_s3_signing` is the same before and after. #### 5. collections: drop `VecExt::from_bump_slice` and `VecExt::writable_slice` The two `unsafe fn`s had three callers between them, all in the bundler, and `Vec` serves each of them. The sqlite loader in `ParseTask` filled two arena slices with defaults, overwrote them, and had `from_bump_slice` copy them into new `Vec`s. It builds the `Vec`s from the values: `init_one` for the property and `from_arena_slice` for the two arguments, the form `require_embedded_asset` above uses for its one argument. The `Expr::init` calls keep their order. Two arena allocations, their default values and the drop of the default property are gone. `generate_symbol_import_and_use` took an uninitialised tail from `writable_slice` and assigned to it in a zip loop. `extend` over a mapped slice iterator is `TrustedLen`: one reserve, raw writes and one store of the length, which comes after the loop instead of before it. The closure is `move` so that it holds the index by value even where `extend` is not inlined. `Dependency.source_index` takes `source_index_to_import_from` as it is: `crate::Index` is `bun_ast::Index`, so `Index::init(x.get())` was an identity. The parameter was `impl Into<Index>`, and every caller passes an `Index`, so it is `Index`. In release mode the function goes from 257 to 269 instructions, with the same 29 branches, 3 compares more and one call fewer: the loops are vectorised and unrolled as before with other induction variables, and the panic for a slice index out of range is gone. Eleven comments named the two methods: six in `vec_ext.rs` and one each in `ast_alloc.rs`, `bun_core/lib.rs`, `computeCrossChunkDependencies.rs`, `renameSymbolsInChunk.rs` and `p.rs`. The last two were already wrong about the code below them. #### 6. sema: the stand-ins that ignore their arguments are safe `extern "C" fn`s Eleven of the `#[no_mangle]` stand-ins for C and C++ symbols in `src/sema/standalone` (ten in `native_libraries.rs`, one in `native.rs`) were `unsafe extern "C" fn` although every parameter is unused: four return a constant and seven panic. No argument can make them misbehave, so they are safe, like `Bun__AOT__addressOfOffsetInCode` and the other stand-ins next to them that take no pointer. Nothing names them in Rust. The crates that import them declare the symbols themselves, so no caller changes, and neither does the compiled code. #### 7. alloc, opaque: remove dead code that carried `unsafe` `MaxHeapAllocator`: `init` sets its private `ptr` to `None` and no method allocates, so the `dealloc` in `Drop` never ran and the `unsafe impl Send` and `Sync` guarded nothing. Its only users were three locals and a per-chunk `scope()` guard in `write_output_files_to_disk`, which their own comments called placeholders. `MAX_ALIGN_T`, with its Windows and FreeBSD aarch64 arms, had no other use. That function loses three calls to the `Drop` impl and one store of `len = 0` per chunk. `unsafe impl Send`/`Sync for BSSList`: the list is only reached as a `*mut` out of the `AtomicPtr` static of `bss_singleton!`, which asks for neither. `unsafe impl Send for Zone`: `OnceLock<&'static Zone>` needs `Zone: Sync`, which stays. `unsafe trait FfiLayout` and its `unsafe impl` in `assert_ffi_layout!`: the trait was never a bound and its two constants were never read. The `const` assertions of the macro stay. The `[bun_opaque]` entry of `mordant-baseline.toml` counted the two constants and goes with them. `opaque_deref_mut_nn` had one caller, `opaque_deref_mut`, and is merged into it. Its `debug_assert` for null went: the pointer comes out of a `NonNull`. Apart from `MaxHeapAllocator::drop`, the release assembly of `bun_alloc` and `bun_core` is the same before and after. #### 8. alloc: `WTFStringImplStruct::m_ref_count` is an `AtomicU32` The C++ field is `std::atomic<uint32_t> m_refCount` (StringImpl.h:162). The Rust mirror declared it `Cell<u32>` and had a private accessor that turned the cell's address into an `&AtomicU32` with `AtomicU32::from_ptr` for `is_static`, `ref` and `deref`. The field has the atomic type itself, which has the size and alignment of `u32`, and the accessor is gone. The struct stays `!Freeze`, and `!Send` and `!Sync` through its other fields. Its doc comment said that `ref` and `deref` let C++ write the count; they do it in Rust. One line says who writes the two interior-mutable fields. `ref_count()` read the cell without an atomic. It does a relaxed load, as `StringImpl::refCount()` does (StringImpl.h:429), so it does not race with another thread's `ref` or `deref`. It has four callers: three `debug_assert`s and `Any::memory_cost`. The orderings of the other three are unchanged. The release assembly of `bun_alloc` is the same. In `bun_core` one function differs, `String::thread_isolated_copy`, which touches no count: it tests the two tags in the other order and uses one `add` more to address the 16 bytes it copies. The crates that use `bun_core::String` emit the same load, test and `lock add` for a `ref`, but the inliner chooses differently around them. In `bun_runtime`, `String::create_if_different` (52 instructions) was called at 16 places and had `String::clone` inlined. It is inlined at those places and calls `String::clone` (18 instructions). At 15 other places in `bun_runtime`, and at one each in `bun_jsc` and `bun_bundler`, `String::clone` is called where it was inlined. #### 9. alloc: `Mutex::lock` returns a guard that borrows the mutex `bun_alloc::Mutex::lock` transmuted the `std::sync::MutexGuard<'_, ()>` to `'static` and wrapped it in its own `MutexGuard`, so that `&mut self` methods could be called while the guard was alive. Only one place needed that: `BSSMapInner::get` calls `self.at_index(..)` under the lock. `get_or_put`, `mark_not_found`, `put` and `remove` only touch sibling fields, and the four callers that have a `*mut Self` dereference it for every access. `lock` returns `std::sync::MutexGuard<'_, ()>`. The body of `at_index` moves, with its `unsafe` block, into a private `slot_at` that takes the two fields it reads, and `at_index` and `get` call that. In `get` the compiler now checks that nothing borrows the mutex mutably while it is locked. Four comments said that the guard stores a raw pointer. It held the `std` guard, which is a reference. Those sentences are deleted. What they concluded was not true either, and is unchanged: `(*this).do_append(..)` and `(*this).append_overflow_uninit()` still form a `&mut Self` that spans `mutex` while the guard holds `&mutex`; the borrow checker does not follow borrows through a raw pointer. The locks are taken and released at the same places. The release assembly of `bun_alloc` is the same. In `bun_resolver`, where the map is instantiated, an out-of-line `slot_at` (28 instructions) stands where the out-of-line `at_index` of the `DirInfo` map (26) stood. #### 10. alloc: use the reference `MaybeUninit::write` returns, and index a checked slot `OverflowListBlock::append` and `BSSMapInner::put` wrote a slot with `MaybeUninit::write`, dropped the `&mut T` it returns, and indexed the slot again for `unsafe { assume_init_mut() }`. They keep the returned reference. In `append` it borrows `items` while the sibling `used` is stored, in the same order as before. `BSSListOverflowBlock::append_uninit` returns `Err` when `index >= BSS_LIST_CHUNK_SIZE` and then did `as_mut_ptr().add(index)` in `unsafe`. `&raw mut self.data[index]` is the same address; the array has `BSS_LIST_CHUNK_SIZE` elements, so its bounds check repeats the test above it. The release assembly of `bun_alloc` is the same before and after. In `bun_resolver`, which instantiates all three, `OverflowList::append` spells one test `> 2047` instead of `>= 2048`, and the rest is the same. #### 11. alloc: `Zone::init` takes `&'static CStr` Both `cfg` arms of `heap_breakdown::Zone::init` were `unsafe fn(name: *const c_char)` whose contract was "NUL-terminated and valid for the whole process". That is `&'static CStr`, so the function takes one and is safe. The macOS arm keeps its `unsafe` block for `malloc_set_zone_name` and `&*zone`. Its one caller, `get_zone!`, built the pointer from `concat!($name, "\0")` in an `unsafe` block. It passes the literal on, and the two users of `c_thunks_for_zone!` write `c"brotli"` and `c"zlib"`. Zones exist only in macOS debug builds without ASAN (`ENABLED`). There `init` receives a pointer and a length instead of a pointer, once per zone. Elsewhere the call is dead code: the release assembly of `bun_alloc`, `bun_zlib` and `bun_brotli` on Linux is the same before and after. The macOS arm passes `cargo check --target aarch64-apple-darwin`. #### 12. threading: a worker's `Thread` is only ever borrowed shared `Thread::run` took `&raw mut self_` and dereferenced it in `unsafe` to call `pop` and, twice, `drain_idle_events`, and `ThreadPool::register` stored `(*thread).next` in `unsafe`. `drain_idle_events` and every method of `Queue` and `Buffer` already take `&self`. `pop` took `&mut self` only to store `self.target`, and that `&mut` was not exclusive: while a worker is in `pop`, other workers read its `next`, `run_queue` and `run_buffer` through the pool's list. `next` and `target` are `Cell<*mut Thread>`, which keeps them at offsets 0 and 8. `next` is set before the compare-exchange that publishes the thread and only read afterwards; `target` is used by its own worker alone. Each field says so. `pop` takes `&self`, `run` calls the methods on its local, and `ThreadRegistration` holds `&'a Thread`. `register` had one caller, `ThreadRegistration::new`, and is written into it, so no safe fn leaves the address of a `&Thread` in the pool's list. `new` stays `unsafe`: a leaked guard would leave `CURRENT` and that list pointing at a dead stack frame, and `pool` has to count the calling thread in `spawned`, which `unregister` subtracts from. `unregister` takes `Option<&Thread>` instead of `*mut Thread`, which has the same ABI: `drop` passes `Some(self.thread)`, `warm` and `notify_slow` pass `None`, and the step through `NonNull` and `BackRef` is gone. The steal loop reads and writes the same fields in the same order with the same orderings: the release assembly of `Thread::run`, into which `pop` is inlined, is the same before and after. One function of `bun_threading` differs: `ThreadRegistration::drop` loses the null test of `unregister`'s `maybe_thread` (68 to 64 instructions). #### 13. threading: `Bun__lock` takes `&ReleaseImpl` `Bun__lock` was `unsafe extern "C" fn(ptr: *mut ReleaseImpl)` around `(*ptr).lock()`. Only C calls it, at six places in `packages/bun-usockets/src`: - `fault_inject.c:44`, `:54`, `:63` and `:73` pass `&us_fault_lock`, a `static zig_mutex_t`. - `loop.c:376` and `:400` pass `&loop->data.mutex`, a field of the `us_loop_t` that `us_create_loop` gets from `us_xcalloc`. Both are non-null and zero-initialised, which is the unlocked state of the futex, `os_unfair_lock` and SRWLOCK implementations. `zig_mutex_t` is `uint32_t`, `os_unfair_lock` or `void *`, which have the alignment of the Rust types. Every caller goes on to use the data that the mutex guards, so the object is live for the whole call. All three implementations are interior-mutable, so a shared reference lets other threads lock at the same time. `Bun__unlock` keeps its raw pointer, for the reason `Mutex::unlock_raw` gives. `Bun__lock` compiles to the same instructions. It now equals `Mutex::lock` down to the parameter attributes, so in the Linux release build LLVM emits `Mutex::lock` as an alias of it. The Windows and macOS implementations pass `cargo check` for their targets. #### 14. ptr: `DebugData::deinit` takes `&self` `DebugData` is the canary that `RefCount` and `ThreadSafeRefCount` carry in builds with `debug_assertions`. Its `deinit` took `&mut self` to clear `magic: u32`. `RefCount::deref`, `ThreadSafeRefCount::deref` and `ThreadSafeRefCount::release` have a `&` to the count in scope, so each of them projected the field from the raw pointer a second time, in `unsafe`, to get a `&mut` next to that `&`. `magic` is an `AtomicU32` that is read and written relaxed, `deinit` takes `&self`, and the three callers write `count.debug.deinit()`. Release builds contain none of this: the release assembly of `bun_ptr` is the same before and after. In debug builds the plain load and store of `magic` become relaxed atomic ones. #### 15. sql: `PostgresSQLConnection::drop` zeroes `options_buf` with `secure_zero` `drop` cleared the buffer that holds the connection options, the password among them, with its own loop of one `write_volatile` per byte in `unsafe`. `bun_alloc::secure_zero` takes the `&mut [u8]` and is safe, and `bun_sql_jsc` already depends on `bun_alloc`. The compiled code changes: the byte loop becomes one `memset`, which a `black_box` on the pointer and a compiler fence keep rustc's LLVM backend from removing. `write_volatile` is guaranteed by the language and `black_box` is documented as best effort; this is the form `secure_zero`'s doc comment argues for, and the other Rust code that zeroes a secret it owns uses it (`Data::zdeinit`, `zero_sensitive`, `free_sensitive`, `free_sensitive_cstr`). #### 16. bundler: `zig__ModuleInfoDeserialized__deinit` takes the `Box` that it drops The exported function took `*mut ModuleInfoDeserialized`. It turned the argument into a `NonNull` with `unwrap_unchecked`, turned that back into a raw pointer, and passed it to `ModuleInfoDeserialized::deinit`, an `unsafe fn` with no other caller whose body was `drop(heap::take(this))`. The parameter is now `Box<ModuleInfoDeserialized>` and the body is `drop(info)`. That is what C++ passes: `ResolvedSource.module_info` is an `Option<Box<ModuleInfoDeserialized>>`, and C++ gives back the pointer that it found in that field. A `Box` of a sized type is passed as that pointer, and `heap::take` is `Box::from_raw`, so the same box is dropped. As a `Box` the parameter is `noalias`, `nonnull` and aligned for LLVM. No `unsafe` is left in the function. `deinit`, the `NonNull` import and `use bun_core;` are gone. A `Box` is never null and is owned by the callee. The four C++ call sites hold to both. `Bun__analyzeTranspiledModule` (BunAnalyzeTranspiledModule.cpp) has returned early if `m_moduleInfo` is null, and `releaseModuleInfo` (ZigGlobalObject.cpp) gets a provider only if it is set; both set the field to null after the call. `~SourceProvider` (ZigSourceProvider.cpp) and `~ErrorableResolvedSource` (headers-handwritten.h) test the pointer in an `if`, and the object that held it is gone afterwards. `m_moduleInfo` gets its value only from `std::exchange(resolvedSource.module_info, nullptr)`, so the two objects never hold the same pointer. #### 17. transpiler: `print_with_source_map` takes `Option<&mut ModuleInfo>` The two callers that collect a module record own it in a local `Option<Box<ModuleInfo>>`. They turned `as_deref_mut()` into an `Option<*mut ModuleInfo>`, which went through four functions of `Transpiler`, all safe, one of them public, until `print_ast_esm_ascii` turned it back into the `Option<&mut ModuleInfo>` that `js_printer::Options` holds. The four functions now take `Option<&mut ModuleInfo>` and the two callers pass `module_info.as_deref_mut()`. They touch the box again only after the call has returned. The third caller, in `AsyncModule`, passes `None` as before. The argument is one pointer with null for `None`, where it was a tag and a pointer. #### 18. bundler: five `unsafe` blocks that plain borrows replace `StaticRouteVisitor` held `&'a LinkerContext<'a>`. `LinkerContext` is invariant in its lifetime, so no borrow of the `&mut LinkerContext<'a>` that `generate_chunks_in_parallel` gets has that type, and the borrow was detached. The struct now has two lifetimes, `&'a LinkerContext<'ctx>`, and holds `&*c`. Up to the visitor's last use `c` is only used shared; `write_output_files_to_disk(c, ..)` is in the other arm of the `if`. `is_done` called `(*this).graph.drain_deferred_tasks(&mut *this)`: a `&mut Graph` and a `&mut BundleV2` that contains it, both live. `drain_deferred_tasks` is now a method of `BundleV2` that names `self.graph.*`. It stays in Graph.rs, next to the counters it moves. `scan_css_imports` was a safe function that took `*const [CssCol]` and dereferenced it. It takes `&[CssCol]`. `finish_from_bake_dev_server` has a slice and passes it. `scan_imports_and_exports` has column pointers and uses `col_ref!`, as it does for the arguments next to this one. `find_imported_files_in_css_order` cast a slice to `*const [LayerName]` and dereferenced it, as a cast "between shadow types". `crate::bun_css::LayerName` re-exports `::bun_css::css_parser::LayerName`, so there is one type and the `match` yields `&[LayerName]`. For the same reason the `.cast::<Vec<LayerName>>()` on each of the two pointers that the function hands to `Layers::borrow` cast to the type it already had. Both are gone, with the comments there, on `Layers::borrow` and in `prepareCssAstsForChunk.rs` that spoke of two types. `Layers::borrow`, a safe function whose result `inner()` dereferences, took a `NonNull` for those casts. It takes `&Vec<LayerName>`, and `BackRef::new` stores the same pointer. `run_resolver` kept the slot from `get_or_put` as `*mut u32` in both arms. With `self.graph.build_graphs[target]` written out, which is the body of `path_to_source_index_map`, the arm for an existing entry reads `*entry.value_ptr` next to `self.graph.ast`. The other arm calls `enqueue_parse_task(&mut self)` before it fills the slot and keeps the pointer. The operations and their order stay the same at all five sites. #### 19. bundler: `BundleThread::enqueue` takes `&self`; `generation` is a local of the thread `generation` was a field that only `thread_main` read and wrote, through the raw `instance` pointer. It was also the only field written after the thread has set `ready_event`, and so the reason why no thread could hold a `&BundleThread`. It is now a local of `thread_main`. After `waker` is written, every thread only reads the fields or uses the atomics in `ready_event` and `queue`, so `thread_main` takes one `&*instance` there, in place of a dereference each for `ready_event.set()`, `queue.pop()`, `waker.wait()` and, on Windows, `waker.uv_loop()`. `enqueue` was an `unsafe fn(*mut Self, *mut C)` that rebuilt a `NonNull` with `new_unchecked`. Its one caller has a `NonNull<C>` and passed `.as_ptr()`. It is now `unsafe fn enqueue(&self, NonNull<C>)`. The types say that the instance is live and that the task is not null, and the body has no `unsafe` block: `UnboundedQueue::push` and `Waker::wake` are safe `&self` functions on every platform. It stays `unsafe` for the one obligation that no type carries: `completion` must be a live task node that stays valid until the bundle thread has run or freed it, because `thread_main` dereferences what it pops. The caller dereferences the singleton pointer and calls `enqueue` in one block, where it called the old function. The counter moves from the heap allocation to the thread's stack or a register. The calls and their order stay the same. #### 20. bundler: `enqueue_entry_points_dev_server` borrows the transpiler where it uses it The loop picked the client or the server transpiler at its top and kept it as `*mut Transpiler`, because a borrow would have held `self` across the plugin dispatch that follows. The pointer was dereferenced three times further down: for `resolve_entry_point`, and twice in its error arm for `log`. The transpiler is now picked after the plugin dispatch, right before `resolve_entry_point`, as a `&mut`. What that call returns borrows only `abs_path` and `dot_slash`, so `self` is free again in both arms. The error arm first copies `transpiler.log` and uses the copy; the dereference for `reset()` stays. The pick now runs only for entry points that no plugin matched. It has no side effect here: the one caller is the dev server, which sets `linker.dev_server` and `client_transpiler` before, so `transpiler_for_target` only selects a pointer. #### 21. bundler: `process_html_import_files` borrows four fields of the parse graph The function went through the raw `parse_graph` pointer five times, four of them in every iteration. One of the five, `(*parse_graph).path_to_source_index_map(..)`, made a `&mut Graph` over the whole graph only to call `get(&self)` on one map, while the source map workers that `link` has just scheduled read the same graph. A sixth block made a `&mut Symbol` with `symbol_mut` to set `link`, which is a `Cell`. One block now borrows the four fields that are used: `html_imports.html_source_indices` mutably, and `html_imports.server_source_indices`, `input_files` and `build_graphs` shared. The workers hold `&Graph` and `&BundleV2` at that time, and of the graph they read only `input_files` and `pool`. The symbol comes from `LinkerGraph::symbol`, which is `symbols.get_const(..).expect(..)`. `get_const` has the same two guards as `get` and no bounds check either. `symbol` was compiled only with `debug_assertions`, because its one user was, and loses that `cfg`. The loop iterates over `server_source_indices` instead of indexing it, which drops one bounds check per iteration, and the `source` column and the browser map are looked up once instead of per iteration. #### 22. transpiler: `Linker` takes what it reads as parameters instead of pointing into its `Transpiler` `Transpiler` holds its `Linker` by value. The `Linker` held three raw pointers back into the same `Transpiler`: `options`, `resolve_queue` and `resolve_results`. They were null after `Transpiler::init`, were set by `configure_linker*`, and had to be set again by `wire_after_move` after each move. Three accessors dereferenced them. `resolve_queue` and `resolve_results` served only `enqueue_resolve_result`, which has one caller, `Transpiler::enqueue_entry_points`. It is now an associated function that gets `&mut self.resolve_results` and `&mut self.resolve_queue`. `options` served only two scalar reads at the top of `link`, which now takes `options: &BundleOptions<'_>`. Its two callers in `Transpiler` pass `&self.options`. The two callers outside the crate, in `AsyncModule` and `jsc_hooks`, pass `&(*jsc_vm).transpiler.options` in the `unsafe` block they already have; that is the field the pointer pointed to, because the VM calls `configure_linker*` on the transpiler in place. `Linker` is left with `log`, and `Linker::init` takes only that. The three accessors and `reseat_self_refs` are gone. `link` gets the address in an argument register instead of loading it from `self`, `Transpiler` is 24 bytes smaller, and the debug assertions that the pointers are not null are gone with the pointers. #### 23. transpiler: `Transpiler::init` builds its value with a struct literal `init` made a `MaybeUninit<Transpiler>` on its stack and passed it to `init_in_place`, which wrote the twelve fields with `addr_of_mut!(..).write(..)`. `init` then returned `Ok(slot.assume_init())`. `init_in_place` was meant for callers that own the final storage, but it is `pub(crate)` and `init` was its only caller: the VM, in another crate, calls `init` and writes the result into its field. `init` now has the body of `init_in_place` and ends in `Ok(Transpiler { .. })`, with the fields in the same order, so the calls that compute them run in the same order. All the fallible work still comes first. No large value is moved more often than before. The options went from a local into the slot and now go from the same local into the literal. The resolver went from the result of `Resolver::init1` into the slot and now goes into the literal. The whole struct went from the slot into the `Ok` and now goes from the literal into the `Ok`. The message of the panic for a null `log` names `init` instead of `init_in_place`. #### 24. bundler: three worker contexts that only read the linker hold `&LinkerContext` `find_all_imported_parts_in_js_order` and `run_walks` put `ParentRef::from_ref_mut(this)` in the context they hand to `each_ptr`, and each task turned it back with `unsafe { &*ctx.c.as_mut_ptr() }`. The first of the two built a whole `GenerateChunkCtx` and used only its `c`. Step 5 of `scan_imports_and_exports` wrapped a `*mut LinkerContext` in a struct with its own `unsafe impl Sync`, and `do_step5` began with `&*this`. The three contexts now hold `&LinkerContext`. `each` and `each_ptr` ask for `Ctx: Sync`, which `&LinkerContext` has through the existing `unsafe impl Sync for LinkerContext`. `find_all_imported_parts_in_js_order` and `run_walks` took `&mut LinkerContext` for `from_ref_mut`, and `find_safe_walks` took it to call `run_walks`. All three take `&LinkerContext`, and `do_step5` takes `&self`. It stays an `unsafe fn`: the caller must give each concurrent call its own rows. While the pool runs, the calling thread is blocked in `each` / `each_ptr` and holds only shared borrows of the linker: the context and `worker_pool()`. The tasks hold `&LinkerContext`, as before, and write the rows and the one column they own through the raw pointers of `split_raw(&self)`, which come from the buffer pointer stored in the list and not from the reference. `unsafe impl Send for PrepareCssAstTask` is removed: no bound asks for it. The tasks stay in a local `Vec` and reach the pool as `*mut Task`. The change is in the types only, with two exceptions. `find_all_imported_parts_in_js_order` no longer forms the two unused pointers into `chunks`. And the attributes of the changed parameters follow their types: the three that were `&mut LinkerContext` lose `noalias` and `dereferenceable`, which a `&` to a type with atomics in it does not get, and `do_step5`'s, which was a raw pointer, gains `nonnull` and `align`. #### 25. bundler: `LinkerContext::load` and `link` take the fields they use, not `*mut BundleV2` Both were `unsafe fn(&mut self, bundle: *mut BundleV2, ..)`, because `self.linker.link(self, ..)` does not borrow-check. All five callers hold a `&mut BundleV2` or the `Box`, and each made a raw pointer from it in an `unsafe` block to project `linker`, `graph.entry_points` and `graph.server_component_boundaries`. Inside there were nine more blocks: five project or read a field of `*bundle` that is disjoint from `linker` (`graph`, `dynamic_import_entry_points`, `transpiler`, `has_any_top_level_await_modules`, `unique_key`), two read the graph through `self.parse_graph`, one builds the resolver's `ParentRef` through a pointer cast, and one is `link`'s call of `load`. `load` now takes `&mut Graph`, `&Transpiler` and the `&mut` map of dynamic import entry points, which the callers borrow as disjoint fields next to `&mut self.linker`. It reads the entry points, the boundaries, the sources and the original names from the graph. The pointer cast that "erased" the resolver's lifetime is gone: the lifetime was `'a` already. `link` takes the `bool` and the `u64` it read from the bundle and no longer calls `load`. The new `BundleV2::link` calls both, and four callers call that. The dev server only loads, and calls `load` as before. `load` stays an `unsafe fn`, with a smaller contract. That its arguments are valid and disjoint from `self` is now checked by the compiler. What is left is that `self` keeps the raw `parse_graph` pointer and a `ParentRef` to the transpiler's resolver after those borrows have ended, and that safe accessors such as `parse_graph()` and `resolver()` dereference them later. So both arguments must be those of the `BundleV2` that holds `self`. `BundleV2::link` and the dev server call `load` in one `unsafe` block each. `link` is a safe function. It relies on `load` having run, like its callees `parse_graph()` and `compute_chunks`, which are safe functions too. `load` is separate so that no `&mut Graph` is live while the pool works on this bundle. It runs after parsing is over and before any task of the link step is scheduled, and its borrows end when it returns. `BundleV2::link`'s own `&mut self` does span the link step, and the workers' pointers derive from it. `generate_from_cli` and `generate_from_bake_production_cli` held only the `Box` and a raw pointer there. `LinkerContext::link`, which schedules the source map tasks and step 5, holds `&mut LinkerContext` as before, and two fewer borrows into the graph: the entry points and the boundaries were parameters of it. `load` is called from `BundleV2::link` instead of from `LinkerContext::link`, and the two scalars are read before `LinkerContext::link` is called instead of during it. Nothing writes them while linking. The stores in `load` keep their order. #### 26. bundler: the back-pointers of the tasks name the bundle's lifetime `ParseTask<'a>` and `ServerComponentParseTask<'a>` carry the bundle's `'a`, but their `ctx` was a `ParentRef<BundleV2<'static>, Mut>`. Nine places fill it. The six in `ParseTask.rs` and `bundle_v2.rs` have the bundle as `BundleV2<'a>`, so each wrote `unsafe { ParentRef::from_raw_mut(p.cast::<BundleV2<'static>>()) }`. In the five in `bundle_v2.rs`, `p` is `ptr::from_mut` of the bundle. The sixth is `ParseTask::init`, a safe function that took `*mut BundleV2<'a>` and read through it; its five callers all pass `self`. The three places in `aot` (`plugins.rs`, `native_plugins.rs` and `components.rs`) fill `ctx` from a `BundleV2<'static>` carrier and do not change. `SourceMapDataTask` did the same as the six with `LinkerContext<'static>`. `ctx` is now `ParentRef<BundleV2<'a>, Mut>`, and those six places use the safe `ParentRef::from_ref_mut(self)`. `init` takes `&mut BundleV2<'a>`. `SourceMapDataTask` and `SourceMapData` get the linker's `'a`, and their `ctx` is `ParentRef::new(&*self)`. `ParseTask<'a>` is now invariant in `'a`. Only the runtime's task relied on shortening `'static`, so `RuntimeSource` and the functions that make it are generic over `'a`. The worker side does not change. The thread pool callbacks recover the task from its intrusive field as `ParseTask<'static>`, and so on, as before, so `Result.ctx` and the code in `aot` keep `BundleV2<'static>`. The pointers have the same value and provenance as before: `ptr::from_mut(self)` and `ptr::from_ref(self)` reborrowed `self` too. In `init` the reborrow is now the parameter. It lasts for that call, on the bundle thread, inside a caller that holds `&mut BundleV2` anyway. The change is in the types only, apart from the `noalias` and `dereferenceable` attributes that `init`'s parameter gets as a `&mut`. #### 27. threading: `ThreadPool::each_ptr` hands each task a `&mut V` `each_ptr` takes `values: &mut [V]`, makes one task per index, and blocks until all of them are done. It passed each task a `*mut V`. All six callbacks, which are all in the bundler, dereferenced it with the reason "`each_ptr` hands each task a distinct pointer". Five began with `unsafe { &mut *ptr }`. The sixth, `run_nested_rename_task`, read two fields at its start and wrote one at its end. That the pointers are distinct and live is something only the pool can know. The callback is now `Fn(&Ctx, &mut V, usize)`. `each_impl` has the exclusive borrow of the slice, does not use it while the tasks run, and gives each index to one task, so the block that computed `&raw mut (*values)[i]` makes `&mut (*values)[i]` instead. The bound is higher-ranked, so a callback cannot keep the reference, and `V: Send` was required already. `EachCall::call` is a safe function now. Its `ByValue` impl, which serves `each` and `each_while`, copies out of the reference. Apart from step 5 of the linker, which passes its own snapshot of the reachable files, every caller of those two passes a fresh `vec[(); n]`. No `&mut` lives longer than before in the five callbacks that made one on entry. `generate_chunk` reads the other chunks through `ctx.chunks` while each task holds the `&mut` to its own, as it did before. The address and the bounds check are the same. What changes are the attributes of the callbacks' parameter: as a `&mut V` it is `noalias` and `dereferenceable`. Four of the six callbacks pass it on, and their callees had these attributes on the same pointer already: `post_process_js_chunk`, `post_process_css_chunk` and `post_process_html_chunk` under `generate_chunk`, `generate_js_renamer_` under `generate_js_renamer`, and `find_imported_parts_in_js_order` and `EntryWalk::run` under the two closures. For `finish_js_renamer` and `run_nested_rename_task` they are new; neither uses another way to reach its value. The doc of `GenerateChunkCtx` said that `each_ptr` asks for `Ctx: Sync + Copy`. It asks for `Sync`. ### How did you verify your code works? - **Static checks on the final commits:** `cargo fmt --check`, `cargo clippy --workspace` (0 warnings), the source lints and `cargo check --workspace --release` on Linux, and `cargo check --workspace --release` for `x86_64-pc-windows-msvc`, `aarch64-apple-darwin` and `x86_64-unknown-freebsd`, because four changes touch code that Linux does not compile. - **Assembly:** the comparison above. - **After the assembly comparison and the tests had run,** three more commits went into #47895, which is under these, and a rebase brought in one more commit of `main` (#47900). Together they touch 17 files, none of which this PR touches, and they left all 27 patches here as they were. The static checks ran after that. - **30 test files on the final code,** on a debug build with ASAN, all started at once and each stopped after 50 seconds: 22 files of `test/bundler` (plugins, HTML, CSS, loaders, minify, naming, chunks, `defer()`, metafile, cjs, browser, bytecode, the resolver's cache, the transpiler) and 8 for the other changes (`Bun.password`, `fs.promises`, two Postgres files that bring their own servers, glob, zlib, S3 and `bun -e`). - 26 files pass completely, 1,419 tests. None fails. - 4 files were stopped at 50 seconds: `transpiler.test.js`, `esbuild/css.test.ts`, `bundler_bytecode_portable.test.ts` and `glob/scan.test.ts`. - With fewer files at once, `transpiler.test.js` passes completely (258 tests). - Run by name, the 31 tests of `esbuild/css.test.ts` with `Layer` in their names pass, and the 18 with `AtImport`. So do 11 of the 22 tests of `bundler_bytecode_portable.test.ts`; the others did not end in 50 seconds. - **30 test files on an earlier base,** some of them the same, run the same way: 22 files of `test/bundler` (the API, plugins, HTML, CSS, loaders, minify, naming, splitting, compile, metafile), the 3 files of `test/bundler/resolver`, 3 of `test/bake/dev` and 2 of `test/js/bun/md`. - 21 files pass completely, 559 tests. - 3 files need packages that I did not have, and fail in the same tests on a debug build without these commits: `bundler_edgecase` (`aws-cdk-lib`), `bake/dev/css` (16 tests) and `bake/dev/html` (9 tests, both `happy-dom`). - 6 files were stopped at 50 seconds: `bake/dev/bundle`, `bun-build-api`, `bundler_cjs2esm`, `bundler_compile`, `bundler_splitting` and `md-edge-cases`. In `bake/dev/bundle` 16 tests had failed by then, all for `happy-dom`. In the other five no test had failed by then. - **22 scenarios by hand** on a debug build of an earlier form of the 12 bundler and transpiler commits, and on a debug build of their base. They were `bun build` with splitting, minify, the source map modes, cjs output, CSS layers, server HTML imports, HTML entry points, top-level await, macros, `--compile`, bytecode and `--no-bundle`, running the outputs, `Bun.build` in sequence, in parallel, with plugins and `defer()`, and with failing builds, and the dev server with an edit and a rebuild. Every bundle had the same digest on both builds, and ASAN reported nothing. --- _Generated by [Claude Code](https://claude.ai/code)_ --------- Co-authored-by: Claude <noreply@anthropic.com>
dylan-conway
added a commit
that referenced
this pull request
Nov 5, 2026
…-ares, BoringSSL, `node:quic` and the parser (#47944) ### What does this PR do? 35 separate changes, one commit each: 10 in IPC, N-API, `bun:ffi` and their neighbours, 15 in `node:zlib`, the c-ares and BoringSSL bindings and `node:quic`, and 10 in the parser, the printer, CSS, `Bun.Image` and smaller crates. They follow #47291, #47317, #47338, #47382, #47393, #47414, #47871, #47893, #47895, #47901 and #47940 and work the same way: signatures name the type and the lifetime that the value already has, hand-written pointer code becomes plain ownership and borrows that the compiler checks, and what was never needed is deleted. Together: **125 fewer `unsafe` blocks, 17 fewer `unsafe fn`s and 4 fewer `unsafe impl`s**, +870/−1499 over 58 files. No safe function became `unsafe`, and no wrapper type was added. **[Where the compiled code changes](#where-the-compiled-code-changes) lists every place where the program does different work.** | # | Change | `unsafe` | Diff | |---|---|---|---| | 1 | ipc: `CHANNEL` holds `&'static IPCInstance`, boxed once the transport is up | −9 blocks, −1 `unsafe fn` | +34/−74, 3 files | | 2 | napi: `Bun__napi_check_gc` and `NapiHandleScope__escape` are `safe fn` | −2 blocks | +4/−7, 1 file | | 3 | napi: entry points take the env and scope handles as `Option<&T>` or `&T` | −7 blocks | +92/−73, 1 file | | 4 | napi: `Finalizer::run` and `schedule` borrow their env, and three more borrows | −5 blocks, −1 `unsafe impl` | +14/−27, 2 files | | 5 | tcc: `Config::options` is `Option<&'a ZStr>` | −2 blocks | +9/−12, 2 files | | 6 | ffi: no reference is made from an address only to turn it back into one | −2 blocks, −1 `unsafe fn` | +38/−64, 3 files | | 7 | ipc: the JSON decoder's free callback takes `&Cell<bool>` | −1 block | +7/−9, 1 file | | 8 | sql: the `ssl_config_from_js` and `timer_heap` hooks are safe `fn`s | −1 block, −4 `unsafe fn` | +5/−6, 2 files | | 9 | webview: both `spawn` functions take `&VirtualMachine` | −2 blocks | +10/−23, 2 files | | 10 | crash_handler: `uv_posix` calls `unsupported_uv_function` as a Rust function | −1 block | +16/−17, 2 files | | 11 | node:zlib: the write's completion owns its ref as a `RefPtr<T>` | −4 blocks, −2 `unsafe fn` | +13/−36, 2 files | | 12 | node:zlib: `borrow_input_copy` borrows the copy through `JsCell::get` | −1 block | +4/−6, 1 file | | 13 | node:zlib: `NativeBrotli` passes `&raw mut self.next_in` and keeps `last_result` as a `c_int` | −5 blocks | +11/−32, 1 file | | 14 | c-ares: `AresReply` names its parser as an associated const | −6 blocks | +15/−27, 1 file | | 15 | c-ares: `get_sockaddr` and `get_name_info` take `sockaddr_storage` | −4 blocks | +19/−52, 3 files | | 16 | boringssl: `Bun__X509__checkHost` types its certificate and its out-parameter | −2 blocks | +7/−8, 1 file | | 17 | boringssl_sys: `X509_get_subject_name` is a `safe fn`, and a stack free without fn-pointer transmutes | −2 blocks, −3 `unsafe fn` | +7/−18, 1 file | | 18 | boringssl_sys: the digest getters return `&'static EVP_MD`, and `EVP_DigestInit` is a `safe fn` | −3 blocks | +35/−55, 3 files | | 19 | net: `BlockList__onStructuredCloneDestroy` takes the `RefPtr<BlockList>` it releases | −2 blocks, −2 `unsafe fn` | +4/−14, 1 file | | 20 | node:quic: `on_new_conn` uses the `Conn` it already has | −4 blocks | +5/−27, 2 files | | 21 | node:quic: the loop-driver callbacks take `&QuicEndpoint`; `lookup_cert` stays typed | −4 blocks, −3 `unsafe fn` | +23/−62, 2 files | | 22 | node:quic: `QuicEndpoint::constructor` reads its options before it creates the cell | −6 blocks | +81/−88, 1 file | | 23 | node:quic: `QuicSession::create` and `QuicStream::create` return the reference they make | −7 blocks | +61/−75, 3 files | | 24 | node:quic: a session holds `Option<Conn>` and a stream holds `Option<Stream>` | −13 blocks | +165/−181, 4 files | | 25 | ast: `Binding::to_expr` takes `&mut C` and the method that wraps an identifier | −3 blocks | +18/−160, 5 files | | 26 | js_parser: `P::init` returns the `&mut P` it wrote, and `init_p!` binds it | −5 blocks | +31/−50, 3 files | | 27 | js_printer: the three `write_reserved` callers use `print_slice` | −3 blocks | +12/−47, 1 file | | 28 | image, aot, mimalloc: imports that only write their out-parameters are `safe fn` | −8 blocks | +51/−72, 7 files | | 29 | js_parser_jsc: `MacroContext::call` borrows `self.macros` and `self.bump` | −2 blocks | +6/−20, 1 file | | 30 | ast, sql_jsc: remove three `unsafe impl Send`/`Sync` that nothing needs | −3 `unsafe impl` | +0/−15, 3 files | | 31 | css: two needless `unsafe` in values/ident.rs | −1 block, −1 `unsafe fn` | +2/−11, 1 file | | 32 | valkey, image, sql_jsc, ast: `&Self` guards, `&Blob`, `&Symbol`, a `const` array | −3 blocks | +17/−34, 4 files | | 33 | aot, md: three byte views go through `bun_core::slice_as_bytes` | −3 blocks | +16/−22, 5 files | | 34 | ast: `Rope<'a>` links its segments with `Option<&'a mut Rope<'a>>` | −1 block | +34/−56, 3 files | | 35 | c-ares: `ResolveHandler` names its callback as an associated const | −1 block | +6/−21, 2 files | The commits can be reviewed one at a time. Worth knowing before reading on: - **Nothing that users can see changes.** Under `test/`, a comment in `spawn.ipc.test.ts` named a function that change 1 deletes, and the inventory of the `vm-thread-door` source lint loses the entries of removed `unsafe impl`s (changes 4 and 30). - **Five changes end an aliasing or contract bug that had no visible effect.** - Change 6: `bun:ffi`'s `toBuffer` and `toArrayBuffer` made a `&mut [u8]` over memory that belongs to the caller. - Change 9: both `spawn` functions of the webview were safe functions that dereferenced a `*mut VirtualMachine`. - Change 15: `get_sockaddr` took a `&mut sockaddr`, 16 bytes, and wrote a 28-byte `sockaddr_in6` through it. - Change 24: ten safe functions of `node:quic` took a raw lsquic pointer that they or a later function trusted. - Change 32: `BlobReadChain::start` held a `&mut Blob` across a call that can run JS. - **The parser copies less:** moving the parser's slot into its drop guard was a `memcpy` of 4,344 bytes per parse, and most callers made a second one to drop it. Both are gone (change 26), and `P` is 32 bytes smaller (25). - **N-API entry points say what an add-on may pass** (change 3): `Option<&NapiEnv>` has the ABI of a nullable pointer, null is `None`, and every null test is where it was. - **In `node:quic` the lifetimes come from the owner:** `create` returns a reference that borrows from the endpoint or the session (change 23), and a session holds `Option<Conn>` and a stream `Option<Stream>`, one pointer each, with six repeated null tests fewer (24). - **Functions that C, C++ or add-ons call have typed parameters** (changes 3, 7, 16, 19 and 21). Each message names the call sites and what they pass. - **Imports that are `safe fn`s:** `Bun__napi_check_gc` and `NapiHandleScope__escape` (change 2), `X509_get_subject_name` (17), `EVP_DigestInit` and the digest getters, which return `&'static EVP_MD` (18), and four that only write their out-parameters (28). Each message says what the C side reads and writes, for every argument, in every state and on every thread. The other shims over `NapiEnv` stay unsafe imports, because they belong on the env's JS thread. - **Dead code goes:** `IPCInstance::new` and `deinit` (change 1), `write_reserved`, `advance` and `advance_by` (27), three declarations of BoringSSL functions that nothing calls (18), three `unsafe impl Send`/`Sync` (30) and an `unsafe fn as_slice` (31). - **Parts of four changes are compiled only for Windows or macOS:** 1 (the Windows arm of `get_ipc_instance`), 9 (the macOS `HostProcess::spawn` and the Windows arms of Chrome's `spawn`), 15 (the WinSock names) and 28 (`mi_process_info` on Windows). ### Where the compiled code changes I compared the release assembly (x86-64 Linux, `--emit=asm`) of all 134 crates of the workspace, before and after. For 95 of them the files are the same byte for byte. The other 39 I compared function by function, with names demangled and labels normalised. In 26 of the 39 (30,267 functions) no function differs: what differs is the debug information. 13 crates differ: | Crate | Functions | Differ | Other constants or names only | Same length | Shorter | Longer | Instructions | Branches | Compares | Calls | Panic call sites | |---|--:|--:|--:|--:|--:|--:|--:|--:|--:|--:|--:| | `bun_ast` | 352 | 1 | 0 | 0 | 1 | 0 | −555 | −45 | −45 | −46 | −9 | | `bun_js_parser` | 1,361 | 374 | 327 | 4 | 34 | 9 | +223 | −24 | −32 | +12 | −6 | | `bun_bundler` | 1,983 | 38 | 26 | 0 | 6 | 6 | +143 | +5 | +3 | −8 | −4 | | `bun_runtime` | 11,204 | 131 | 76 | 12 | 21 | 22 | −207 | +2 | +3 | −16 | −8 | | `bun_js_printer` | 130 | 1 | 0 | 0 | 1 | 0 | −11 | 0 | 0 | −1 | 0 | | `bun_install`, `bun_sourcemap_jsc` | 3,000 | 6 | 0 | 0 | 0 | 6 | +97 | +9 | +9 | −7 | 0 | | `bun_sha_hmac` | 34 | 10 | 0 | 1 | 9 | 0 | −54 | 0 | 0 | −24 | 0 | | `bun_boringssl_sys` | 14 | 3 | 0 | 1 | 2 | 0 | −10 | −2 | −2 | −1 | −1 | | `bun_ini` | 59 | 3 | 0 | 0 | 3 | 0 | −24 | −11 | −12 | −1 | 0 | | `bun_js_parser_jsc` | 51 | 2 | 0 | 0 | 2 | 0 | −20 | 0 | 0 | 0 | 0 | | `bun_cares_sys` | 21 | 0 | | | | | −6 | 0 | 0 | 0 | 0 | | `bun_crash_handler` | 123 | 1 | 0 | 0 | 1 | 0 | +5 | 0 | 0 | +1 | 0 | | All 13 | 18,332 | 570 | 429 | 18 | 80 | 43 | −419 | −66 | −76 | −91 | −28 | The last five columns are net, and count the functions that exist on one side only: 38 only before and 27 only after, which were removed, renamed or inlined, or are no longer inlined. Where only constants differ, each is an offset into the parser's `P` and is 32 lower, because `P` is 32 bytes smaller (change 25). The names that differ: - `Store::append` has the same 112 instructions for `E::Object` and `E::Array`, and the compiler keeps one copy in `bun_js_parser`. That copy was named after `E::Object` and is named after `E::Array`, at 173 call sites. - The twelve `do_resolve_cares` load the address of another function (change 35, in the last table), and five functions of `Bun.Image` call a wrapper (change 28, below). **What costs something.** No change adds an allocation, a lock or a reference count, and none adds a test to the source. These are the places where the compiled code does more than before: - **`Binding::to_expr` is compiled once per wrapping method (change 25).** One copy in `bun_ast` (505 instructions), which called the method through a function pointer, becomes four in `bun_js_parser` (210 to 223) and one in `bun_bundler` (225), which call it directly or have it inline. That is about 590 instructions of code more. Each copy is less than half as long as the one it replaces, and a call of it loses the indirect calls and an `expect` per array or object pattern. `maybe_relocate_vars_to_top_level` was one copy (134) for two instances of the parser and is two (130 each), because each calls its own `to_expr`. - **Around it the inliner chooses differently.** `E::Array` and `E::Spread` were stored through `into_data_store` (115 and 111) and two `new_expr` (77 each). They are stored through `Store::append` (112 and 108), which is inline in some parser functions and not in others, in both directions: `parse_type_script_decorators` and `pfx_t_open_brace` of the bundler's parser are 88 and 86 shorter, `pfx_t_open_bracket` of one instance 56 shorter, `parse_prefix` and `parse_jsx_element` of another 31 and 30 longer. In the bundler two `from_bump_vec` (101 and 100) are functions of their own. - **`has_globals_of_vitest` (35) is a function of its own (change 26).** It was inline in both `_parse`, which call it once each. - **`Bun.Image` unpins its input through `JSValue::unpin_array_buffer` (change 28).** Six call sites in five functions called the C++ function through a declaration of their own. They call the wrapper that `bun_jsc` has for it, as `node:zlib` and `node:http` do. The wrapper is one jump, which goes where the link inlines across crates. - **The fields of `EVP` are laid out in another order (change 18).** `md` is a reference now, and the compiler puts it first, so the context is at offset 8. `EVP::drop` (1 to 2), `CryptoHasher::update` and its export for `node:crypto` (1 more each) add 8 to a pointer that they used as it was, and `EVP::final` (40 to 43) keeps that address in one more register. `byteLength`, `digest` and `getDigestSize` are 1 longer for another reason: they load `md` for `EVP_MD_size`, where `EVP_MD_CTX_size` loaded it from the context after a null test. - **The constructor of `QuicEndpoint` goes from 557 to 635 (change 22),** with 3 branches, 3 compares and 3 call sites more. It reads its options before it makes the cell, so when a read throws it has to free the bind host and the `Strong` on the block list itself. Seven fields are stored once instead of twice. It runs once per endpoint. - **The six `uv_*` stand-ins are 1 longer (change 10):** they load the length of their name before the call that ends the process. **Checks.** The source makes no check that it did not make before. These are all the other functions in which the listing shows more conditional branches, compares or panic call sites than before, and why: | Function | Listing | Why | |---|---|---| | In the printer: `FmtAdapter::write_str` and `write_char`, `print_binding`, `print_identifier_utf16`. 15 copies in `bun_bundler`, `bun_install`, `bun_runtime` and `bun_sourcemap_jsc`. | 22 to 38, 59 to 71, 1,122 to 1,134, 106 to 114 and 133 to 162. +1 branch, +1 compare and −1 call per place that writes. | Change 27. Each write called `reserve_next` and `advance_by` in `bun_js_printer`. The capacity test of the first and the store of the second are inline now, and growing the buffer is the only call left. The `memcpy` is the same. In `write_str` the compiler also skips it for an empty string. | | `FFI.toBuffer()` | 224 to 223, +1 compare | Change 6. The compiler duplicates one compare. | Everything else that differs: | Change | What differs | Effect | |---|---|---| | 1 | `get_ipc_instance` goes from 197 to 188, with 2 calls fewer. `Bun__closeChildIPC`, `send` and `emitHandleIPCMessage` keep their length. | **`CHANNEL` is set after the socket is set up instead of before.** Nothing reads it in between. When the set-up fails, no box is made or freed. | | 4 | `napi_create_promise` goes from 76 to 75, with one call fewer: it reads the promise with `value()`, and **stores `*deferred` after that read instead of before it.** | Same promise. | | 10 | Apart from the above: the body of `CrashHandler__unsupportedUVFunction` (46 to 6) is `unsupported_uv_function` (45), and `uv_posix::unsupported` (2) forwards to it. | Only on a path that ends the process. | | 11 | `run_task::handle` goes from 3,119 to 3,101 with 3 branches, 3 calls and 3 panic sites fewer. The three `release_unrun` go from 69 to 63 with one of each fewer. | The same count goes up and down at the same places. | | 12 | The three `write` go from 517, 519 and 519 to 514, 516 and 516. | The same range check, after the copy is filled instead of while it is borrowed. | | 13 | `native_brotli_impl::Context::do_work` goes from 53 to 45. | Same calls of brotli. | | 14 | Six `AresReply::parse`, one jump each, go. | The parser is called directly. | | 15 | `Resolver::reverse` goes from 547 to 545, with one call fewer: for an address that does not parse it calls `on_hostent` and not the C thunk around it. | Same error. | | 17 | `call_general_name_free` goes from 10 to 2: it calls `GENERAL_NAME_free` by name, and loses a null test with its panic. `GeneralNames::drop` keeps its length and passes null where it passed that function. | Same frees. | | 18 | Apart from the above: `Algorithm::md` goes from 63 to 25, because its 16 calls are tail calls. The `init` of the 8 hashers goes from 28 to 26, and `getByName`, `getFromOther` and `copy` are 2 shorter: **one call of `EVP_MD_CTX_init` fewer each**, which `EVP_DigestInit` repeats. `EVP::by_name` goes from 53 to 48, `tls_server_end_point_hash` from 98 to 96 with one null test fewer. | Same digests. | | 20 | `on_new_conn` goes from 683 to 684. Two exits call `Conn::abort_error`, which calls lsquic, where they called lsquic. The two exits that call `Conn::abort_silent` shared a call site and have one each. | Only when a connection is refused. | | 23, 24 | `QuicStream::bind_raw` goes from 264 to 258 with a branch, a call and a panic site fewer. `create` and `create_stream` are 2 shorter, `on_new_stream` is 1 longer. `QuicEndpoint.connect()` goes from 1,032 to 1,035: the exit for a failed connect is laid out twice, so `teardown` has 2 call sites. Four functions keep their length. | Same calls of lsquic. The result of `create` is tested for null instead of by a tag. | | 25 | Apart from the above: two closures per parser instance (6 and 38) and the code in `prepare_for_visit_pass` that stored them go. | `to_expr` on a `P` before `prepare_for_visit_pass` used to panic and now works. Nothing does that. | | 26 | `_parse` goes from 5,133 to 5,075 and from 5,553 to 5,477, `_scan_imports` from 895 to 830 and from 930 to 865, `to_lazy_export_ast` from 582 to 569, `parse_only` from 1,674 to 1,662, the drop glue of the guard in `_parse::<true>` from 615 to 556, and each instance of `P::init` is 4 shorter. In `bun_runtime`, `ContextParameter::of` goes from 2,047 to 1,969 and `write_inline_snapshots` from 2,946 to 2,732. The stack frames of the eight callers of `P::init` named here are 2,208 to 4,736 bytes smaller. | **One or two copies of the whole `P`, 4,344 bytes, fewer per parse.** `write_inline_snapshots` copied `P` in pieces, and copies fewer of them. | | 27 | Apart from the above: `advance_by` (2) goes, and `reserve_next` goes from 19 to 10 because it returns nothing. | Same bytes. | | 29 | `__bun_macro_context_call` goes from 1,876 to 1,870 and its closure from 337 to 323. | Same macro calls. | | 34 | `Rope::append` goes from 24 to 22. In `bun_ini`, `prepare_str` goes from 1,030 to 1,018 with 9 branches fewer, `str_to_rope` from 143 to 136 and `commit_rope_part` from 66 to 61. | Same ropes. | | 35 | The twelve `raw_callback` (33 to 52) had the record type's thunk inline. They go, and the twelve thunks are functions under their own names, with the same instructions apart from change 14. | c-ares calls the same code. | Every function that differs and is not named above differs by 18 instructions or fewer, and has the same or a smaller number of branches, compares and panic call sites. Changes 2, 3, 5, 7, 8, 9, 16, 19, 21, 30, 31, 32 and 33 do not show up in this comparison. What changes 1, 9, 15 and 28 do to code for Windows and macOS is not compiled on Linux. The comparison covers x86-64 Linux only, one crate at a time. The build that ships also inlines across crates when it links. ### The changes in detail #### 1. ipc: `CHANNEL` holds `&'static IPCInstance`, boxed once the transport is up `CHANNEL` held `Option<NonNull<IPCInstance>>` and `get_ipc_instance` returned `Option<*mut IPCInstance>`, so `send`, `close_child_ipc`, `global_object_connected_ipc` and `get_ipc_instance` itself dereferenced a raw pointer. A channel that came up is never freed. The only free was `IPCInstance::deinit`, on the two paths where the socket (on Windows the pipe) could not be set up, and it was needed only because the box was made and published before that attempt. `get_ipc_instance` sets the transport up on the `RefPtr<SendQueue>` it holds and boxes it after that has succeeded: `Box::leak` gives the `&'static IPCInstance` that `CHANNEL` stores and the function returns. On failure it detaches the queue and the `RefPtr` drops. `IPCInstance::new` and `deinit` are gone, and the socket group stays the `&mut SocketGroup` that `spawn_ipc_group` returns. What differs when it runs: `CHANNEL` is set after `from_fd` and `set_timeout` (on Windows after `windows_configure_client`) instead of before. Only `get_ipc_instance`, `global_object_connected_ipc` and `global_object_has_ipc` read it, and none of them runs in between: `us_socket_from_fd` arms the poll, links the socket and may start the date header timer. The failing path neither allocates the box nor touches `CHANNEL`, and it releases the queue after the warning is printed instead of before. The comment on the test of the failing path in spawn.ipc.test.ts named `IPCInstance.deinit`. It says what the path does now. #### 2. napi: `Bun__napi_check_gc` and `NapiHandleScope__escape` are `safe fn` Each was declared with a raw handle pointer and called in an `unsafe` block from a safe method on `&self`, which passed its `as_mut_ptr()`. The handle is the only pointer they take. They take it by reference, as the `safe fn` shims over `JSGlobalObject` do: - `Bun__napi_check_gc` reads `nm_version`, which never changes, and asks the heap whether the current thread is doing GC work, which any thread may ask. It can abort the process. - `NapiHandleScope__escape` answers false once the escape slot is used or the scope is closed. It took the value as `usize`; it takes `JSValue`, which is `repr(transparent)` over it. The empty value is fine: the write barrier and the visitor skip a null cell. The other shims over `NapiEnv` stay unsafe imports: they belong on the env's JS thread, and a `&NapiEnv` is also held on pool threads and by the collector. Same calls with the same arguments. #### 3. napi: entry points take the env and scope handles as `Option<&T>` or `&T` The 44 N-API functions written in Rust that begin with `get_env!`, `preamble!` or `ungated!` took `env_: *mut NapiEnv`, and `get_env!` made the reference with `unsafe { $env.as_ref() }`. `napi_escape_handle` did the same with its scope, `napi_ref_threadsafe_function` and `napi_unref_threadsafe_function` with the env in a debug assertion, and three functions that only Bun's C++ calls with `&*env_` or `as_ref()`. `napi_open_callback_scope` and `napi_close_callback_scope` ignore their env and get the same type. `NapiEnv` and `NapiHandleScope` are `opaque_ffi!` types: zero-sized, align 1 and `!Freeze`, so every non-null pointer is a valid reference to one, and `Option<&T>` has the ABI of a nullable pointer. The parameter types say what the macro did: null is `None` and still answers `napi_invalid_arg`. Call sites of the three that addons cannot reach: - `Bun__napi_register_cleanup_zig(env: &NapiEnv)`: `this`, in the body of the `NapiEnv` constructor (napi.h). - `Bun__napi_run_finalizers_after_teardown_wait(env: &NapiEnv)`: `this`, in `NapiEnv::cleanup()` (napi.h). - `Bun__napi_enqueue_finalizer(env: Option<&NapiEnv>, ..)`, five: `this` in `enqueueRefFinalizer` and `doFinalizer` (napi.h), an env dereferenced just before in `drainOneRefFinalizer` (NapiRef.cpp), one that `node_api_post_finalizer` has tested for null (napi.cpp), and `RefPtr::get()` in `NapiFinalizer::call` (napi_finalizer.cpp), for which it keeps its null test. Every null test is where it was. `type napi_env` stays for the declarations of the functions written in C++ and for the callbacks into addons. #### 4. napi: `Finalizer::run` and `schedule` borrow their env, and three more borrows `Finalizer::run(&mut self)` only reads its fields, and it made an unbounded `&NapiEnv` from `self.env.get()`. It takes `&self` and borrows the env from the `NapiEnvRef` it holds, so the reference lives as long as that ref. Its three callers own the `Finalizer` (two locals, one box just taken back) and drop `mut`. `NapiFinalizerTask::schedule` made the same unbounded reference twice. Both go through the `NapiEnvRef` as well. Each borrow ends with its statement, before `self` is given away: `vm_handle()` returns a `BorrowedRef`, which has no lifetime, and `bun_vm()` returns `&'static VirtualMachine`. The one other use of `global_this` was `global_this.bun_vm()`, which is `vm`. `as_mut()` reads the thread-local itself and uses its receiver in a debug assertion only, so a release build runs the same code. `napi_async_work::run_from_js` dereferenced `env` a second time for `surface_exception`. `env_ref`, made from the same pointer, is still in use after that line: the handle scope guard holds it to the end of the function. `napi_create_promise` read the promise back through the raw pointer it had just made from its box. It reads the value first, with `JSPromiseStrong::value()`, and gives the box away after, so `*deferred` is stored after that read instead of before it. `get().as_value(env.to_js())` reached the same value the long way: it downcast the value in the strong slot to `*mut JSPromise` and turned that pointer back into a `JSValue`. Two FFI calls go (the downcast, `JSC__JSValue__asPromise`, and `NapiEnv__globalObject` for an argument that `as_value` ignores), and with them the `is_cell` test and the null test in `as_promise`, the unwrap of its result and the null test in `opaque_mut`. `napi_node_version::release` was a `*const c_char` to a literal, which needed an `unsafe impl Sync` for the static. It is a `&'static [u8; 5]`, `b"node\0"`: a thin pointer at the same offset, and the struct is `Sync` by itself. The inventory of the `vm-thread-door` lint drops the entry. #### 5. tcc: `Config::options` is `Option<&'a ZStr>` `Config::options` was an `Option<NonNull<ZStr>>`, "kept as a raw ptr rather than a borrow", and `State::init` turned it back into a reference in two `unsafe` blocks on the caller's word that the string outlives the call. Both callers start from a `&ZStr` and build the `Config` in the argument of `init`, and `tcc_set_options` copies what it parses, so a borrow for the call says the same. `Config` has a lifetime and holds the `&ZStr`. In `CompileC::compile` the string can borrow `self.flags`, so the `*mut CompileC` for the error handler is taken before that borrow starts instead of in the `Config` literal. The handler only pushes to `deferred_errors`. Same pointers and the same calls. #### 6. ffi: no reference is made from an address only to turn it back into one `make_napi_env_if_needed` cast the `*mut c_void` from `make_napi_env_for_ffi` to `&'static NapiEnv`. Its one caller hands the result to `Function::compile`, which turned it back into an address for `add_symbol`, where it is a symbol's value and is not dereferenced. `'static` was true (`makeNapiEnvForFFI` returns `&out.leakRef()`, so the allocation is never freed), but nothing used the reference as a reference. The function returns `NonNull::new(..)` of the pointer it got, an `Option<NonNull<c_void>>` with the same niche, and `compile` passes `as_ptr()`. `CompileC::compile` already passes that pointer as it comes. The comment at `add_symbol` called the env VM-owned; it names the leaked ref. The doc of `make_napi_env_for_ffi` called it "owned by this global" and ended "callers in `bun_runtime` cast to `*mut NapiEnv`", which neither caller does. It says that C++ leaks a ref, and the clause is gone. `to_buffer` built a `&mut [u8]` over the caller's foreign memory for `create_buffer_with_ctx`, a private function with this one caller that split the slice into pointer and length again for C++. Its call of `JSBuffer__bufferFromPointerAndLengthAndDeinit` is written into `to_buffer`, with the pointer and length that `get_ptr_slice` returned. Debug builds lose the precondition check inside `from_raw_parts_mut`; release builds make the same call. The helper declared that symbol itself, so the call was made from `bun_runtime` before too; the declaration stands at the head of `to_buffer`. `JSValue::create_buffer_with_ctx` in `bun_jsc` makes the same call but is neither generic nor `#[inline]`, so it would stand between `to_buffer` and C++ wherever the linker does not inline it. `to_array_buffer` built the same kind of slice only for `ArrayBuffer::from_bytes`, which stores the slice's pointer, its length in `len` and `byte_len`, and the type, and takes the other fields from `Default`. A struct literal sets the same four fields from the pointer and length of `get_ptr_slice` and takes the rest from `Default` too. The `unsafe` block stays for `to_js_with_context`, an `unsafe fn`. Debug builds lose the same check here. `noop_bytes_deallocator` has an empty body and is not an `unsafe fn`. It still coerces to the `unsafe extern "C" fn` pointer that the deallocator slot holds. #### 7. ipc: the JSON decoder's free callback takes `&Cell<bool>` `json::decode_ipc_message` wraps an ASCII line in an external string and gave the address of a local `bool` as the context of the string's free callback, which set the flag through the `*mut bool` in an `unsafe` block. `create_external` is generic over the context and documents it as a raw pointer or `&T`. The flag is a `Cell<bool>`, the context is `&Cell<bool>` and the callback calls `set(true)`. The callback has two callers: `create_external` itself when the line is too long, and `ExternalStringImpl::freeExternalBuffer` when the string is destroyed, with the `m_freeCtx` it stored at creation (`releaseBufferEarly` also calls it; nothing calls that). The `unsafe` on `create_external` stays, and its comment names the flag too: the string carries the lifetime of neither borrow. The same byte is stored and read back. #### 8. sql: the `ssl_config_from_js` and `timer_heap` hooks are safe `fn`s `SqlRuntimeHooks::ssl_config_from_js` was an `unsafe fn(&JSGlobalObject, JSValue)` and its one call sat in an `unsafe` block whose comment named no obligation ("may run JS getters / throw"). The one implementation calls `SSLConfig::from_js` and boxes the result, all safe. The field is a `fn`, the implementation and the call are safe. `timer_heap` was an `unsafe fn(*mut VirtualMachine) -> *mut c_void` and states no obligation either: the one implementation ignores its argument and calls the safe `timer_all()`. The field and the implementation are a `fn`. The block at its one call stays, for the dereference of the result. An `unsafe fn` does not coerce to a `fn` field, so an implementation of either hook has to be safe. The same indirect calls. #### 9. webview: both `spawn` functions take `&VirtualMachine` `ChromeProcess::spawn` and the macOS `HostProcess::spawn` are safe functions that took `vm: *mut VirtualMachine` and dereferenced it twice. Each has one caller, which holds a `&JSGlobalObject`: `Bun__Chrome__ensure` passed `bun_vm_ptr()`, and `Bun__WebViewHost__ensure` cast the `&'static VirtualMachine` from `bun_vm()` to `*mut`. Both callers pass `global.bun_vm()`. `VirtualMachine::event_loop` and `EventLoopHandle::init` are safe functions, so that block goes in each. The block that dereferences the raw `transpiler.env` stays. Its comment in ChromeProcess.rs called `map` a `&mut Map` slot; it is a field of the loader, and the sentence goes. In a release build `bun_vm()` and `bun_vm_ptr()` are the same read of the thread-local, so the same code runs. #### 10. crash_handler: `uv_posix` calls `unsupported_uv_function` as a Rust function `uv_posix::unsupported` reached the crash handler, which is Rust, through its C symbol. It declared `CrashHandler__unsupportedUVFunction` itself, returning `()` where the definition returns `!`, called it in an `unsafe` block with the pointer of its `&'static CStr`, and put `unreachable!` after the call. The body of the symbol moves into `pub fn unsupported_uv_function(name_bytes: &'static [u8]) -> !`. The symbol, which `__bun_throw_not_implemented` in uv-posix-polyfills.c calls, keeps its `cstr` and calls that function. `uv_posix` calls it too and has no declaration of its own. Only the C symbol needed the NUL terminator, so `unsupported`, `handle_type` and `ref_state` take the name as `&'static [u8]`, and the six literals are `b"uv_close"` and so on. The literals are one byte shorter. The rest differs only on a path that ends the process: from `uv_posix` the length of the name comes with the slice instead of from a `strlen`, which is one more instruction before the call in each of the six. From C the counter is bumped after the `strlen` instead of before it, and the reported stack starts one frame deeper unless the function is inlined into the symbol. #### 11. node:zlib: the write's completion owns its ref as a `RefPtr<T>` `run_from_js_thread` and `release_unrun` were `unsafe fn(*mut T)`. Each turned the pointer into a `ParentRef` behind an `expect` and released the ref that `write()` took with `unsafe { T::deref(this_ptr) }`, once on each of their four exits. Both now take `RefPtr<T>`, read the stream through it and `drop` it where the `deref` was, after `poll_ref.unref`. Their two callers, the dispatch arm and `Taskable::release_unrun`, keep one `unsafe` block each: `RefPtr::from_raw` on the queued pointer, which adds no ref. `RefPtr::drop` calls `rc_deref`, which the derive forwards to the same `CellRefCounted::deref`, so every path still releases exactly one ref. The two `expect` null tests are gone. #### 12. node:zlib: `borrow_input_copy` borrows the copy through `JsCell::get` The slice was taken inside the `with_mut` closure and rebuilt with `slice::from_raw_parts` to get it out of the closure's `&mut` borrow. The closure now only fills the copy and says whether the allocation worked. The slice is taken afterwards from `input_copy().get()`, a shared borrow that lasts as long as `&self`, which is what the signature already promised. The range check is the same one `get(range)`, moved after the closure. `write()` does not touch `input_copy` between this call and `set_buffers`. #### 13. node:zlib: `NativeBrotli` passes `&raw mut self.next_in` and keeps `last_result` as a `c_int` `do_work` copied `next_in` into a local, passed the local's address to brotli and then advanced the field with `self.next_in.add(local - self.next_in)`, in both modes. It now passes the address of the field, as it does for `next_out`. brotli only advances `*next_in` and does not keep the address. `last_result` was a union of the encoder's `c_int` and the decoder's `BrotliDecoderResult`, read with `unsafe` in three places. It is a `c_int`: the decode arm stores `result as c_int` and compares the enum value it still holds, and `get_error_info` compares with `needs_more_input as c_int`. The slot keeps its size and its initial value 0. The union had no other user. #### 14. c-ares: `AresReply` names its parser as an associated const `AresReply::parse` was an `unsafe fn` whose six impls each forwarded their three arguments to the matching `ares_parse_*_reply` extern from inside an `unsafe` block. The trait now has `const PARSE: unsafe extern "C" fn(..)`, the shape `HostentWithTtlsHandler::PARSE` has, and each impl names its extern. The six externs have exactly that signature. `ares_reply_callback`, the only caller, keeps its `unsafe` block and calls `(R::PARSE)(..)`, which is a direct call to the C function once the callback is monomorphised. #### 15. c-ares: `get_sockaddr` and `get_name_info` take `sockaddr_storage` `get_sockaddr` took `&mut sockaddr`, 16 bytes, and wrote a 28-byte `sockaddr_in6` through it. Its one caller, `Resolver::global_lookup_service`, cast its `sockaddr_storage` to that type in an `unsafe` block, twice. Both functions now take the `sockaddr_storage` the caller has. `get_name_info` takes it shared: `ares_getnameinfo` has `const struct sockaddr *` and copies the address. The first cast block in dns.rs goes, the second shrinks to `&mut *request`. Inside `get_sockaddr`, the two address stores went through `(&raw mut field).cast().write(..)`. They assign `sin_addr.s_addr` and `sin6_addr.s6_addr`, which libc and `bun_windows_sys::ws2_32` both name so: the same 4-byte and 16-byte stores. `get_host_by_addr` reported an unparsable address by calling its own C thunk with `ARES_ENOTIMP`. The thunk only turns the pointer back into `&mut T` and the status into `Some(Error::ENOTIMP)`, so it calls `ctx.on_hostent` with that. Only `Error::ENOTIMP` names `ARES_ENOTIMP` now, so it is a plain `const` like its neighbours that only the enum names. The comment on the `winsock` import in cares_sys is cut to the one line that still holds. #### 16. boringssl: `Bun__X509__checkHost` types its certificate and its out-parameter `x509` was a `*mut X509` turned into a reference with `unsafe { as_mut() }`, and `out_peer` a `*mut String` that `set_peer` tested for null and wrote with `unsafe { out_peer.write(..) }`. They are `Option<&mut X509>` and `Option<&mut MaybeUninit<String>>`: the same two null tests, and `MaybeUninit::write` stores without dropping, as `ptr::write` did. The function stays `unsafe` for `host_ptr`. Its one caller is `X509View::checkHost` in ncrypto.cpp. It returns early when `cert_` is null, and passes either null or the address of a local `BunString` that it initialises to the empty string and does not touch during the call. `X509` is an opaque zero-sized `!Unpin` type, so `&mut X509` claims nothing about the certificate's memory. #### 17. boringssl_sys: `X509_get_subject_name` is a `safe fn`, and a stack free without fn-pointer transmutes `X509_get_subject_name` (crypto/x509/x509_cmp.cc:69) returns the address of a member and reads nothing. It is a `safe fn` over `&X509`. `common_names` is its only Rust caller. `GeneralNames::drop` transmuted `GENERAL_NAME_free` to `fn(*mut c_void)` and passed it to `sk_pop_free_ex` as `free_func`, and its trampoline unwrapped that argument and transmuted it back before calling it. `OPENSSL_sk_pop_free_ex` (crypto/stack/stack.cc:112) only passes `free_func` on to the trampoline, so `drop` passes `None` and the trampoline calls `GENERAL_NAME_free` by name. That removes two transmutes and an `expect`, and the indirect call per element becomes a direct one. #### 18. boringssl_sys: the digest getters return `&'static EVP_MD`, and `EVP_DigestInit` is a `safe fn` The 16 `EVP_*()` getters returned `*const EVP_MD`, so `EVP::init` was a safe fn with a `# Safety` section about its `md` pointer. Each getter returns the address of a static object (crypto/fipsmodule/digest/digests.cc.inc through `DEFINE_DATA` in delocate.h:62, and crypto/digest/digest_extra.cc:276, 301, 326, 359, 393 and 416), so they return `&'static EVP_MD`. `EVP_get_digestbyname` (digest_extra.cc:218) and `EVP_get_digestbynid` (:79) walk one table (:43) and return what one of these getters or `EVP_md5_sha1` returns, or null. `EVP_md5_sha1` (:455) returns the address of a constant at file scope too. Both return `Option<&'static EVP_MD>`. `Algorithm::md()` and the `EVP::md` field follow. `Option<&EVP_MD>` is one word where `Option<*const EVP_MD>` was two. A result that goes on to a `*const EVP_MD` parameter coerces, so those calls are unchanged. Two callers tested a result for null. `EVP::by_name` matches on the `Option`. In `X509::tls_server_end_point_hash` the test stood after a `match` with an `EVP_sha256()` arm and an `EVP_get_digestbynid()` arm. The second arm ends in `?` and the test goes, so that arm has one null test as before and the first has none. `EVP_DigestInit` (crypto/fipsmodule/digest/digest.cc.inc:162) starts with `EVP_MD_CTX_init`, which overwrites the three pointers of the context before anything reads them, then calls `EVP_DigestInit_ex`, which keeps `type` in `ctx->digest` (:157). It becomes `safe fn(&mut EVP_MD_CTX, &'static EVP_MD)`. `EVP::init` calls it instead of `EVP_MD_CTX_init` followed by `EVP_DigestInit_ex`: the same work in one call. The `new_evp!` hashers in sha.rs called `EVP_MD_CTX_init` and then `EVP_DigestInit`. They lose the first call and the block around the second: one FFI call fewer per `init`, for the 8 types. `EVP_MD_CTX_init` has no Rust caller left and its declaration goes. bun-sema links no BoringSSL and has weak stand-ins in src/sema/standalone/native_libraries.rs: its `EVP_sha256` (:167) returns the address of a static, and its `EVP_DigestInit` (:199) aborts on any other `type_` and overwrites the whole context, so both keep what the signatures promise. Its `EVP_MD_CTX_init` (:187) stays, because a test in that file calls it. `EVP::size` asked `EVP_MD_CTX_size(&self.ctx)`, which is `EVP_MD_size(ctx->digest)` after a null test. `ctx.digest` is `self.md` from `init` on, so it asks the new `safe fn EVP_MD_size(&EVP_MD)` (digest.cc.inc:36). `ctx` was a `pub` field that nothing outside EVP.rs used. It is private, so only EVP.rs can break that equality. `EVP_MD_CTX_size` has no caller left and its declaration goes. `EVP_MD_do_all_sorted` has no caller either, and its declaration goes too. The comment on `md` repeated its type and goes. `EVP::reset` is unchanged. #### 19. net: `BlockList__onStructuredCloneDestroy` takes the `RefPtr<BlockList>` it releases The hook was `unsafe extern "C" fn(*mut c_void)` and ended in `unsafe { BlockList::deref(ptr.cast()) }`, the only use of that inherent `unsafe fn deref`. It now takes `bun_ptr::RefPtr<BlockList>` by value, which is `repr(transparent)` over `NonNull`, so C++ passes the same pointer. It removes the table entry under the lock as before and then drops the `RefPtr`, which calls the same `ThreadSafeRefCount::deref`: one ref down per call, matching the one ref up in `BlockList__onStructuredCloneSerialize`. `BlockList::deref` goes. SerializedScriptValue.cpp calls it in two places, the destructor (line 4300) and `releaseSerializedRefs` in `create` (line 4957). Both walk a vector whose only `append` (line 1440) stores the `blockList->wrapped()` that the line before passed to the serialize hook as `&BlockList`, and each vector is walked once. #### 20. node:quic: `on_new_conn` uses the `Conn` it already has `QuicEndpoint::on_new_conn` wraps lsquic's pointer in a `Conn` at its top. Two later exits wrapped the same pointer again with `unsafe { Conn::from_raw }` and a null test to call `abort_silent`, and two more called `lsquic_conn_abort_error` on the raw pointer. All four use the first `Conn`. `Conn::abort_error` takes a `&CStr`, so the two refusals pass `c""` where they passed null. `lsquic_conn_abort_error` (lsquic_conn.c:208) replaces a null reason with `""`, so the frame is the same. The argument is now the address of a constant instead of zero. No other crate names `lsquic_conn_abort_error`, so the declaration is private. #### 21. node:quic: the loop-driver callbacks take `&QuicEndpoint`; `lookup_cert` stays typed `Bun__nodeQuic__drainEndpoint` and `Bun__nodeQuic__processEndpoint` were `unsafe extern "C" fn(*mut c_void)` that cast the pointer through `unsafe fn from_driver_owner` and called `run_driver_pass(&self)`. They take `&QuicEndpoint`, and `from_driver_owner` goes. Each has one call site, node_quic_shim.c:601 and :615, both under `if (d->owner)`. `owner` is written only by `us_nq_loop_register` (:571), whose one caller, `link_loop_driver`, passes `from_ref(self)`. `unlink_loop_driver` takes the node off the list before the endpoint can be freed. A walk that is under way has read its successor already, so it can still call an endpoint that the pass before it closed and unlinked; `run_driver_pass` returns at once for it, as before. The reference lives exactly as long as the `&self` of `run_driver_pass` did. `lookup_cert` already held `this: &QuicEndpoint` and still ended in `unsafe { get_ssl_ctx(owner, local) }`, which turned the `void*` back into the same reference behind a second null test. The body of `get_ssl_ctx` moves to `fn server_ssl_ctx(&self)`, and both callbacks call it. `lookup_cert` was the one use of the form `ctx: *mut c_void as r: &T` of `lsquic_callback!`. The two arms for that form were otherwise only what the two short arms forwarded to, so the short arms hold the expansion themselves and the forwarding goes. No other use of the macro changes. #### 22. node:quic: `QuicEndpoint::constructor` reads its options before it creates the cell The constructor built the struct with defaults, moved it into its JS cell, and then stored each option it read through the raw payload pointer, in six `unsafe { (*raw).field.set(..) }` blocks. It now reads the options into locals first and puts them in the struct literal, so seven fields are stored once instead of twice. The two blocks that need the cell's address or the wrapper (the vtable, and `state`/`stats`) stay. What changes in order: the six property reads (`address`, `blockList`, `blockListPolicy`, `disableStatelessReset`, `statelessResetBurst`, `statelessResetRate`) keep their order and their conversions, but run before the cell and the boxed vtable are allocated instead of after. If one of them throws, the cell and the vtable are never allocated, and the default bind host and the `Strong` on the block list are freed on return instead of when the GC finalizes the abandoned cell. Nothing could reach that cell, and its `Drop` has nothing to release at that point. The one caller, `new Endpoint_(options)` in quic.ts, passes a fresh null-prototype object of data properties. #### 23. node:quic: `QuicSession::create` and `QuicStream::create` return the reference they make Both constructors form `&*payload` to finish setting the object up and then returned the raw pointer, so each caller went back through `unsafe { (*session).x() }` with "was just created" as the reason. They return `&'a Self`, and `'a` is the borrow of the owner. `QuicSession::create` takes `endpoint: &'a QuicEndpoint`, `QuicStream::create` takes `session: &'a QuicSession`, and `create_stream` borrows from `&self` and passes the reference on. `global` has a lifetime of its own. Both constructors took the owner as `*mut`, and all five call sites passed `from_ref(self).cast_mut()`. They pass `self`, and the cast stands at the one place in each constructor that stores the back-pointer. In every caller the owner is the caller's own `&self`, so no body can choose a longer `'a`. That is how `live_session` and `endpoint_ref` bound what they return. `apply_server_session_options` takes `&QuicSession`. Seven blocks go: in `on_new_conn`, `apply_server_session_options`, `connect` and `connect_verneg_probe`, two in `on_remote_stream` and one in `open_stream`. The large block in `connect` shrinks to the `lsquic_conn_set_ctx` call. The payload belongs to the GC, and the compiler does not check that it lives for `'a`. `create` roots the wrapper with `set_strong` before it returns, and only `teardown` (for a stream, also `release_close_root`) undoes that. After that the frame that has the reference holds the cell, which is what `JsWrapper::payload_ref` asks of its caller. Of the callers only `connect` tears the session down, and it returns right after. Nothing forms a `&mut` to either payload before the sweeper drops it. Where a pointer is stored (`sessions`, `pending_new_sessions`, `provisional`, `pending_verneg`, `streams`, the conn and stream ctx, the two back-pointers) it is `ptr::from_ref(x).cast_mut()`, the form these files use for `self`. Every use of those pointers forms `&T`. The operations and their order are unchanged. #### 24. node:quic: a session holds `Option<Conn>` and a stream holds `Option<Stream>` `QuicSession::conn` and `QuicStream::raw` were `Cell<*mut _>`. Nine places wrapped the field again with `unsafe { from_raw(..) }` before using it, among them the safe accessors `conn()` and `ls()`. Ten safe functions took a raw lsquic pointer that they or a later function trusted: `bind_conn`, `cache_sockaddrs`, `conn_peer_addr`, `on_remote_stream`, `bind_raw`, `QuicEndpoint::on_new_conn`, `defer_stream_abort`, `QuicSession::create`, `create_stream` and `QuicStream::create`. `bind_conn`, `on_remote_stream` and `bind_raw` wrapped their parameter, and `connect` wrapped at its end the pointer it had tested for null further up. The fields are `Cell<Option<Conn>>` and `Cell<Option<Stream>>`, and those ten functions take the handle or an `Option` of it. 13 blocks go and none is added. In `connect`, `from_raw` joins the block that calls `lsquic_engine_connect`. The block in `QuicEndpoint::on_new_conn` moves into the `on_new_conn` callback with its SAFETY comment. What is left to prove sits where lsquic hands a pointer over (the `on_new_conn` callback, `connect`, `on_new_stream`, `on_stream_read`). The fields are cleared where they were set to null. `DeferredAbort::ls` stays a raw pointer with its own `from_raw`, because what keeps it alive is a different argument. `defer_stream_abort` stores `ls.raw()` in it. In `connect` the local `Conn` is used after `apply_options`, as the raw pointer was. The SAFETY comment at `from_raw` says why no script runs in between when quic.ts calls. `Conn` and `Stream` hold a `NonNull` and are `repr(transparent)`, so the `Option` is one pointer with null as `None`. Each `is_null()` becomes `is_none()`, `is_some()` or `if let Some`, one for one. Six null tests go: - `connect` tested the pointer from `lsquic_engine_connect`, and again in `from_raw` at its end. - `bind_conn` matched on `from_raw` of its parameter. - `on_remote_stream` did the same. - `refresh_conn_stats` tested the field and then `from_raw` of it. - `bind_raw` had `from_raw(..).expect(..)`. Its panic path goes too. - The `on_new_conn` callback tested its pointer, and `QuicEndpoint::on_new_conn` tested it again in `from_raw`. `report_close` matches on the field instead of testing it in a guard and reading it again. 39 lines in lsquic_sys pass `self.0.as_ptr()`. `Conn` gains `raw()`, which `Stream` had. The other operations and their order are unchanged. #### 25. ast: `Binding::to_expr` takes `&mut C` and the method that wraps an identifier `to_expr` took a `*mut c_void` context and a `ToExprWrapper`: a back reference to the arena and a fn pointer to a trampoline that cast the context back to `&mut P` or `&mut ExportHoist`. All four call sites hold that `&mut` and the arena, so they pass both, and name the method (`Self::wrap_identifier_namespace`, `Self::wrap_identifier_hoisting`, `ExportHoist::wrap_identifier`) instead of a trampoline around it. `to_expr` is generic over the context and the function. The function is a zero-sized fn item, so nothing is passed for it and the call to it is direct. The cost is code size: `to_expr` is compiled once per method (four copies in bun_js_parser, one in bun_bundler) and not once in bun_ast. In a `--release` build bun_js_parser grows by 526 instructions in 343,000 and bun_bundler by 78, and bun_ast shrinks by 553. `ToExprWrapper` goes away, and with it the `expect` on its arena for every array and object pattern, the two fields of `P` that held it (32 bytes) and the code in `prepare_for_visit_pass` that filled them. One behaviour differs: `to_expr` on a `P` before `prepare_for_visit_pass` used to panic and now works. #### 26. js_parser: `P::init` returns the `&mut P` it wrote, and `init_p!` binds it `P::init(out: &mut MaybeUninit<P>, ..)` returned `Result<(), Error>`. `init_p!` called it, moved the slot into a drop guard and gave the guard to the caller. Each of the four users of the macro then took its `&mut P` out with `unsafe { __p.assume_init_mut() }`, and so did the fifth caller of `P::init`, the copy of the macro that runtime/test_runner/snapshot.rs writes out by hand because the macro is local to its crate. `init` cannot fail: its body has no `?`, `return`, `Err`, `unwrap`, `expect` or `panic!`. And `MaybeUninit::write` returns the reference the callers want. So the signature is ``` pub fn init<'o>(out: &'o mut MaybeUninit<Self>, ..) -> &'o mut Self ``` The guard holds that reference and the macro binds the caller's `p` to it. The slot and the guard are locals of the expansion, which the caller cannot name. The guard's closure runs `drop_in_place` on the reference, which is what `assume_init_drop` does. snapshot.rs does the same by hand: its guard type `InitializedParser` holds the reference, and its `Drop` runs `drop_in_place`. Five `unsafe` blocks go, and with the `?` the macro's hidden early return. Compiled code, in a `--release` build of bun_js_parser (default flags, no LTO), the parent of the commit before this one against this commit, so with the 32 bytes that that commit takes off `P`: - Moving the slot into the guard was a `memcpy` of the whole `P` (4344 bytes) in every caller. `_parse::<false>` and both `_scan_imports` copied it a second time to drop it. All eight copies are gone. - Stack frames: `_parse::<false>` 13240 to 11016 bytes, `_parse::<true>` 12856 to 10648, `_scan_imports` 9480 to 5176, `to_lazy_export_ast` 10008 to 5816. - Instructions: `_parse::<false>` 5133 to 5075, `_parse::<true>` 5553 to 5477, `_scan_imports::<false>` 895 to 830, `to_lazy_export_ast` 582 to 569, and 4 fewer in each of the four instances of `init`. - `init` returns its first argument, and LLVM sees that. In the five callers that were read (both `_parse`, both `_scan_imports`, `to_lazy_export_ast`) the code after the call ignores the returned register and addresses `p`'s fields off the stack pointer, as before, without a spilled pointer to `p`. - `P` is dropped as before: inline in `_scan_imports`, by a call to its drop glue in `_parse::<false>` and `to_lazy_export_ast`, and by a call to the guard's drop glue in `_parse::<true>`. No cost was found in the assembly. Run time was not measured. #### 27. js_printer: the three `write_reserved` callers use `print_slice` `WriterTrait::write_reserved` got a raw pointer to `n` spare bytes from `reserve`, copied the bytes there and committed them with `advance`. On `BufferWriter`, the only `WriterContext`, that is `Vec::reserve`, a copy and `set_len`, which is what `print_slice` does through `Vec::extend_from_slice`. The three callers use `print_slice`, two of them through `self.print` like the code around them, and `write_reserved`, `advance` and `advance_by` go away. `reserve` has two more callers, `print_ast` and `print_with_writer_and_platform`, which pre-size the buffer and ignore the pointer, so `reserve` and `reserve_next` stay and return nothing. `print_slice` is `#[inline]` down to the `Vec`. `reserve_next` and `advance_by` are not, and the printer is instantiated in other crates, so without LTO each `write_reserved` made two calls into bun_js_printer. #### 28. image, aot, mimalloc: imports that only write their out-parameters are `safe fn` Four imported functions took raw pointers to locals of the caller, and every call was an `unsafe` block. Typed as references, the signature says that the pointers are valid: - `JSC__JSValue__borrowBytesForOffThread` (bindings.cpp) writes `*out_ptr` and `*out_len`, or neither, and reads neither. - `tj3GetScalingFactors` (turbojpeg.c) stores a constant in `*n` and returns its static table. - `Bun__AOT__initializeOptions` (BunAOTCompile.cpp) calls `WTF::initialize()`, which is a `call_once`, and assigns `makeDefaultCompileOptions()`: zeroes, three integers, a double and two enumerators of enums generated from the schema that also generates the Rust type. One of the integers comes from `WTF::numberOfProcessorCores()`, which caches its answer in a plain `static int`; two first calls at once both store the same value, and safe Rust could already make them through `EngineOptions::new`. - `mi_process_info` (mimalloc, src/util/stats.c) tests each of its eight pointers for null before it writes through it, so they are `Option<&mut usize>`. `&mut T` and `Option<&mut T>` are passed like the pointers they replace. Image.rs also had its own declaration of `JSC__JSValue__unpinArrayBuffer`. bun_jsc declares that function `safe` and wraps it as `JSValue::unpin_array_buffer`, which other files of bun_runtime call. The two calls in Image.rs use the wrapper. That is the removal of a duplicate declaration, not a proof: the compiler checks nothing new at those two sites, and the C++ function still wants a value that was pinned. In Image.rs that holds by construction, because `Pin`'s field is private and a `Pin` is built only for a result of 2. #### 29. js_parser_jsc: `MacroContext::call` borrows `self.macros` and `self.bump` `call` turned the map entry and the arena into `*const Macro` and `*const Arena` "so the closure does not extend `&mut self`", and the closure made references of them again with two `unsafe` blocks. Both are borrows of one field each. What else `call` touches while they live is other fields (`self.javascript_object`, and before that `self.env` and `self.transform_options`), so the borrow checker accepts `&Macro` and `&Arena` as they are. `call` holds `&mut self` across the macro's JS as before, and `Runner::run` got the same two shared references. `disabled` and `vm` were copied out of the entry "so the borrow can be released". They are read through `macro_`, `vm` after the `disabled` test and not before it. #### 30. ast, sql_jsc: remove three `unsafe impl Send`/`Sync` that nothing needs `JsonTape` is five `Vec<_, TapeAlloc>` and a fieldless enum. `TapeAlloc` and what the element types hold (`StoreStr`, `StoreRef<ObjectJSON>`, `StoreRef<ArrayJSON>`) have `Send` and `Sync` impls of their own, none behind a `cfg`, so `JsonTape` is `Send + Sync` as an auto trait and its two impls said nothing more. sql_jsc's handle to the runtime's `SSLConfig` was declared `Send`. No code asks for that. The type is not named outside the crate, and none of its four holders looks at the bound: - `MySQLConnection` sits in `JSMySQLConnection` as a `JsCell<MySQLConnection>`, and `JsCell<T>` is `Send + Sync` for every `T`. - `PostgresSQLConnection` and `ConnectParams` hold an `Rc<SSLConfig>`, which is never `Send`. - `ConnectionCtorArgs` is `pub(crate)`, and nothing in the crate puts a bound on it. `Send` occurs nowhere else in the code of sql_jsc or sql, and the crate has no platform `cfg`. Without the impl the handle is `!Send`, like the `NonNull` in it. The entry for src/sql_jsc/jsc.rs leaves vm-thread-door.inventory.json. #### 31. css: two needless `unsafe` in values/ident.rs `IdentOrRef::as_ident` built a `&[u8]` with `from_raw_parts` only to turn it into the `*const [u8]` that `Ident` stores. `slice_from_raw_parts` builds that pointer and no reference. `arena_slice_newtype!` gave its three types an `unsafe fn as_slice` that calls the safe `v()`. It has no caller. #### 32. valkey, image, sql_jsc, ast: `&Self` guards, `&Blob`, `&Symbol`, a `const` array - `JSValkeyClient::on_valkey_connect` put `self.as_ctx_ptr()` in its drop guard and dereferenced it there. Both calls in the guard take `&self`, and `&Self` is `Copy`, so the guard holds `self`. The eight other drop guards in js_valkey.rs held `BackRef::new(self)` (or `this`) for the same kind of call. They hold the plain reference too, with no other change. - `BlobReadChain::start` made a `&mut Blob` of what `as_::<Blob>()` returned and only called `read_bytes_to_handler(&self, ..)` on it, which can run the handler, and through it JS, before it returns. `as_class_ref` is `as_` plus the shared dereference, so the same tests run. - `CachedStructure::build_from_columns` made its array of `MaybeUninit` with `MaybeUninit::uninit().assume_init()`. `[const { .. }; 70]` is the safe spelling. In an optimised build neither form writes anything; at opt-level 0 (the debug build) the old form copied the whole array with a `memcpy` and the new one fills it with a `memset`, 70 × 32 = 2240 bytes either way. - `Symbol::merge_contents_with` only reads `old`, so it takes `&Symbol`. No `unsafe` goes away with this one: its caller makes both references from raw pointers. #### 33. aot, md: three byte views go through `bun_core::slice_as_bytes` `OutputSlice::put_words` (`&[u32]`), the test helper `bytes_of` (`&[u64]`) and the markdown parser's copy of `current_block_lines` into `block_bytes` (`&[VerbatimLine]`) each built the `&[u8]` with `from_raw_parts`. `slice_as_bytes` is that same expression, `#[inline(always)]`, behind the bound `T: NoUninit`. `bytes_of` would only be another name for it, so it goes and its four call sites, all in tests, call `slice_as_bytes`. `VerbatimLine` (`#[repr(C)]`, three `u32`) gets the bound from `impl_no_uninit!`, which compiles only if rustc can prove that the type has no padding. #### 34. ast: `Rope<'a>` links its segments with `Option<&'a mut Rope<'a>>` `Rope::next` was a `*mut Rope`. `append` walked the chain through `StoreRef`, and `next_ref` turned the pointer into a `&'a Rope` of any lifetime with an `unsafe` block for `get_or_put_object`. Every segment is allocated in the arena that the head is allocated in (`ropealloc` in the INI parser; `bun init` has a single segment), so the link is a `&'a mut` into that arena. `append` walks a `&mut Option<..>` cursor, and `get_or_put_object` reads `rope.next.as_deref()`. `append` returned `Result<*mut Rope, AllocError>`, always `Ok`, and its three call sites dropped the pointer. It returns nothing. The null niche keeps the layout. In a `--release` build of bun_ast, `get_or_put_object` compiles to the same instructions and `append` loses the two that built its return value. #### 35. c-ares: `ResolveHandler` names its callback as an associated const `ResolveHandler::raw_callback` was an `unsafe extern "C" fn` in the trait. Its one impl, for `ResolveInfoRequest<T>`, forwarded its five arguments to `T::RAW_CALLBACK` from inside an `unsafe` block. The trait now has `const RAW_CALLBACK: unsafe extern "C" fn(..)`, the shape `CAresRecordType::RAW_CALLBACK` and `AresReply::PARSE` have, and the impl names `T::RAW_CALLBACK`. `Channel::resolve`, the only user, keeps its two `unsafe` blocks: it calls the constant for a bad name and hands it to `ares_query` otherwise. c-ares then calls the record type's thunk itself, and not a thunk that calls it. ### How did you verify your code works? - **Static checks on the final commits:** `cargo fmt --check`, `cargo clippy --workspace` (0 warnings), the source lints and `cargo check --workspace --release` on Linux, and `cargo check --workspace --release` for `x86_64-pc-windows-msvc`, `aarch64-apple-darwin` and `x86_64-unknown-freebsd`, because four changes touch code that Linux does not compile. - **Assembly:** the comparison above. - **After the assembly comparison and the tests had run,** three more commits went into #47895, which is under these, and a rebase brought in one more commit of `main` (#47900). Together they touch 17 files. This PR touches one of them, Image.rs, in other functions, and all 35 patches here stayed as they were. Then changes 2 and 17 were narrowed: six imports that they had marked `safe fn` stay unsafe imports and are called as on `main`. I compared the release assembly of all crates before and after that, and no function differs. The static checks ran last. - **30 test files on the final code,** on a debug build with ASAN, all started at once and each stopped after 50 seconds. - 24 files of Bun's own tests pass completely, 3,137 tests: `macro-test`, `css.test.ts`, `image.test.ts`, `image-adversarial`, `bun-cryptohasher`, `x509`, `node-https-checkServerIdentity`, `blocklist-gc`, `napi-value-ffi`, `cc.test.ts` and `ptr-byte-offset` of `bun:ffi`, `ini.test.ts`, `valkey-gc`, 4 files on IPC (`spawn.ipc`, `spawn-ipc-gc`, `bun-ipc-inherit`, `child_process_ipc`), 4 of `test/js/node/zlib` and 3 of `test/js/node/quic`. - 4 of Node's own tests for `node:quic` pass: `test-quic-blocklist`, `test-quic-connection-limits`, `test-quic-endpoint-busy` and `test-quic-internal-endpoint-options`. - `node-dns.test.js` passes 144 tests and fails 28, the same 28 as on a debug build without these commits. They need a DNS server, which I did not have. - `snapshot.test.ts` was stopped at 50 seconds, also when it ran alone. No test had failed by then. Its group `inline snapshots`, which runs the code that change 26 touches in snapshot.rs, passes when run by name: 35 tests, and 1 that the file skips. - **8 more files for the parser, the printer and their users,** run the same way. - 6 pass completely, 964 tests: `transpiler.test.js`, `decorators`, `decorator-metadata`, `es-decorators`, and `vitest-flavor` and `expect-deferred`, whose callbacks go through `ContextParameter::of`. - `bake/dev/esm.test.ts`, for the bundler's `to_expr`, passes 12 tests and fails 7, the same 7 as on a debug build without these commits. They need `happy-dom`, which I did not have. - `ffi.test.js` was stopped at 50 seconds. No test had failed by then. --- _Generated by [Claude Code](https://claude.ai/code)_ --------- Co-authored-by: Claude <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
32 separate changes, one commit each: 20 in the dev server (
src/runtime/bake) and 12 in the code behindBun.Transpiler,Bun.build,Bun.Image,Bun.Terminaland a few otherBun.*APIs. Commit 33 is a follow-up to change 2, for a finding of themordantcheck. They follow #47291, #47317, #47338 and #47382 and work the same way: signatures name the type and the lifetime that the value already has, hand-written pointer code becomes plain ownership and borrows that the compiler checks, and what was never needed is deleted.Together: 146 fewer
unsafeblocks and 3 fewerunsafe fns, +833/−1426 over 45 files. There is no newunsafe, no safe function becameunsafe, and nothing that is new hasunsafeinside. Where the compiled code changes lists every place where the program does different work.unsafeEntry::render_jsontakes noDevServerstart_async_bundlekeeps itsASTMemoryAllocatoron the stackget_or_put_route_bundleholds its index slot as&Cellon_file_updateborrows the watchlist's columnsprocess_file_listborrows the dependency acrossresolveFrameworkview instead of re-deriving itHmrSocketreads theDevServerthrough itsBackRefPerThread::inittakes theBackRefits caller holdsemit_*visualizer_message_if_neededfunctionsfinalize_bundleruns its cleanup after the body, not from guardsfinalize_bundle_innerborrows fields instead of going throughdev_ptrBunFrontendDevServerAgentwraps a reference to the debugger's slotRequestEnsureRouteBundledCtxis passeddevand borrows the requesttake_js_bundleis givenrootand the configuration hash keyCopyvaluescompute_arguments_for_framework_requesttakes&mut selfunsafe fnserver_transpilerandclient_transpilerareManuallyDrop, notMaybeUninitgenerate_html_payloadtakes the fields it usesinit_transpilerreturns the transpiler it wroteAssets::replace_pathbecomesDevServer::replace_asset_pathcreate_and_configure_transpilerreborrows the transpiler instead of going through a raw pointer*mut JSObjectstraight toJSPropertyIterator::initBun.Transpilerstops swappingTranspiler.arenaon every callJSBundleCompletionTaskbinds what it matched instead of matching again withunreachable_uncheckedNonNull<[u8]>from the null test that is already therecodecs::encodetakes the ICC profile asOption<&[u8]>Terminal::openowns the first ref as aRefPtrfrom the startBun.Transpiler's guards borrow the transpiler, andTranspiler::printtakes&selfJSTranspiler's constructor andDropuse the exclusive access they haveNonNullaNonNullinstead of going raw and back withnew_uncheckedunsafe fnunsafeblocks that a safe form replacesstart_async_bundlemakes the arena's&'staticreference onceThe commits can be reviewed one at a time.
Worth knowing before reading on:
&mut self(or went from a field back to theDevServerthat contains it) and was also handed a reference to something insideself, so one of the two became a raw pointer. The changes give such a function what it reads: the fields it works on (14, 17, 18, 20), aCopyindex instead of a reference (15, 16), ordevas a parameter instead of a stored pointer (13).unsafe:JsCell::as_mut(&mut self)(change 29), which isUnsafeCell::get_mut.TraceRoot(15), aCopyenum of two indices.DevRouteScan(17), which borrows three fields of the dev server for the router's scan, likeRouteScanof the production build.self:relative_path(root, ..)(14),BunFrontendDevServerAgent::of(vm)(11),GraphTraceState::new(18) andfinalize_bundle_inner(10). The first three methods forward to them.panic = "abort"is set for every profile, so "after the body" covers the same exits as a guard, as long as the body has no early return past it. In change 10 the body is a function of its own for that reason.ManuallyDropand not plain fields.HMAC_CTX_initas asafe fnthat takes&mut MaybeUninit<HMAC_CTX>. It only writes to its argument.Where the compiled code changes
I compared the release assembly (x86-64 Linux,
--emit=asm) of the six crates that this PR touches and ofbun_jsc, which calls a function whose signature changes, before and after, function by function, with names demangled and labels normalised. Commit 33 came after this comparison; it handsBundleV2::initthe same two addresses as before.bun_ast,bun_boringssl_sys,bun_bundler,bun_ptr,bun_watcherbun_astlosesrelease_ast_state(change 2).bun_jsc(not edited, it callsTranspiler::print_with_source_map)bun_runtimeDevServerhave other offsets (change 17), andEncodeOptionsis 10 bytes instead of 32 (change 26).Of the other 58 functions, 10 keep their number of instructions, 31 get shorter and 17 get longer. 13 functions exist only before and 10 only after: they were renamed, moved, inlined or removed. Over everything that differs, with the functions that exist on one side only, there are 316 fewer instructions, 22 fewer conditional branches, 14 fewer compares and 23 fewer calls.
Checks. In these functions the call sites of panic functions go from 858 to 856. Three functions have more of them than before:
on_framework_request_with_bundle(+2),ensure_route_is_bundled(+1) andserve_client_script(+1). In all three the number of checks on a path stays the same or goes down. They looked a route bundle up throughroute_bundle_ptr, whose one panic location the compiler could share between the inlined copies. Now each lookup is an index expression on a line of its own, with a panic location of its own.ref_countand the guard go.Drop for DevServergoes from 358 to 322 (with change 17).release_ast_stateare the drop of the local.start_async_bundlegoes from 691 to 660 (with change 17).on_file_updategoes from 308 to 302.flush_evictionsruns at the same point.finalize_bundle(5,951) and the drop code of its two guards (180 and 52) are nowfinalize_bundle_inner(5,908, with changes 1 and 11) and a wrapper, which the compiler inlines in the dispatch thunk (8 to 197).BunFrontendDevServerAgent::ofis a function of 19 instructions.make_array_for_server_components_patchis 1 longer.devin the context:on_defergoes from 424 to 417, andon_requestandhtml_bundle::Route::on_any_requestare 2 shorter.defer_requestpicks the list right before it links the node, not before it saves the request.rootarrives in two registers instead of being loaded fromself.relative_pathgoes from 287 to 294, and its callers get longer:take_js_bundle_to_listfrom 924 to 929 and its closure from 79 to 83,ErrorReportRequest::on_body+4,insert_failure(twice) andreport_pages_hidden_by_server_routes+1. Intake_js_bundle_to_listthe compiler duplicates the capacity test of oneVecappend. Onemalloc, one copy and onefreeper client bundle go, those of the entry point's path.trace_all_route_imports(369 to 357) to its callers.ensure_route_is_bundledgoes from 602 to 622: it has that test, and the call for an HTML route.serve_client_scriptgoes from 979 to 966 (with change 14), with 7 conditional branches fewer.on_framework_request_with_bundle, which hascompute_arguments_for_framework_requestinlined, goes from 1,572 to 1,599. A request whose arguments are all cached makes 1 bounds check onroute_bundlesinstead of 2. One that has to build the list of styles makes 2 instead of 3, with 2 tests of the route's kind as before. The function gets longer for two reasons: the second lookup has its own two panic calls, and the test in front of the drop of oneStrong, which the 8 error exits shared, is now duplicated at each of them (+7 compares, +7 branches in the listing, one per path as before).scan_initial_routesgoes from 81 to 91, because it writes three references to the stack. The three handler functions go from 35, 116 and 14 to 33, 119 and 14.initgoes from 1,712 to 1,711.on_html_request_with_bundlegoes from 693 to 696 and callstrace_importsitself (change 15).init_transpilergoes from 595 to 591.build_with_vmgoes from 2,539 to 2,544, with 2 compares and 2 branches fewer (with changes 6 and 8).Assets::replace_path(514) isDevServer::replace_asset_path(516). Its dispatch thunk goes from 75 to 74.Config::from_js+2 and theBun.Transpilerconstructor +1 (with change 29), moves.scangoes from 528 to 509,transformSyncfrom 607 to 585, the guard'sdropfrom 32 to 26.scanImportsgoes from 418 to 419 andTransformTask::runkeeps its 361. IntransformSyncthe compiler duplicates two tests and one call site ofmi_heap_destroy.Vec<OutputFile>is not moved out of the result and back, and the code that dropped it on the way goes.on_completegoes from 2,611 to 2,599.PipelineTask::rungoes from 6,783 to 6,753,Image::set_formatfrom 286 to 258,schedulefrom 573 to 564 andwritefrom 356 to 310. Seven more functions that copy the options are 2 to 4 shorter, and five keep their length. Inwrite, the compiler no longer folds "can this format be written" into the lookup of the file extension. It used to dropbmp,gif,tifandtifffrom the lookup, because they end like an unknown extension. Now it looks up all 11 extensions and tests the format afterwards: up to 4 compares more perwrite()that takes its format from the path. The source of that code is not touched.RefCount::deref, where each had the decrement and the destructor inline.fail_reader_start(12) is inlined.Terminal::opengoes from 427 to 411. On the failure paths the error value is freed before the ref is released, not after.Drop for JSTranspilergoes from 15 to 8 and the drop of itsBoxfrom 43 to 39.config.aborthook callsstd::process::abort.metadatagoes from 236 to 235.bun:internal-for-testing.Changes 3, 5, 6, 7, 8, 9, 12, 21 and 31 ask for the same operations in the same order. Where they show up at all, it is in registers and in the order of instructions:
process_file_listgoes from 1,213 to 1,212,index_failuresfrom 524 to 525, andHotReloadEvent::run,on_js_requestandstart_next_bundle_if_presentkeep their length.The comparison covers x86-64 Linux only.
The changes in detail
1. bake:
Entry::render_jsontakes noDevServerrender_jsontook&mut DevServerand never used it. To pass it, the.js.maphandler turned its borrow of the entry into a raw pointer and back. Without the parameter the handler borrows the entry.finalize_bundlerenders a temporaryEntry, a local, and had a guard that reached it through a raw pointer to clearfilesandpathsat the end of the block. The local is dropped at the same brace, which frees the same two fields, so the guard is gone, and with itEntry::deinit, which nothing else called.deinitasserted thatref_countis 0. Two writes of that field were only there for the assert: the 1 in the temporary entry, and the zeroing of every entry inDrop for DevServer. Nothing reads either, so both are gone. The loop indropkeeps its own assert.2. bake:
start_async_bundlekeeps itsASTMemoryAllocatoron the stackThe allocator was allocated in the bundle's arena and kept as a raw pointer. Because an arena value never runs
Drop, a hand-written guard released itsAstAllocStatethrough that pointer, and entering the scope and taking the state went through it too.Nothing needs the allocator after the function returns: the nodes live in the arena and in the
AstAllocState, and leaving the scope restores the thread-local pointer. It is a local now, like at the other callers ofborrowing, and its ownDropreleases the state on the error paths.release_ast_statehas no caller left and is removed.BundleV2::init'sallocparameter takes any lifetime, so it gets&heap.3. bake:
get_or_put_route_bundleholds its index slot as&CellThe slot that caches a route's bundle index is either a plain field of the framework route or the
Cellin theHTMLBundleRoute. To give both one type, the function turned them into*mut Option<Index>and read and wrote through that.Cell::from_mutgives the plain field the type the other one already has. The borrow ofself.routerlasts until the final store, and the code in between only touchesgraph_safety_lock,client_graphandroute_bundles.4. bake:
on_file_updateborrows the watchlist's columnsThe three columns came from one
Slice, whoseitems_mutborrows all of it, so each column was turned into a raw slice pointer and back. The two columns that are only read now come fromwatchlistitself, which leaves theSlicefree for thecountcolumn.flush_evictionsran in adefer!that reachedselfthrough a raw pointer. Nothing after it leaves the function early, so it is the last statement, still before the guard that submits the event.5. bake:
process_file_listborrows the dependency acrossresolveThe loop copied the dependency's
specifierout as a raw pointer so that no borrow ofdevwas held during the call to the resolver. The dependency lives indev.directory_watchersand the resolver indev.server_transpiler, two different fields, so a plain borrow of the dependency can stay alive for the call.6. bake: borrow the arena's
Frameworkview instead of re-deriving itarena.allocreturns&'a mut Framework. Both callers turned it into a raw pointer at once, becauseDrop for DevServerneeds one, and then dereferenced that pointer to get the&'a Frameworkfor the transpiler options. They now keep the reference, take the raw pointer from it and let the reference coerce to the shared one.7. bake:
HmrSocketreads theDevServerthrough itsBackRefon_openand the console-log message only read the dev server, but got it fromdev(), theunsafe fnthat hands out&mut DevServer. The field is aBackRef, which derefs to&DevServer. A copy of it in a local leavesselffree for the assignments in between.8. bake:
PerThread::inittakes theBackRefits caller holdsThe caller unwrapped
pt.vmto a raw pointer, andinitwrapped that pointer in aBackRefagain withfrom_raw_mut. It now takes theBackRef.9. bake: remove the two empty
emit_*visualizer_message_if_neededfunctionsThe body of
emit_memory_visualizer_message_if_neededis{}. Its only two callers, both insweep_weak_refs, went from theSourceMapStoreto theDevServerthat contains it, and so formed a&mut DevServerover the live&mut SourceMapStore, to call it.emit_visualizer_message_if_neededis just as empty, so it goes too, with its two calls.10. bake:
finalize_bundleruns its cleanup after the body, not from guardsTwo
defer!s at the top offinalize_bundledrained the bundle's requests and ranfinalize_bundle_cleanupon every exit. Each had to reachdev,bv2and the current bundle through raw pointers, because a closure that borrowed them would have locked them for the whole body.The body is now
finalize_bundle_inner, andfinalize_bundlecalls it, then the cleanup, then returns its result. The cleanup already matches ondev.current_bundle, so it drains the requests in theSomearm, beforebv2.deinit_without_freeing_arena()as before.11. bake:
finalize_bundle_innerborrows fields instead of going throughdev_ptrFour places reached a second field of the dev server through a raw copy of
dev, because the first borrow covered all of it. Two more kept the inspector agent as a raw pointer:route_bundle_ptrborrows all ofdev. Indexingdev.route_bundlesleavesdev.source_mapsfree forinvalidate_client_bundle.Entryfromput_or_increment_ref_countborrows onlydev.source_maps, sodev.client_graph.take_source_map(entry)compiles as it is.make_array_for_server_components_patchandsend_serialized_failurestook&mut selfbut only read. With&selfthey can be given slices ofdev.incremental_resultanddev.bundling_failures.inspector()ties the agent's lifetime to&self, although the agent lives in the VM's debugger.BunFrontendDevServerAgent::of(vm)holds the same body andinspector()forwards to it. Here the agent is borrowed from a copy ofdev.vm, so it can be kept across the loop that mutatesdev.The guard that takes
graph_safety_lockagain before the function returns is the only user ofdev_ptrleft, so the pointer is taken right above it.12. bake:
BunFrontendDevServerAgentwraps a reference to the debugger's slotThe agent was a
#[repr(transparent)]struct aroundErasedAgentSlot, andfrom_slotcast&ErasedAgentSlotto&Self. It is only ever a view of the slot, so it now holds&'a ErasedAgentSlot, isCopy, and its methods takeself.Option<BunFrontendDevServerAgent<'_>>is one nullable pointer, asOption<&BunFrontendDevServerAgent>was.13. bake:
RequestEnsureRouteBundledCtxis passeddevand borrows the requestThe context kept
devas a raw pointer, andensure_route_is_bundled, which holds&mut DevServeritself, called methods on the context that made a second&mut DevServerfrom it.on_deferandon_loadednow takedevas a parameter.on_deferalso picked the request list insidedevand handed it todefer_requestnext to&mut self.defer_requestnow takes theBundleQueueTypeand picks the list itself, right before it links the node.ReqOrSaved::Reqheld*mut Request, although both places that build a context have&mut Request. It holds that reference now, whichSavedRequestUnion::Stackandprepare_and_save_js_request_contexttake as it is.14. bake:
take_js_bundleis givenrootand the configuration hash keyTo print an initial response,
take_js_bundle_to_listwent from the client graph to theDevServerthat contains it and readroot, throughrelative_path, andconfiguration_hash_keyfrom there. The step to the container needs&mut self, which is the only reason that both functions took it, and because of itgenerate_client_bundlehad to copy the entry point's path out of the graph before the call.Both values are now fields of
TakeJSBundleOptionsClient, the functions take&self, and the entry point's path is borrowed from the graph. The body ofDevServer::relative_pathmoves to a free function that takesroot, and the method forwards to it.15. bake: trace a route and build its client bundle from
Copyvaluestrace_all_route_importsandgenerate_client_bundletook&mut selfand a&RouteBundlethat lives insideself, so every caller turned one of the two into a raw pointer. All that they read from the route bundle isCopy: the route index or the HTML file's index, and the source map id.trace_all_route_importsnow takes theRouteIndexof a framework route. For an HTML route it only forwarded toclient_graph.trace_imports, which the callers now call themselves, in thematchon the route's data that used to be inside.generate_client_bundletakes aTraceRoot, aCopyenum of the two indices, and the source map id.on_js_request_with_bundleis folded intoserve_client_script, its only caller, which has already looked the route bundle up. TheTraceRootis built only where the bundle has to be built, so a request for a cached bundle does what it did before, with one bounds check less.In
generate_client_bundlethe guard that resets the client graph ownsself, and the one that released the source map on failure is anif let Err.16. bake:
compute_arguments_for_framework_requesttakes&mut selfIt was an
unsafe fnover*mut Selfthat made its accesses in 17unsafeblocks, and was also handed a&mut Frameworkthat points intoself.route_bundles. Its last arm read that same route bundle again while the&mutwas live.What it touches are separate fields:
route_bundles,router,server_graphandroot. With&mut selfeach is borrowed on its own. The one call that needs all ofself,generate_css_js_array, now takes the route index, so no borrow is alive across it, and the result is stored through a fresh lookup.client_fileis read before the borrow of the router type ends, andglobalcomes from the caller, which has it.The function looks the route bundle up itself, so
on_framework_request_with_bundleno longer does.Data::frameworkhas no caller left.17. bake:
server_transpilerandclient_transpilerareManuallyDrop, notMaybeUninitThe three transpiler fields were
MaybeUninit, so every use afterinit()neededassume_init_*. Butinit()builds the whole struct underBox<MaybeUninit<DevServer>>and only callsassume_init()on it after the transpilers are written, so noDevServerwith an unwritten server or client transpiler ever exists.Those two are
ManuallyDropand not plain fields to keep the point where they are dropped: insideDrop for DevServer, before thebundler_framework_viewsthat their options borrow are dropped in place. Plain fields would be dropped afterdropreturns. The order indropis unchanged. Their four accessors are gone, and the callers use the fields.ssr_transpilerstaysMaybeUninit. Without a separate SSR graph it is a bitwise copy ofserver_transpilerand holds no value of its own. Its accessor, its uses underseparate_ssr_graphand the check indropare as they were.start_async_bundlegives the bundler a pointer to the slot withNonNull::from(..).cast(), which claims nothing about its contents.With that, the rest of
init()can borrow fields through theBoxinstead of throughdev_ptr:framework.resolvewith the two resolvers, and the loop over the router types.scan_initial_routespassed&mut *self_ptras the insertion handler next to&mut self.router. The handler only needsroot,server_graphandroute_lookup, soDevRouteScanborrows those three, likeRouteScanof the production build andJSFrameworkRouterScanCtx.18. bake:
generate_html_payloadtakes the fields it usesIt took
&mut selfand a&RouteBundleinsideself, soon_html_request_with_bundlekept bothselfand the route bundle as raw pointers and dereferenced them for each step.The function only uses the graph lock and the two graphs, none of which overlaps
route_bundles. It takes those three, in the wayinvalidate_client_bundletakes theSourceMapStore, and the caller holds one&mut RouteBundlefrom the lookup to the store of the response.GraphTraceState::newholds the body ofinit_graph_trace_state, which needs all of&self, and that method forwards to it.19. bake:
init_transpilerreturns the transpiler it wroteinit_transpilerfills aMaybeUninitslot.MaybeUninit::writegives it a&mut Transpilerto the result, which it dropped at the end, so the production build calledassume_init_muton each slot afterwards, for the SSR slot at every use. It now returns that reference.The production build keeps the three references. The SSR one is an
Option, which replaces the tests ofseparate_ssr_graphat its uses. The bundler wants&'a mut Transpiler<'a>, which borrows the slot for as long as the arena. That is fine here, because nothing uses the slots after that call, so the reference is passed as it is instead of through a raw pointer, and the other two becomeNonNull::from(..)without a null check.20. bake:
Assets::replace_pathbecomesDevServer::replace_asset_pathreplace_pathwent from theAssetsto theDevServerthat contains it, to readmagicandserverand to register the path inclient_graph. Both callers hold&mut DevServerwith nothing of it borrowed, so the function is a method ofDevServerthat starts by destructuring the three fields it works on. It takes the same arguments as before, andAssetsdoes not recover its parent from its own address any more.21. bundler:
create_and_configure_transpilerreborrows the transpiler instead of going through a raw pointerconfigure_bundlertakes&mut Transpiler<'a>with a borrow of its own, so passing the&'a mut Transpiler<'a>frombump.allocis an implicit reborrow that ends when the call returns. The transpiler lives inbump, not inself, so nothing conflicts with&mut self. The raw pointer and the twounsafe { &mut *tp }are gone; the reference is passed and then returned.22. api: pass the
*mut JSObjectstraight toJSPropertyIterator::initinittakesimpl IntoIterObject, which is implemented for*mut JSObject, and stores the raw pointer. Four callers inJSTranspilerandJSBundlerturned the pointer fromget_object()/get_own_object()into a&JSObjectwithunsafe { &*obj }only to hand it toinit, which turned it back. They pass the pointer as the other callers do.23. transpiler:
Bun.Transpilerstops swappingTranspiler.arenaon every callscan,transformSyncandscanImportspointed the long-livedTranspilerat their per-call arena, and a guard put the old one back. Thetransformtask did the same to its own bitwise copy of theTranspiler, with nothing to put back. Each took adetach_lifetime_refto'static.Nothing they call reads that field: its readers are
configure_defines,build_with_resolve_result_eagerandbuild_css_output, andparse,print,MacroContext::initandcache::JavaScript::scanreach none of them.parseandprinttake the arena as an argument with a lifetime of its own.The swap, the saved
prev_arenaand the four lifetime erasures are gone, and&arenais passed as it is.24. bundler:
JSBundleCompletionTaskbinds what it matched instead of matching again withunreachable_uncheckedFour places tested an enum with
matches!and then matched it a second time with_ => unsafe { unreachable_unchecked() }to get at the payload.do_compilationbindsbytesin the condition.do_compilationtook&mut self, soon_completehad to moveoutput_filesout ofself.resultand put it back. It only usesconfig,envandlog, so it takes those, and the caller lends it&mut build.output_filesnext to them: four disjoint fields.on_completehandlesPendingandErras arms of the one match. No loan ofthis.resultis live in theErrarm, soto_js_errorcan borrowthis.25. image: build the encoders'
NonNull<[u8]>from the null test that is already thereThe PNG and WebP encoders tested the codec's buffer for null and then made
Encoded.byteswithNonNull::new_unchecked, or with a&mut [u8]fromfrom_raw_parts_mutthat was only used for its pointer and length.NonNull::newis that same null test, andNonNull::slice_from_raw_parts(as inEncoded::from_owned) adds the length without claiming a reference.WebPChunkIteratoris written out as a literal of zeroes instead ofMaybeUninit::zeroed().assume_init(); it has no padding. TheBlobarm ofPipelineTask::thendrops theEncoded, as the base64 arm does, instead of calling itsfreeby hand:Encoded::dropmakes the same call.26. image:
codecs::encodetakes the ICC profile asOption<&[u8]>EncodeOptions.icc_profilewas anOption<NonNull<[u8]>>standing in for a borrow that only has to last for oneencode()call. It had one writer, right before that call, and one reader,unsafe { p.as_ref() }at the top ofencode(); no other code set it, so theis_none()test in front of the write was always true, and both system backends ignore it.The field is gone and the one caller passes
decoded.icc_profile.as_deref()as an argument.27. terminal:
Terminal::openowns the first ref as aRefPtrfrom the startopenboxed theTerminal, leaked it withheap::into_raw, worked throughunsafe { &*terminal }, calledderef_()by hand on each failure path and wrapped the pointer withunsafe { RefPtr::from_raw(..) }at the end.It returns a
RefPtr<Terminal>anyway, so it makes one withRefPtr::new, which is the same allocation. Every method it calls takes&self, which theRefPtrderefs to. Returning early drops theRefPtr, which releases the initial ref where the explicitderef_()did, so the one in the writer path and the one infail_reader_start(called only fromopen) are gone. The reader's own ref is still released by hand.28. transpiler:
Bun.Transpiler's guards borrow the transpiler, andTranspiler::printtakes&selfTransformTask::runkept a raw pointer toself.transpiler.macro_contextin a scopeguard while it went on usingself.transpiler. The guard holds&mut *self.transpilerand the body works through the guard; the task's other fields are disjoint places.TranspilerStateGuardkeptJsCell::as_ptr()and turned it into a&mutin itsDrop. It borrows theJsCellfor its own lifetime and restores the state withwith_mut, the way the callers set that state up.print*functions ofTranspileronly readself.options, so they take&self, andtransformSyncprints throughJsCell::get()instead of the unsafetranspiler_mut().29. transpiler:
JSTranspiler's constructor andDropuse the exclusive access they haveThe constructor owns a fresh
Box<JSTranspiler>and still went through the unsafeJsCell::get_mut(&self)forconfigandtranspiler.JsCellgetsas_mut(&mut self), which isUnsafeCell::get_mutand needs nounsafe, and the constructor borrows the two fields with it.Dropcalledclear_and_free()through the rawtranspiler.log. That pointer is&self.config.logwhenever aJSTranspileris dropped: the constructor sets it right after boxing, before anything can return, and the guard inscan,transformSyncandscanImportsputs it back on every exit. Dropping theconfigfield frees the same messages, so the call is gone.30. api: keep a
NonNullaNonNullinstead of going raw and back withnew_uncheckedThree callers had a non-null pointer by type, passed it on as
*mut T, and the receiver rebuilt theNonNullwithunsafe { NonNull::new_unchecked(..) }.CompletionDispatch::enqueue_task_concurrenttakesNonNull<ConcurrentTask>: the bundler holds one andTicket::postwants one.Subprocess::on_process_exittakesNonNull<Process>, made from the&mut Processthat the exit interface hands the thunk. Its only use isVirtualMachine::on_subprocess_exit, which takes aNonNull.CronJob::tick_settledgets theNonNullinside itsThisPtrthrough the existingBackRefconversions, ashtml_rewriterdoes forderef_nn.31. api: four functions exported to C++ are typed like the ones next to them
Bun__CryptoHasherExtern__destroytakesBox<CryptoHasher>, the type thatgetByNameandgetFromOtherhand out, instead of a raw pointer it fed toBox::from_raw.Box<T>has the ABI of a non-null pointer, and no C++ caller passes null:~JSHashanddigesttestm_zigHasherfirst, and aunique_ptronly runs its deleter on a non-null pointer.JSBundlerPlugin__onDefertakes&mut Loadand&JSGlobalObject, likeonLoadAsyncbelow it. C++ calls it only for a request thatholdsRequestfound.JSBundlerPlugin__addErrortakes&mut Plugin. It staysunsafefor its*mut c_voidcontext.BunObject_lazyPropCb_*thunks take&JSGlobalObjectand&JSObject, which is what they pass on.jsc_host_abi!still wantsunsafe fn.The first two are no longer
unsafe fn. None has a Rust caller.32. runtime: drop four small
unsafeblocks that a safe form replacesImage::metadata:js_thread_bytesreturned a slice of a temporaryArrayBufferview with the lifetime stretched to&self. Its one caller keeps the view in its own frame, so the slice borrows the view.HMAC_CTX_initis a write-only initialiser that reads nothing from its argument, so it is declaredsafe fn (&mut MaybeUninit<HMAC_CTX>), liketcgetattrinbun_sys, andHMAC::initandHMAC::copycall it without a block. The SHA*_Initdeclarations in the same file stay*mut; their comment says that what it objects to is&mut CTX.abortcrash-test hook callsstd::process::abort(), which islibc::abort()on Unix.33. bake:
start_async_bundlemakes the arena's&'staticreference onceBundleV2::inittakes the arena twice, asallocand asheap.start_async_bundlepassed&heapfor the first andunsafe { &*heap_ptr }for the second. The lintarg_named_like_other_paramreads a variable namedheapin theallocposition as an argument in the wrong place.The
&'staticreference is made once, in the block that was already there, and both parameters get it. It is the address that both got before.How did you verify your code works?
cargo fmt --check,cargo clippy --workspace(0 warnings), the source lints andcargo check --workspace --releaseon Linux.test/bake/, all oftest/js/bun/image/,test/js/bun/terminal/andtest/js/bun/cron/, 20 files oftest/js/bun/transpiler/andtest/bundler/transpiler/, 14 files oftest/js/bun/spawn/, and the files for the rest (bun-build-api, three of the bundler's plugin tests,plugins,resolve,import-meta,bun-cryptohasher, twonode:cryptofiles andrun-crash-handler).terminal.test.tsandspawn-maxbuf.test.ts, failed in 2 and 1 tests the first time, with eight files running at once. Alone they pass on both builds. Because change 27 touchesTerminal::open, I then ranterminal.test.tssix more times on each build, all twelve at once: 101 of 101 every time.spawn-pipe-leak.test.ts(3 of 3, their time limit of 30 seconds),bun-build-api.test.ts(1 of 91, its time limit of 180 seconds) andrun-crash-handler.test.ts(1 of 31, which expects a frame name that a debug build prints differently).plugins.test.tsandtest/bake/dev/ecosystem.test.ts(svelte), and 1 of the 108 tests ofresolve.test.ts(tsyringe).happy-dom. I ran them with version 20.0.11 and its script evaluation switched on, not with the version thattest/package.jsonpins.#[inline]attributes that an earlier form of changes 11, 14 and 18 had added, and added commit 33. The static checks ran again on the final commits.Generated by Claude Code