Skip to content

perf(store): serialize the pack catalog once when compacting - #449

Merged
rmanibus merged 1 commit into
mainfrom
perf/compact-single-marshal
Aug 4, 2026
Merged

perf(store): serialize the pack catalog once when compacting#449
rmanibus merged 1 commit into
mainfrom
perf/compact-single-marshal

Conversation

@rmanibus

@rmanibus rmanibus commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Split sealShard (render a shard, return its key and bytes, no I/O) from writeShard (seal plus Put, returning the key it wrote)
  • CompactCatalog seals the catalog in place under the read lock it already holds, so it neither copies the catalog nor marshals it twice
  • An emptied catalog records no consolidated shard, instead of recording the hash of {} as an absorbed object that was never written

CompactCatalog marshalled the catalog twice and copied it once: writeShard
marshalled it to derive the key it wrote to, then mustMarshal marshalled the
identical map again solely to recompute that key. On a 50k-file repository each
marshal is a ~19 MB byte slice, both live alongside the duplicate map.

Closes #438

The correctness half

mustMarshal returned nil on error, so a failed marshal named
core.ComputeHash(nil) — the hash of no bytes. Compaction would then record a
consolidated shard that was never written and delete the shards it was meant to
replace.

The same key showed up on a path that needs no marshal failure at all. When every
entry has been deleted, writeShard returns early without writing, but the caller
still computed shardPrefix + ComputeHash(mustMarshal(merged)) and stored it in
mergedIndex. Verified against main at ee61760:

mergedIndex names index/packmap/44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a,
which does not exist: not found: index/packmap/44136fa...

That is sha256("{}"). TestCompactCatalog_EmptiedCatalogRemovesItsShards fails
on main and passes here.

Note on the lock

Sealing now happens under s.mu.RLock rather than after copying the catalog out.
Sealing does no I/O — the Put still happens after the unlock — so the lock is
held for a marshal rather than a round trip, and it is a read lock, so concurrent
readers are unaffected. A repository large enough for compaction to matter is
exactly the one whose catalog is expensive to duplicate.

Verification

env GOCACHE=/tmp/cloudstic-gocache go test -race -count=1 ./internal/storelayer/... ./internal/engine/... .
env GOCACHE=/tmp/cloudstic-gocache GOLANGCI_LINT_CACHE=/tmp/cloudstic-golangci-lint golangci-lint run ./internal/storelayer/...

Both pass; lint reports 0 issues.

CompactCatalog marshalled the whole catalog twice and copied it once:
writeShard marshalled it to derive the key it wrote to, and mustMarshal
then marshalled the identical map again purely to recompute that key.
On a 50k-file repository each marshal is a ~19 MB byte slice, and both
were live alongside a duplicate of the catalog map.

Split sealing from writing. sealShard renders a shard and returns the
key it belongs under, doing no I/O, so compaction can seal the catalog
in place under the read lock it already holds and write afterwards --
no copy, one marshal. writeShard is sealShard plus a Put, and returns
the key so no caller has to derive it a second time.

That also removes a way for the two derivations to disagree. mustMarshal
returned nil on error, so a failed marshal named the hash of no bytes:
compaction would record a consolidated shard that was never written and
delete the shards it was meant to replace. The same key appeared for an
emptied catalog, where writeShard wrote nothing but the caller still
recorded the hash of "{}" in mergedIndex -- an absorbed object that does
not exist. An empty catalog now records no consolidated shard, and still
removes the inputs, which is the deletion the compaction exists to make
durable.

Closes #438
@rmanibus rmanibus added area/store perf Performance, memory, and scaling work labels Aug 4, 2026
@coderabbitai

coderabbitai Bot commented Aug 4, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@rmanibus, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 11 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 5dcdb8f9-6031-474c-afd6-3d3182ac8763

📥 Commits

Reviewing files that changed from the base of the PR and between ee61760 and 7e8c6db.

📒 Files selected for processing (4)
  • internal/storelayer/pack.go
  • internal/storelayer/pack_compact_test.go
  • internal/storelayer/packshard.go
  • internal/storelayer/packshard_test.go

Comment @coderabbitai help to get the list of available commands.

@rmanibus rmanibus added the benchmark Run the memory-scaling benchmark on this PR label Aug 4, 2026
@codecov

codecov Bot commented Aug 4, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 74.07407% with 7 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
internal/storelayer/packshard.go 76.92% 4 Missing and 2 partials ⚠️
internal/storelayer/pack.go 0.00% 0 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@rmanibus
rmanibus merged commit 1ca7e68 into main Aug 4, 2026
25 checks passed
@rmanibus
rmanibus deleted the perf/compact-single-marshal branch August 4, 2026 14:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/store benchmark Run the memory-scaling benchmark on this PR perf Performance, memory, and scaling work

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Serialize the pack catalog once during compaction

1 participant