Skip to content

perf(vindex/mpt): parallelize PrepareBatch sort and release checkpoint map buckets - #205

Merged
mhutchinson merged 1 commit into
transparency-dev:mainfrom
mhutchinson:perf/mpt-prepare-batch
Dec 1, 2026
Merged

mhutchinson merged 1 commit into
transparency-dev:mainfrom
mhutchinson:perf/mpt-prepare-batch

Conversation

@mhutchinson

Copy link
Copy Markdown
Contributor

Why

During 50M-key Genesis Backfill coarse checkpoints on Let's Encrypt Sycamore 2026h1 (926M entries), pprof showed two bottlenecks on every coarse MPT commit:

  1. Single-core scalar sort (60+ s on 1 of 64 cores): mpt.PrepareBatch ran single-threaded slices.SortFunc(changes, torchmpt.KeyVal.CompareKey), spending ~75–77% of CPU in scalar torchmpt.Key.cmp (even though validated canonical keys are printable ASCII 0x21..0x7E with no 0x00 bytes, where Key.Compare equals AVX2 bytes.Compare).
  2. Heap spike & permanent bucket pinning (5.83 GB + 3.59 GB+): PrepareBatch converted every string key to an individual heap []byte (50M pointer objects, ~1.83 GB), while Coordinator.indexToTarget's flushCoarseCheckpoint kept modifiedValueRoots live across SetKeyBatch + Snap + Persist and called clear(modifiedValueRoots) (which zeros slots without releasing multi-GB map buckets), compounding swap pressure on 128 GiB hosts.

What Changed

  • Parallel oversampled sample-sort + contiguous key arena (vindex/internal/mpt/mpt.go):
    • Uses oversampled splitter selection (32 * workers samples) so reversed-DNS CT prefix skew (~60% "com.") balances evenly across cores (where naive 1–2 byte MSD radix bucketing stalls on a single core).
    • Validates canonical keys in parallel via wire.ValidateCanonicalKeyString, packs all key bytes into a single exact-sized keyArena []byte (3 retained heap allocations in Batch instead of N+2), releases transient staging slices before Phase 3, and sorts disjoint buckets in parallel using bytes.Compare.
    • Verified against torchmpt.KeyVal.CompareKey across boundary keys and worker counts (1, 4, 64, 128) in TestPrepareBatch_ParallelSampleSortMatchesTorchwoodOrder.
  • Release checkpoint map buckets before MPT commit (vindex/internal/coordinator/coordinator.go):
    • flushCoarseCheckpoint prepares the mpt.Batch, immediately reallocates modifiedValueRoots = make(...) so GC can reclaim peak map buckets prior to trie mutation, and commits in a single CommitBatchAndPersist lock cycle instead of 3 separate SetKeyBatch + Snap + Persist cycles.
  • Backfill tuning flags (vindex/cmd/vindexd/main.go):
    • Expose --coarse_checkpoint_interval and --backfill_max_pending_keys (keeping the existing 50,000,000 defaults).

Benchmarks (linux/amd64, 24 vCPUs, Intel Xeon @ 2.20GHz)

Benchmark (100k keys, 60% "com." CT skew) Before (main) After Delta
BenchmarkPrepareBatch_ReversedDNS_100k-24 97.39 ms/op 15.91 ms/op 6.12x faster
Allocations (allocs/op) 100,003 184 (O(workers) constant) -99.82% allocs (3 retained Batch slices vs N+2)

Verification

  • go vet ./...
  • CGO_ENABLED=0 go test ./vindex/...
  • go test -race ./vindex/...

@mhutchinson
mhutchinson requested a review from patflynn November 30, 2026 12:57
…t map buckets

During 50M-key Genesis Backfill checkpoints on Sycamore 2026h1, single-threaded
mpt.PrepareBatch consumed 60+ seconds on 1 core (75-77% in scalar torchmpt.Key.cmp)
and allocated 50M individual []byte key slices (~1.83 GB) while Coordinator held
modifiedValueRoots (~3.6-4.5 GB) live across SetKeyBatch+Snap+Persist and retained
its peak bucket array permanently via clear().

Changes:
- Parallelize mpt.PrepareBatch using oversampled sample-sort (immune to "com."
  reversed-DNS prefix skew) with AVX2 bytes.Compare (equivalent to Key.Compare on
  validated 0x21..0x7E canonical keys), parallel ASCII validation, and a single
  exact-sized contiguous keyArena ([]byte) rather than N per-key heap slices.
- In Coordinator.indexToTarget flushCoarseCheckpoint, prepare the mpt.Batch,
  immediately reallocate modifiedValueRoots (freeing multi-GB map buckets instead
  of pinning them via clear()), and commit via a single CommitBatchAndPersist call.
- Expose --coarse_checkpoint_interval and --backfill_max_pending_keys on vindexd.

Benchmarks (linux/amd64, 24 vCPUs, Intel Xeon @ 2.20GHz):
- BenchmarkPrepareBatch_ReversedDNS_100k-24 (60% "com." prefix skew):
  97,393,973 ns/op -> 15,366,610 ns/op (6.34x faster),
  12,804,360 B/op -> 13,457,543 B/op (+5.1% transient; 3 retained Batch allocs),
  100,003 allocs/op -> 182 allocs/op (-99.82%; O(workers) constant at 50M keys)
@mhutchinson
mhutchinson force-pushed the perf/mpt-prepare-batch branch from b71f70b to c8737d4 Compare December 1, 2026 11:22
@mhutchinson
mhutchinson merged commit a800525 into transparency-dev:main Dec 1, 2026
12 checks passed
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.

2 participants