Repository navigation
perf: avoid copying complete asset buffers #71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
| "finalhandler", | ||
| "fullhash", | ||
| "hono", | ||
| "jsonjoy", | ||
| "leadinghash", | ||
| "memfs", | ||
| "middie", | ||
|
|
||
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import { Node, Superblock } from "@jsonjoy.com/fs-core"; | ||
| import { Volume, createFsFromVolume } from "memfs"; | ||
|
|
||
| /** | ||
| * Rspack emits complete buffers and truncates files before replacing them. | ||
| * Retain those buffers to avoid copying and unused growth capacity. Callers | ||
| * must not mutate the input buffer or its aliases after a complete write. | ||
| * | ||
| * @this {Node} | ||
| * @param {Buffer} buffer input buffer | ||
| * @param {number} [offset] source offset | ||
| * @param {number} [length] number of bytes | ||
| * @param {number} [position] file offset | ||
| * @returns {number} written bytes | ||
| */ | ||
| function write(buffer, offset = 0, length = buffer.length, position = 0) { | ||
| if ( | ||
| this.getSize() === 0 && | ||
| position === 0 && | ||
| offset === 0 && | ||
| length === buffer.length && | ||
| length > 0 | ||
| ) { | ||
| // The pinned fs-core's private setter preserves metadata and change events. | ||
| /** @type {{ _setBuf(buffer: Buffer): void }} */ ( | ||
| /** @type {unknown} */ (this) | ||
| )._setBuf(buffer); | ||
| return length; | ||
| } | ||
|
|
||
| return Node.prototype.write.call(this, buffer, offset, length, position); | ||
| } | ||
|
|
||
| class OutputSuperblock extends Superblock { | ||
| /** | ||
| * @param {number} mode file mode | ||
| * @returns {Node} file node | ||
| */ | ||
| createNode(mode) { | ||
| const node = super.createNode(mode); | ||
| node.write = write; | ||
| return node; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @returns {import("memfs").IFs} memory file system | ||
| */ | ||
| function createMemoryFileSystem() { | ||
| return createFsFromVolume(new Volume(new OutputSuperblock())); | ||
| } | ||
|
|
||
| export default createMemoryFileSystem; | ||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import createMemoryFileSystem from "../../src/utils/memoryFileSystem.js"; | ||
|
|
||
| it("should write whole files, including overwrites", () => { | ||
| const fs = createMemoryFileSystem(); | ||
| for (const content of ["hello world", "hello", ""]) { | ||
| const buffer = Buffer.from(content); | ||
| fs.writeFileSync("/asset.js", buffer); | ||
|
|
||
| expect(fs.statSync("/asset.js").size).toBe(buffer.length); | ||
| expect(fs.readFileSync("/asset.js", "utf8")).toBe(content); | ||
| } | ||
| }); | ||
|
|
||
| it("should preserve partial writes, existing contents and appends", () => { | ||
| const fs = createMemoryFileSystem(); | ||
| const fd = fs.openSync("/asset.js", "w+"); | ||
| fs.writeSync(fd, Buffer.from("abcd"), 1, 2, 2); | ||
| fs.writeSync(fd, Buffer.from("X"), 0, 1, 0); | ||
| fs.closeSync(fd); | ||
| fs.appendFileSync("/asset.js", "!"); | ||
|
|
||
| expect(fs.readFileSync("/asset.js", "utf8")).toBe("X\0bc!"); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| /** | ||
| * @returns {import("memfs").IFs} memory file system | ||
| */ | ||
| declare function createMemoryFileSystem(): import("memfs").IFs; | ||
| export default createMemoryFileSystem; |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a plugin or other caller reuses or mutates a
Bufferafter a synchronous write returns (or after an asynchronous write's callback),_setBuf(buffer)leaves the file backed by that same mutable storage. A laterbuffer.fill()therefore silently changes the bytes served by the middleware without another filesystem write, metadata update, or watcher event; the previousNode.writepath copied the bytes. Because this hook applies to every complete write rather than only buffers whose ownership Rspack can guarantee, the filesystem must preserve the normal post-write snapshot semantics.Useful? React with 👍 / 👎.