Avoid per-blob manifest existence queries during garbage collection marking

perfloop-oss/distribution · Excessive Round Trips

https://perfloop.ai/t/oss/case_77y50edwpn

Verdict

VERIFIED · settled 2026-11-24 · pull request opened as distribution/distribution#5100

What happened: The paired measurements met the required improvement.

Hypothesis

During garbage collection marking, the registry queries the storage backend to check whether every single referenced image layer and config blob is a nested manifest, even though garbage collection already enumerates every manifest revision in that repository. Almost every referenced digest in a container image is a layer or config blob, so each check becomes a sequential storage read that returns not found. On object stores like S3, GCS, or Azure, or large local filesystems, this adds one wasted sequential storage round trip per unique blob (and two round trips per child manifest). Real-world registry sizes, backend latencies, and production GC run frequency remain unmeasured.

In `registry/storage/garbagecollect.go`, `MarkAndSweep` enumerates each repository's `_manifests/revisions` namespace via `manifestEnumerator.Enumerate` (`linkedBlobStore.Enumerate`), which already reads each revision link and calls `linkedBlobStore.Stat` on every manifest revision in the repository. For every marked manifest, `markManifestReferences` (`registry/storage/garbagecollect.go:218-240`) fetches the manifest via `manifestService.Get` and iterates over `manifest.References()`. For each distinct reference not yet in `markSet` — `B` config and layer blobs plus `C` child manifests — line 231 calls `manifestService.Exists(ctx, descriptor.Digest)`. In `registry/root.go:78`, `GCCmd` constructs storage with no descriptor cache, so `Exists` goes straight to `linkedBlobStatter.Stat`, performing 1 sequential `driver.GetContent` (returning `PathNotFoundError`) for each of the `B` non-manifest blobs, and 2 sequential calls (`driver.GetContent` + `driver.Stat`) for each of the `C` child manifests: `B + 2*C` sequential storage round trips per repository.

Because garbage collection operates on a stopped or read-only registry (`docs/content/about/garbage-collection.md`), collecting the repository's manifest revision digests from `manifestEnumerator.Enumerate` into a per-repository map before invoking `markManifestReferences` collapses all `B + 2*C` canonical-digest `manifestService.Exists` round trips into O(1) in-memory map lookups. For any non-canonical digest algorithm not found in the map, `markManifestReferences` can fall back to `manifestService.Exists` (checking its error) without losing alias compatibility.

A search over both `inmemory` and `filesystem` drivers, `layerCounts` in `{1, 5, 20}`, and `RemoveUntagged` in `{false, true}` (`TestPerfloopGCMarkExistsRoundTripsSearch`) confirmed `L + 1` missed `/_manifests/revisions/` `GetContent` calls per image on baseline and 0 misses across all 12 cases with the experimental edit. The reduced deterministic replay (`go test -count=1 -v -run='^TestPerfloopGCMarkExistsReplay$' ./registry/storage`) fails on unchanged code with 4 non-manifest `/_manifests/revisions/` `GetContent` misses (3 layers + 1 config) in both `removeUntagged=false` (`totalGetContent=20`, `revGetContent=10`) and `removeUntagged=true` (`totalGetContent=23`, `revGetContent=11`), while verifying tagged index retention, untagged child manifest retention, and orphan blob deletion.

Local sandbox benchmarks on the filesystem driver (`go test -count=1 -run='^$' -bench='^BenchmarkGCMarkAndSweepFastFilesystem$' -benchmem -benchtime=20x ./registry/storage`, 20 OCI images with 10 layers and 1 config each = 220 non-manifest blobs) measured on baseline: 146.6 ms/op, 720 `GetContent`/op, 280 `/_manifests/revisions/` `GetContent`/op, 220 misses/op, 4,977,164 B/op, 44,475 allocs/op. With the experimental per-repository manifest-set edit, the same benchmark measured 93.5 ms/op (36.2% faster on local filesystem), 500 `GetContent`/op, 60 `/_manifests/revisions/` `GetContent`/op, 0 misses/op, 4,176,355 B/op, 36,322 allocs/op, and all existing GC tests in `./registry/storage` passed. These local filesystem measurements do not establish remote object-store latency or production GC workloads.

This mechanism is distinct from prior findings and open upstream PRs: `hyp_wz3snpp7bn` / https://github.com/distribution/distribution/pull/5083 propagate errors from `manifestService.Exists` without eliminating the per-blob existence round trips; `hyp_64s0t9bzah` and `hyp_vxtm5cb3vm` address cross-repo untagged pruning and quiet output; https://github.com/distribution/distribution/pull/4795 (two-pass online GC, parallel workers, per-repo tag prefetch) and https://github.com/distribution/distribution/pull/4884 (parallel repo marking) leave `markManifestReferences` and its per-reference `manifestService.Exists` loop completely unchanged; and https://github.com/distribution/distribution/pull/5089 only ignores missing `_layers`. GitHub PRs are the documented review venue; the shallow checkout leaves older merged history outside recent `git log` uninspected.

