Repository navigation
Free a thread-pool job's own copy of a large input when the pool thread is done with it #47668
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
2
commits into
main
Choose a base branch
from
robobun/7cdeade8/free-job-input-copy-on-pool
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 1 commit
Commits
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,89 @@ | ||
| // @runtime node, bun | ||
| // fs.promises.writeFile of data that the write job cannot read in place. bun reads an ASCII | ||
| // string or a buffer of the caller in place. It has its own copy of the other sources here. | ||
| // The rows of mitata write one file at a time. The bursts start every write before they await | ||
| // one. The chains await each write before the next one. Each burst and each chain runs in a new | ||
| // process, because a process that has run one has its memory to use again. | ||
| import { Buffer } from "node:buffer"; | ||
| import { spawnSync } from "node:child_process"; | ||
| import { mkdtempSync, rmSync } from "node:fs"; | ||
| import { writeFile } from "node:fs/promises"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join } from "node:path"; | ||
| import process from "node:process"; | ||
| import { fileURLToPath } from "node:url"; | ||
|
|
||
| const KB = 1024; | ||
| const MB = 1024 * KB; | ||
| // Each gives a source that is `size` bytes in the file. | ||
| const sources = { | ||
| "ASCII": size => Buffer.alloc(size, "a").toString("latin1"), | ||
| "Latin-1": size => Buffer.alloc(size / 2, 0xe9).toString("latin1"), | ||
| "UTF-16": size => Buffer.alloc((size / 3) * 2, "\u4e2d", "utf16le").toString("utf16le"), | ||
| "base64": size => Buffer.alloc(size, "a").toString("base64"), | ||
| "resizable view": size => new Uint8Array(new ArrayBuffer(size, { maxByteLength: 2 * size })).fill(0x61), | ||
| }; | ||
| const encoding = kind => (kind === "base64" ? "base64" : "utf8"); | ||
| const label = size => (size >= MB ? size / MB + " MB" : size / KB + " KB"); | ||
|
|
||
| const dir = mkdtempSync(join(tmpdir(), "bun-bench-write-file-copied-data-")); | ||
| try { | ||
| const mode = process.argv[2]; | ||
| if (mode === "burst" || mode === "chain") { | ||
| const [kind, count, size] = [process.argv[3], Number(process.argv[4]), Number(process.argv[5])]; | ||
| const source = sources[kind](size); | ||
| const paths = Array.from({ length: count }, (_, i) => join(dir, "out" + i)); | ||
| const faults = process.resourceUsage().minorPageFault; | ||
| const start = performance.now(); | ||
| if (mode === "burst") await Promise.all(paths.map(path => writeFile(path, source, encoding(kind)))); | ||
| else for (let i = 0; i < count; i++) await writeFile(paths[0], source, encoding(kind)); | ||
| const ms = performance.now() - start; | ||
| const usage = process.resourceUsage(); | ||
| console.log( | ||
| `${count} x ${label(size)} ${kind} ${mode === "burst" ? "at once" : "in a chain"}`.padEnd(38) + | ||
| `${ms.toFixed(0)} ms`.padStart(9) + | ||
| `${usage.minorPageFault - faults} minor faults`.padStart(22) + | ||
| `${(usage.maxRSS / KB).toFixed(0)} MB peak RSS`.padStart(18), | ||
| ); | ||
| } else { | ||
| // First, while this process is small: a child starts with the peak RSS of its parent. | ||
| if (!process.env.BENCHMARK_RUNNER) { | ||
| for (const [mode, kind, count, size] of [ | ||
| ["burst", "ASCII", 300, MB], | ||
| ["burst", "UTF-16", 1500, 300 * KB], | ||
| ...["Latin-1", "UTF-16", "base64", "resizable view"].map(kind => ["burst", kind, 300, MB]), | ||
| ["chain", "UTF-16", 16, 32 * MB], | ||
| ["chain", "resizable view", 16, 32 * MB], | ||
| ]) { | ||
| spawnSync(process.execPath, [fileURLToPath(import.meta.url), mode, kind, count, size].map(String), { | ||
| stdio: "inherit", | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| const { bench, run } = await import("../runner.mjs"); | ||
| const path = join(dir, "out"); | ||
| for (const [kind, size] of [ | ||
| ["ASCII", 12], | ||
| ["UTF-16", 12], | ||
| ...Object.keys(sources).flatMap(kind => [16 * KB, 300 * KB, MB, 16 * MB].map(size => [kind, size])), | ||
| ]) { | ||
| const source = size === 12 ? (kind === "ASCII" ? "short string" : "short \u4e2d\u6587") : sources[kind](size); | ||
| bench(`${size === 12 ? "12 B" : label(size)} ${kind}`, async () => { | ||
| await writeFile(path, source, encoding(kind)); | ||
| }); | ||
| } | ||
| const chain = sources["UTF-16"](MB); | ||
| const paths = Array.from({ length: 16 }, (_, i) => join(dir, "chain" + i)); | ||
| bench("16 chains of 8 x 1 MB UTF-16", async () => { | ||
| await Promise.all( | ||
| paths.map(async path => { | ||
| for (let i = 0; i < 8; i++) await writeFile(path, chain); | ||
| }), | ||
| ); | ||
| }); | ||
| await run(); | ||
| } | ||
| } 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
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
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.