-
Notifications
You must be signed in to change notification settings - Fork 4.7k
perf: use as_chunks* instead of chunks_exact* for static chunk sizes
#31415
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
7
commits into
main
Choose a base branch
from
claude/31414/as-chunks-static-size
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
7 commits
Select commit
Hold shift + click to select a range
537420c
perf: use as_chunks* instead of chunks_exact* for static chunk sizes
robobun 6ef0064
test: cover Latin-1 -> UTF-16 widening path in buffer-utf16
robobun af516ff
perf: use as_chunks in lockfile Meta/Bin validation; test field loader
robobun a440b84
test: use a single toEqual for the multi-chunk widening assertion
robobun 6c3d78a
test: use describe.each for the encoding matrix
robobun b4d3136
perf: use as_chunks in verify-baseline-static aarch64 scanner
robobun 8f154c7
perf: use as_chunks in copy_ascii_prefix SWAR loop
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| import { install_test_helpers } from "bun:internal-for-testing"; | ||
| import { expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe, tempDir } from "harness"; | ||
| import { copyFileSync, readFileSync, writeFileSync } from "node:fs"; | ||
| import { join } from "node:path"; | ||
|
|
||
| const { parseLockfile } = install_test_helpers; | ||
|
|
||
| // These tests exercise the raw-byte validation loops in the binary-lockfile | ||
| // loader (`Package::load_fields`), which iterate the `meta`/`bin` columns and | ||
| // reject out-of-range enum discriminants before the bytes are reinterpreted as | ||
| // `#[repr(u8)]` enums. A `file:` tarball dependency is used so the lockfile is | ||
| // produced and re-parsed entirely offline — no registry needed. `parseLockfile` | ||
| // drives `Lockfile::load_from_dir`, which runs `load_fields`. | ||
|
|
||
| const tarball = join(import.meta.dir, "bar-0.0.2.tgz"); | ||
|
|
||
| async function installFileDep(dir: string) { | ||
| copyFileSync(tarball, join(dir, "bar-0.0.2.tgz")); | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "install", "--no-progress"], | ||
| cwd: dir, | ||
| env: bunEnv, | ||
| stdout: "ignore", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stderr, exitCode] = await Promise.all([proc.stderr.text(), proc.exited]); | ||
| expect(stderr).not.toContain("error:"); | ||
| expect(exitCode).toBe(0); | ||
| } | ||
|
robobun marked this conversation as resolved.
|
||
|
|
||
| // Locate the `meta` and `bin` columns in a binary lockfile. Packages are stored | ||
| // SoA and the columns are written back-to-back in declaration order: name (8), | ||
| // name_hash (8), resolution (72 in format v3, 64 in v2), dependencies (8), | ||
| // resolutions (8), then meta (88 bytes/record) and bin (20 bytes/record). | ||
| const META_SIZE = 88; | ||
| const BIN_SIZE = 20; | ||
|
|
||
| function packageColumns(lockb: Buffer) { | ||
| const fmt = lockb.readUInt32LE(42); | ||
| const n = Number(lockb.readBigUInt64LE(86)); | ||
| const begin = Number(lockb.readBigUInt64LE(110)); | ||
| let resolutionSize: number; | ||
| switch (fmt) { | ||
| case 2: | ||
| resolutionSize = 64; | ||
| break; | ||
| case 3: | ||
| resolutionSize = 72; | ||
| break; | ||
| default: | ||
| // If the binary format changes again, fail loudly rather than silently | ||
| // corrupting the wrong byte and masking it as a field-validation test. | ||
| throw new Error(`unexpected bun.lockb format version ${fmt}`); | ||
| } | ||
| const metaStart = begin + n * (8 + 8 + resolutionSize + 8 + 8); | ||
| const binStart = metaStart + n * META_SIZE; | ||
| return { n, metaStart, binStart }; | ||
| } | ||
|
|
||
| test("valid binary lockfile round-trips through the field loader", async () => { | ||
| using dir = tempDir("lockb-field-valid", { | ||
| "package.json": JSON.stringify({ | ||
| name: "lockb-field-valid", | ||
| version: "1.0.0", | ||
| dependencies: { "dummy-package": "file:./bar-0.0.2.tgz" }, | ||
| }), | ||
| "bunfig.toml": "[install]\nsaveTextLockfile = false\n", | ||
| }); | ||
| await installFileDep(String(dir)); | ||
|
|
||
| const parsed = parseLockfile(String(dir)) as { packages?: Record<string, unknown> }; | ||
| // Loading succeeds, which means `load_fields` ran its meta/bin validation | ||
| // loops over the real column bytes without rejecting them. | ||
| expect(parsed.packages).toBeDefined(); | ||
| expect(Object.keys(parsed.packages!).length).toBe(2); | ||
| }); | ||
|
|
||
| test("rejects a binary lockfile whose meta.origin byte is out of range", async () => { | ||
| using dir = tempDir("lockb-field-origin", { | ||
| "package.json": JSON.stringify({ | ||
| name: "lockb-field-origin", | ||
| version: "1.0.0", | ||
| dependencies: { "dummy-package": "file:./bar-0.0.2.tgz" }, | ||
| }), | ||
| "bunfig.toml": "[install]\nsaveTextLockfile = false\n", | ||
| }); | ||
| await installFileDep(String(dir)); | ||
|
|
||
| const lockbPath = join(String(dir), "bun.lockb"); | ||
| const lockb = readFileSync(lockbPath); | ||
| const { n, metaStart } = packageColumns(lockb); | ||
|
|
||
| // `Meta.origin` is the first byte of each 88-byte record; the `Origin` enum | ||
| // is `#[repr(u8)]` with discriminants 0..=2, so 0x42 is out of range and the | ||
| // per-element check in the `meta` validation loop must reject it. | ||
| expect(n).toBeGreaterThan(0); | ||
| const originOffset = metaStart + (n - 1) * META_SIZE + 0; | ||
| expect(lockb[originOffset]).toBeLessThanOrEqual(2); // sanity: valid before | ||
| lockb[originOffset] = 0x42; | ||
| writeFileSync(lockbPath, lockb); | ||
|
|
||
| expect(() => parseLockfile(String(dir))).toThrow("Lockfile validation failed: invalid package meta"); | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| test("rejects a binary lockfile whose bin.tag byte is out of range", async () => { | ||
| using dir = tempDir("lockb-field-bin", { | ||
| "package.json": JSON.stringify({ | ||
| name: "lockb-field-bin", | ||
| version: "1.0.0", | ||
| dependencies: { "dummy-package": "file:./bar-0.0.2.tgz" }, | ||
| }), | ||
| "bunfig.toml": "[install]\nsaveTextLockfile = false\n", | ||
| }); | ||
| await installFileDep(String(dir)); | ||
|
|
||
| const lockbPath = join(String(dir), "bun.lockb"); | ||
| const lockb = readFileSync(lockbPath); | ||
| const { n, binStart } = packageColumns(lockb); | ||
|
|
||
| // `Bin.tag` is the first byte of each 20-byte record; the `Tag` enum is | ||
| // `#[repr(u8)]` with discriminants 0..=4, so 0x42 is out of range and the | ||
| // per-element check in the `bin` validation loop must reject it. | ||
| expect(n).toBeGreaterThan(0); | ||
| const tagOffset = binStart + (n - 1) * BIN_SIZE + 0; | ||
| expect(lockb[tagOffset]).toBeLessThanOrEqual(4); // sanity: valid before | ||
| lockb[tagOffset] = 0x42; | ||
| writeFileSync(lockbPath, lockb); | ||
|
|
||
| expect(() => parseLockfile(String(dir))).toThrow("Lockfile validation failed: invalid bin tag"); | ||
| }); | ||
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.