Repository navigation
perf(vindex/mpt): parallelize PrepareBatch sort and release checkpoint map buckets - #205
Merged
mhutchinson merged 1 commit intoDec 1, 2026
Conversation
patflynn
approved these changes
Nov 30, 2026
…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
force-pushed
the
perf/mpt-prepare-batch
branch
from
December 1, 2026 11:22
b71f70b to
c8737d4
Compare
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.
Why
During 50M-key Genesis Backfill coarse checkpoints on Let's Encrypt Sycamore 2026h1 (926M entries),
pprofshowed two bottlenecks on every coarse MPT commit:60+ son 1 of 64 cores):mpt.PrepareBatchran single-threadedslices.SortFunc(changes, torchmpt.KeyVal.CompareKey), spending ~75–77% of CPU in scalartorchmpt.Key.cmp(even though validated canonical keys are printable ASCII0x21..0x7Ewith no0x00bytes, whereKey.Compareequals AVX2bytes.Compare).5.83 GB+3.59 GB+):PrepareBatchconverted everystringkey to an individual heap[]byte(50M pointer objects, ~1.83 GB), whileCoordinator.indexToTarget'sflushCoarseCheckpointkeptmodifiedValueRootslive acrossSetKeyBatch+Snap+Persistand calledclear(modifiedValueRoots)(which zeros slots without releasing multi-GB map buckets), compounding swap pressure on 128 GiB hosts.What Changed
vindex/internal/mpt/mpt.go):32 * workerssamples) 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).wire.ValidateCanonicalKeyString, packs all key bytes into a single exact-sizedkeyArena []byte(3 retained heap allocations inBatchinstead ofN+2), releases transient staging slices before Phase 3, and sorts disjoint buckets in parallel usingbytes.Compare.torchmpt.KeyVal.CompareKeyacross boundary keys and worker counts (1, 4, 64, 128) inTestPrepareBatch_ParallelSampleSortMatchesTorchwoodOrder.vindex/internal/coordinator/coordinator.go):flushCoarseCheckpointprepares thempt.Batch, immediately reallocatesmodifiedValueRoots = make(...)so GC can reclaim peak map buckets prior to trie mutation, and commits in a singleCommitBatchAndPersistlock cycle instead of 3 separateSetKeyBatch+Snap+Persistcycles.vindex/cmd/vindexd/main.go):--coarse_checkpoint_intervaland--backfill_max_pending_keys(keeping the existing50,000,000defaults).Benchmarks (
linux/amd64, 24 vCPUs, Intel Xeon @ 2.20GHz)100kkeys, 60%"com."CT skew)main)BenchmarkPrepareBatch_ReversedDNS_100k-2497.39 ms/op15.91 ms/opallocs/op)100,003184(O(workers)constant)Batchslices vsN+2)Verification
go vet ./...CGO_ENABLED=0 go test ./vindex/...go test -race ./vindex/...