Repository navigation
Description
Rspack's native output bridge allocates a fresh Buffer for each asset, then Rsbuild's memfs complete-write path copies that Buffer again. The first copy separates compiler source from JavaScript ownership; the second can be avoided when the output caller explicitly transfers ownership.
This is a narrow follow-up to Rsbuild PR 8507, which correctly avoids unused buffer capacity while preserving ordinary copying semantics.
Reproduction
Source-only fixture, patches and recorded results. Download materialize.mjs, run node materialize.mjs ./memfs-ownership-repro, then follow the generated README. The fixture uses Node 24.19.0, Rsbuild 2.2.9 and Rspack 2.2.7. It runs the real native compiler with stock package bytes in both modes. Follow its README to install the pinned packages, then:
node --expose-gc repro.mjs --mode stock --output "$PWD/out-stock"
node --expose-gc repro.mjs --mode candidate --output "$PWD/out-candidate"Both completed successfully using an existing installation. Fresh dependency installation was not executed. The fixture rejects native overrides and earlier source overlays; the candidate transforms only the two JavaScript adapters in memory. No installed file changes or native rebuild are needed.
| 65,537-byte emitted asset | Stock | Candidate |
|---|---|---|
| Stored bytes match native input | Yes | Yes |
| Inode retains the same Buffer object | No | Yes |
| Inode retains the same backing allocation | No | Yes |
The fixture also checks binary/empty assets, replacement, executable output, compiler-source mutation isolation, retention after compiler close/GC, ordinary async/sync input reuse, read-result mutation, append, partial writes, truncation/regrowth and public wrappers. Candidate controls add concurrent transfers, callback reentrancy and error-marker cleanup. Both close two compilers and deregister instrumentation. These are correctness observations, not speed or memory measurements.
Separate application profile
This copy is visible in a large application's component HMR profile: the main-isolate Buffer.from stack under memfs setBuffer accounted for 1.124s during an edit. With the guarded candidate, that stack had zero samples, while read copies remained. The corresponding profiled write-to-visible interval was 12.558s → 11.552s. Two separate unprofiled edits were 12.074/13.037s → 11.095/11.557s.
This application uses additional local lazy/HMR patches, including a custom native build; the standalone fixture above uses stock native bytes. Extra rebuilds in restoration cycles prevent clean whole-cycle performance acceptance. These samples motivate the narrower copy optimization; they do not establish a stable speedup, GC or total-memory saving. No private application code or profile is included.
Ownership boundary
NodeFileSystem::write creates data.to_vec(), converts it to a Napi Buffer and never accesses that allocation again. Rsbuild's complete-write specialization currently calls setBuffer, which clones the input.
The experimental Rspack patch selects an optional owned-write capability inside the existing first-use memoization. The memfs patch marks only explicit transfers and adopts them only for complete writes to an empty/truncated file. Existing public wrappers disable the capability; ordinary writes still copy. Later wrappers retain the current memoization behavior.
Transferred buffers and all backing-memory aliases must never be reused, including after callbacks. Failure after adoption does not promise rollback. This is not a general optimization for arbitrary filesystem writes, and the native source copy remains.
Upstream API needed
The attached Rsbuild patch deliberately uses private fs-core _setBuf with an explicit type suppression. It is a pinned experiment, not merge-ready public API usage. The preferred upstream path is a supported fs-core method such as setOwnedBuffer, preserving its existing size/capacity/events, followed by the two adapter changes. The included ownership document separates that proposal from the executed candidate.
Rspack 9391 covers broader duplicated asset storage; Rspack PR 15782 proposed native disk emission for plain node:fs. Neither establishes this memfs ownership contract. The standalone fixture does not cover watch lifecycle, browser/application integration, native shutdown/abort, or a full upstream build.
Activity
chenjiahan commentedon Sep 28, 2026
Thanks for the feedback! Fixed in #8595