Repository navigation
Bun.write: write a string from its own buffer, not from a copy #47602
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
Open
robobun
wants to merge
4
commits into
main
Choose a base branch
from
robobun/8e876004/bun-write-lend-string-source
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
19a69a4
Bun.write: write a string from its own buffer, not from a copy
robobun 8c29b4b
Bun.write: take the lent string's bytes once, and keep the copy test …
robobun afe4bf1
Bun.write tests: read the pipe inside the fixture, by byte count
robobun 5fc9e58
Bun.write tests: end the pipe read when the write settles
robobun 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 |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| // A write of one string: Bun.write beside fs.promises.writeFile. It runs in Bun and in Node. | ||
| // | ||
| // in call until the call returns its promise | ||
| // stall the longest time that the JS thread is held, from the call to the end of the write | ||
| // total until the promise settles | ||
| // | ||
| // Each row is the median of ROUNDS writes. A "spaced" write comes 150 ms after the write | ||
| // before it and makes a new file. By then the allocator has given the memory of a freed copy | ||
| // of the string back to the OS. A "back-to-back" write follows the write before it at once | ||
| // and replaces the file of that write. | ||
| // | ||
| // On POSIX, Bun.write writes a string below 0.25 M characters on the JS thread. | ||
| // | ||
| // bun bench/snippets/write-string.mjs [sizes in M characters, default 0.125,0.5,4,32,128] | ||
| import { mkdtempSync, rmSync } from "node:fs"; | ||
| import { writeFile } from "node:fs/promises"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join } from "node:path"; | ||
| import { setTimeout as sleep } from "node:timers/promises"; | ||
|
|
||
| const M = 1024 * 1024; | ||
| const ROUNDS = 5; | ||
| const sizes = (process.argv[2] ?? "0.125,0.5,4,32,128").split(",").map(Number); | ||
|
|
||
| // A character is 1 byte of the file for ASCII, 2 for Latin-1 and 3 for UTF-16. | ||
| const kinds = { | ||
| "ASCII": length => Buffer.alloc(length, "a").toString("latin1"), | ||
| "Latin-1": length => Buffer.alloc(length, 0xe9).toString("latin1"), | ||
| "UTF-16": length => Buffer.alloc(length * 2, "\u4e2d", "utf16le").toString("utf16le"), | ||
| }; | ||
| const apis = { | ||
| ...(typeof Bun !== "undefined" && { "Bun.write": (path, string) => Bun.write(path, string) }), | ||
| "fs.promises.writeFile": (path, string) => writeFile(path, string), | ||
| }; | ||
|
|
||
| async function time(write, path, string) { | ||
| let pending = true; | ||
| let stall = 0; | ||
| let idle; | ||
| const stopped = new Promise(resolve => (idle = resolve)); | ||
| const start = performance.now(); | ||
| let last = start; | ||
| const promise = write(path, string); | ||
| const inCall = performance.now() - start; | ||
| setImmediate(function turn() { | ||
| const now = performance.now(); | ||
| stall = Math.max(stall, now - last); | ||
| last = now; | ||
| if (pending) setImmediate(turn); | ||
| else idle(); | ||
| }); | ||
| let total; | ||
| try { | ||
| await promise; | ||
| total = performance.now() - start; | ||
| } finally { | ||
| pending = false; | ||
| } | ||
| await stopped; | ||
| return { inCall, stall, total }; | ||
| } | ||
|
|
||
| function row(api, label, samples) { | ||
| const median = key => samples.map(sample => sample[key]).sort((a, b) => a - b)[samples.length >> 1]; | ||
| const ms = key => median(key).toFixed(3).padStart(8); | ||
| console.log(` ${api.padEnd(22)} ${label.padEnd(13)} ${ms("inCall")} ${ms("stall")} ${ms("total")}`); | ||
| } | ||
|
|
||
| const dir = mkdtempSync(join(tmpdir(), "bun-bench-write-string-")); | ||
| const runtime = | ||
| typeof Bun !== "undefined" ? `bun ${Bun.version} (${Bun.revision.slice(0, 9)})` : `node ${process.version}`; | ||
| console.log(`# ${runtime}, ms, median of ${ROUNDS}`); | ||
| try { | ||
| for (const size of sizes) { | ||
| for (const [kind, make] of Object.entries(kinds)) { | ||
| const string = make(size * M); | ||
| console.log( | ||
| `${size} M characters of ${kind}`.padEnd(39) + | ||
| "in call".padStart(9) + | ||
| "stall".padStart(9) + | ||
| "total".padStart(9), | ||
| ); | ||
| for (const [api, write] of Object.entries(apis)) { | ||
| const spaced = []; | ||
| for (let round = 0; round < ROUNDS; round++) { | ||
| const path = join(dir, `spaced-${round}`); | ||
| await sleep(150); | ||
| spaced.push(await time(write, path, string)); | ||
| rmSync(path); | ||
| } | ||
| row(api, "spaced", spaced); | ||
| const path = join(dir, "back-to-back"); | ||
| const backToBack = []; | ||
| await write(path, string); | ||
| for (let round = 0; round < ROUNDS; round++) backToBack.push(await time(write, path, string)); | ||
| rmSync(path); | ||
| row(api, "back-to-back", backToBack); | ||
| } | ||
| } | ||
| } | ||
| } finally { | ||
| rmSync(dir, { recursive: true, force: true }); | ||
| } | ||
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.