Repository navigation
gitindex: verify cat-file headers match the requested blobs - #1304
Merged
Merged
Conversation
catfileReader trusted the cat-file stream to line up with the IDs it sent: it only parsed the size from each header and discarded the byte after the content. If the stream ever desynced, file contents would be indexed under the wrong names with no error. Each header must now echo the next requested ID, present objects must be blobs, and the delimiter after the content must be a LF. The LF is checked by Next (or a Read to EOF) rather than eagerly at the end of Read, because io.ReadFull drops an error returned with the final bytes. The ID check decodes the echoed hex into a stack array so it adds no allocation per entry. The benchmark keys are now actually sorted so the files=N subsets are stable across runs, and a replay benchmark measures the reader's per-entry work without git in the loop.
ReadBytes allocated a copy of every header, one allocation per blob in the hot indexing loop. Next only inspects the header before returning, so a view into the bufio buffer from ReadSlice is enough.
keegancsmith
force-pushed
the
k/catfile-verify-headers
branch
from
November 26, 2026 20:14
4494130 to
ae5b0e2
Compare
keegancsmith
marked this pull request as ready for review
November 26, 2026 20:14
Member
Author
|
Benchmarks for this PR: The header checks add no measurable time. Removing the per-header |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
catfileReadertrusted thegit cat-file --batch --bufferstream to line up with the IDs it wrote: it parsed only the size from each header and discarded whatever byte followed the content. If the stream ever desynced, file contents would be indexed under the wrong names with no error.Next()now checks that each header echoes the next requested ID (includingmissingandexcludedentries, which are otherwise handled as before), that present objects areblobs, and that the byte after the content is\n. A mismatch returns an error.One subtlety: the LF check moved out of
Readand intoNext()(or aReadto EOF).io.ReadFulldrops an error returned alongside the final bytes, so checking eagerly at the end ofReadwould be swallowed and the nextNext()would carry on. The ID check decodes the echoed hex into a stack array, so it adds no per-entry allocation. A second commit reads headers withReadSliceinstead ofReadBytes, removing the one allocation per blob the reader itself made.Benchmarks on kubernetes are in a comment below.
BenchmarkCatfileReader_Replayis new: it replays recorded cat-file output from memory so the reader's own per-entry cost isn't hidden behind git, andsortedBlobKeysnow actually sorts so thefiles=Nsubsets are stable across runs.