The Case proof target is to verify 0 `/_manifests/revisions/` `GetContent` misses for non-manifest blobs during `MarkAndSweep` across both `RemoveUntagged` modes, preserve tagged indexes with untagged children, dangling references, missing `_manifests` handling, and error propagation on enumeration/lookup failures, and benchmark `MarkAndSweep` on filesystem and counting/latency-injected drivers (test and benchmark placement are provisional).

Change to test: Collect each repository's enumerated manifest revision digests into an in-memory set before marking references, and use that set in markManifestReferences instead of calling manifestService.Exists for every referenced canonical digest (retaining a fallback lookup only if a non-canonical digest algorithm is referenced). Preserve exact mark-set membership, error propagation, and tagged/untagged index recursion.

Where it lives

perfloop-oss/distribution · registry/root.go

Evidence

GC filesystem images · 138 sample pairs

metric baseline candidate paired median change confidence range required result
ns/op 121298794 110555453 −8.7% (−10571783) −13321921 to −9183317 gain > 0 PASSED
peak_rss_KiB 58596 58506 +218 −2748 to +2788 no regression (noise ≤ 0.05 × |baseline median|) PASSED

GC OCI example · 70 sample pairs

metric baseline candidate paired median change confidence range required result
ns/op 2741621 2571477 −6.2% (−169046) −267984 to −26288 gain > 0 PASSED

The check fails before this change and passes with it: `On complete manifest enumeration, MarkAndSweep performs no non-manifest revision-link reads for an image retained by a tagged index, with either RemoveUntagged mode on the in-memory and filesystem drivers.`

GC empty index · 20 sample pairs

metric baseline candidate paired median change confidence range required result
allocs/op 379 381 +0.5% (+2) +2 to +2 loss ≤ 0.008 × |baseline median| PASSED
B/op 29065 28988 −78 −222 to +342 no regression (noise ≤ 0.05 × |baseline median|) PASSED

GC untagged repository · 20 sample pairs

metric baseline candidate paired median change confidence range required result
allocs/op 339 341 +0.6% (+2) +2 to +2 loss ≤ 0.009 × |baseline median| PASSED
B/op 23094 23055 −33 −266 to +188 no regression (noise ≤ 0.05 × |baseline median|) PASSED

GC absent alias · 20 sample pairs

metric baseline candidate paired median change confidence range required result
allocs/op 589 592 +0.5% (+3) +3 to +3 loss ≤ 0.007 × |baseline median| PASSED
B/op 48450 48497 +22 −253 to +463 no regression (noise ≤ 0.05 × |baseline median|) PASSED

GC interrupted enumeration · 20 sample pairs

metric baseline candidate paired median change confidence range required result
allocs/op 576 578 +0.3% (+2) +2 to +2 loss ≤ 0.0053 × |baseline median| PASSED
B/op 46865 46786 −71 −495 to +542 no regression (noise ≤ 0.05 × |baseline median|) PASSED

GC child-first indexes · 20 sample pairs

metric baseline candidate paired median change confidence range required result
allocs/op 553 556 +0.5% (+3) +3 to +3 loss ≤ 0.0073 × |baseline median| PASSED

GC resolved alias · 20 sample pairs

metric baseline candidate paired median change confidence range required result
allocs/op 904 909 +0.6% (+5) +3 to +5 loss ≤ 0.0067 × |baseline median| PASSED

GC parent-first alias · 20 sample pairs

metric baseline candidate paired median change confidence range required result
allocs/op 909 913 +0.4% (+4) +3 to +5 loss ≤ 0.0067 × |baseline median| PASSED

GC interrupted index reference · 20 sample pairs

metric baseline candidate paired median change confidence range required result
allocs/op 528 530 +0.4% (+2) +2 to +2 loss ≤ 0.0057 × |baseline median| PASSED

GC interrupted absent reference · 20 sample pairs

metric baseline candidate paired median change confidence range required result
allocs/op 402 404 +0.5% (+2) +2 to +2 loss ≤ 0.0075 × |baseline median| PASSED

GC empty reference · 21 sample pairs

metric baseline candidate paired median change confidence range required result
allocs/op 392 394 +0.5% (+2) +2 to +2 loss ≤ 0.0077 × |baseline median| PASSED

GC malformed reference · 21 sample pairs

metric baseline candidate paired median change confidence range required result
allocs/op 392 394 +0.5% (+2) +2 to +2 loss ≤ 0.0077 × |baseline median| PASSED

Checks: 4 of 4 passed. Verification: no defect found.

Timeline