Skip to content

gitindex: verify cat-file headers match the requested blobs - #1304

Merged
keegancsmith merged 2 commits into
mainfrom
k/catfile-verify-headers
Nov 26, 2026
Merged

keegancsmith merged 2 commits into
mainfrom
k/catfile-verify-headers

Conversation

@keegancsmith

@keegancsmith keegancsmith commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

catfileReader trusted the git cat-file --batch --buffer stream 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 (including missing and excluded entries, which are otherwise handled as before), that present objects are blobs, and that the byte after the content is \n. A mismatch returns an error.

One subtlety: the LF check moved out of Read and into Next() (or a Read to EOF). io.ReadFull drops an error returned alongside the final bytes, so checking eagerly at the end of Read would be swallowed and the next Next() 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 with ReadSlice instead of ReadBytes, removing the one allocation per blob the reader itself made.

Benchmarks on kubernetes are in a comment below. BenchmarkCatfileReader_Replay is new: it replays recorded cat-file output from memory so the reader's own per-entry cost isn't hidden behind git, and sortedBlobKeys now actually sorts so the files=N subsets are stable across runs.

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
keegancsmith force-pushed the k/catfile-verify-headers branch from 4494130 to ae5b0e2 Compare November 26, 2026 20:14
@keegancsmith
keegancsmith marked this pull request as ready for review November 26, 2026 20:14
@keegancsmith
keegancsmith merged commit e879480 into main Nov 26, 2026
8 checks passed
@keegancsmith
keegancsmith deleted the k/catfile-verify-headers branch November 26, 2026 20:16

Copy link
Copy Markdown
Member Author

Benchmarks for this PR: main (f03e5eb) vs the merged head (ae5b0e2). Run on a --depth=1 kubernetes clone (32,100 blobs, 289 MB) with 10 interleaved runs per side on a 2 vCPU machine. The baseline used the same benchmark file.

                                     │     main     │                PR                   │
                                     │   sec/op    │   sec/op     vs base               │
BlobRead_CatfileReader/files=1000-2    148.0m ± 6%   148.6m ± 4%       ~ (p=0.912 n=10)
BlobRead_CatfileReader/files=5000-2    414.1m ± 5%   398.6m ± 7%       ~ (p=0.436 n=10)
BlobRead_CatfileReader/files=32100-2    1.401 ± 7%    1.393 ± 5%       ~ (p=0.393 n=10)
CatfileReader_Replay-2                 50.71m ± 9%   53.64m ± 8%       ~ (p=0.218 n=10)
geomean                                256.8m        258.0m       +0.43%

                                     │     main      │                 PR                    │
                                     │     B/op     │     B/op      vs base                │
BlobRead_CatfileReader/files=1000-2    34.73Mi ± 0%   34.67Mi ± 0%   -0.17% (p=0.000 n=10)
BlobRead_CatfileReader/files=5000-2    89.90Mi ± 0%   89.59Mi ± 0%   -0.34% (p=0.000 n=10)
BlobRead_CatfileReader/files=32100-2   291.0Mi ± 0%   289.1Mi ± 0%   -0.67% (p=0.000 n=10)
CatfileReader_Replay-2                 3.189Mi ± 3%   1.248Mi ± 3%  -60.87% (p=0.000 n=10)
geomean                                41.26Mi        32.53Mi       -21.14%

                                     │      main       │                 PR                   │
                                     │   allocs/op    │  allocs/op   vs base                │
BlobRead_CatfileReader/files=1000-2       2.081k ± 0%   1.085k ± 0%  -47.86% (p=0.000 n=10)
BlobRead_CatfileReader/files=5000-2      10.080k ± 0%   5.085k ± 0%  -49.55% (p=0.000 n=10)
BlobRead_CatfileReader/files=32100-2      64.25k ± 0%   32.15k ± 0%  -49.96% (p=0.000 n=10)
CatfileReader_Replay-2                 32104.000 ± 0%    4.000 ± 0%  -99.99% (p=0.000 n=10)
geomean                                   14.42k         917.8       -93.64%

The header checks add no measurable time. Removing the per-header ReadBytes copy halves allocations on the end-to-end path: 64.25k → 32.15k on the full repo. The remaining allocations are the benchmark's own make([]byte, size) per blob. The replay benchmark measures only the reader, and it drops from 32,104 to 4 allocations per pass.

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