Speed up freelist reconstruction on open #1342
All Vouch review confidence checks passed
Residual risk 0.19 = Change Risk (0.48) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.48 · 396 lines diff across 4 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.358
Shallow Review Depth
shallow_review_depth
0.289
Ai Authored
ai_authored
0.050
Real PR Conversation Timeline
1 comments
"Review active on #1342"
general
weight: 0.35
tjungblu
Opening a database without a synced freelist (
NoFreelistSync) rebuilds the freelist by scanning every page. On storage with a noticeable per-request latency this is very slow, and it gets worse when the database doesn't fit into memory. This PR addresses both, without changing what is verified during the scan.Related: #305
Problem
DB.freepages()callstx.recursivelyCheckBucket, which does three passes over each bucket from a single goroutine:1.
tx.forEachPageto mark the pages as reachable2.
recursivelyCheckPageKeyOrder3.
Bucket.ForEachBucketwith a cursor to find the nested bucketsAll of it reads through the mmap, so every page that isn't cached is a synchronous page fault. The storage only ever sees one outstanding 4 KiB read, and a cold open takes roughly
pages * read latencyregardless of how many IOPS the device could serve.If the database is larger than the available memory, the pages read by one pass have been evicted by the time the next pass needs them, so every page is read three times.
Changes
Single pass. The first commit adds
recursivelyCheckBucketPages, which verifies reachability and key order of a page when it visits it, and descends into nested buckets from the leaf page they are stored in.DB.freepages()uses it instead ofrecursivelyCheckBucket. The same properties are verified and the same helpers report the errors.Tx.Checkis not changed. It can run in a writable transaction, where nested buckets have to be looked up through the in-memory nodes, which is what the cursor in the third pass does. The freelist is always reconstructed in a fresh read-only transaction, where the pages are all there is.Prefetch. The second commit hints to the kernel with
MADV_WILLNEEDthat all children of a branch page are about to be read when the branch page is visited, so that it starts reading them at once. Adjacent children are covered by one call, and ranges are aligned to the OS page size. This is a no-op on Windows.The hint only covers pages the scan is about to read anyway, so it doesn't need the file to fit into memory, unlike
MAP_POPULATE.Benchmarks
bbolt.OpenwithNoFreelistSyncon a 488 MB database (~101k pages) with a cold page cache, on a network block device with ~2 ms per read. The memory limits are cgroup limits on the process opening the database.| | fits in memory | 200 MB limit | 64 MB limit | warm cache |
|---|---|---|---|---|
| main | 224.3s | 649.9s | | 0.13s |
| this PR | 5.1s | 5.4s | 5.5s | 0.12-0.16s |
main reads ~105k pages when the file fits and ~315k with the 200 MB limit. With this PR it is ~101k in all cases.
For comparison,
MmapFlags: syscall.MAP_POPULATE(measured on v1.4.3) takes 2.4s when the file fits in memory and 481.2s with the 200 MB limit.Testing
TestTx_recursivelyCheckBucketPageschecks that the single pass reaches exactly the same pages asrecursivelyCheckBucketon a database with multiple levels of branch pages, overflow pages, nested and inline buckets, for the default page size and for one smaller than an OS page.TestPrefetchRangecovers the alignment of the prefetched ranges.