Skip to content

packfile: reuse Open's first-read buffer - #1046

Merged
tamirms merged 0 commit into
tamirms/packfile-byte-limitfrom
tamirms/packfile-reuse-buffers
Oct 5, 2026
Merged

tamirms merged 0 commit into
tamirms/packfile-byte-limitfrom
tamirms/packfile-reuse-buffers

Conversation

@tamirms

@tamirms tamirms commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Every request opens its packs, and each open reads the end of the file
into a new buffer of FirstRead bytes: 256 KiB by default and for
events.pack, 64 KiB for ledger packs. The Reader keeps a copy of the
tail or the second read's buffer, never the first read's, so that
buffer is garbage once the open finishes. It is most of what an open
allocates: 262 of the 267 KB that BenchmarkOpen's Open, TotalItems and
Close allocated at 12,600 records, with the file in the page cache.

The first read now goes into the scratch buffer of a workspace from
recordWorkspacePool, which already holds every read's buffer, and the
workspace goes back to the pool when the open finishes, whether or not
it succeeded. A scratch buffer smaller than the first read is replaced
by one of the right size, so the pool's buffers settle at the largest
read the process makes. At 12,600, 80,000 and 450,000 records the
benchmark now takes 35, 43 and 74 µs instead of 55, 67 and 94 (medians
of 10 interleaved runs), and allocates 5.5, 28 and 143 KB instead of
267, 288 and 402.

The pool needs no cap: the buffer's size is FirstRead(fileSize), which
the store sets and the file size bounds, so no file's contents can make
the pool keep a large buffer. What an open still allocates is mostly the
tail the Reader keeps, which is not pooled because AppData returns a
view into it that a caller may hold past Close.

🤖 Generated with Claude Code

@socket-security

Copy link
Copy Markdown

@tamirms tamirms changed the title packfile: reuse the open path's buffers packfile: reuse Open's first-read buffer Oct 5, 2026
@tamirms
tamirms changed the base branch from feature/full-history to tamirms/packfile-byte-limit October 5, 2026 10:55
@tamirms
tamirms added this pull request to stack #1081 October 5, 2026 10:56
@tamirms
tamirms removed this pull request from stack #1081 October 5, 2026 16:19
@tamirms
tamirms force-pushed the tamirms/packfile-reuse-buffers branch from 0872c4c to 1a38a80 Compare October 5, 2026 16:19
@tamirms
tamirms merged commit 1a38a80 into tamirms/packfile-byte-limit Oct 5, 2026
1 check passed
@tamirms
tamirms force-pushed the tamirms/packfile-byte-limit branch from b7a055b to 6ab0f8d Compare October 5, 2026 16:19
@tamirms
tamirms deleted the tamirms/packfile-reuse-buffers branch October 5, 2026 16:19
@tamirms
tamirms restored the tamirms/packfile-reuse-buffers branch October 5, 2026 16:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant