← Back to Pull requests

Speed up freelist reconstruction on open #1342

Closed
unknown closed this pull request without merging on Nov 24, 2026 16:42 UTC
View on GitHub
unknown unknown commented on Nov 17, 2026 14:29 UTC
Author

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() calls tx.recursivelyCheckBucket, which does three passes over each bucket from a single goroutine:

1. tx.forEachPage to mark the pages as reachable
2. recursivelyCheckPageKeyOrder
3. Bucket.ForEachBucket with a cursor to find the nested buckets

All 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 latency regardless 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 of recursivelyCheckBucket. The same properties are verified and the same helpers report the errors.

Tx.Check is 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_WILLNEED that 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.Open with NoFreelistSync on 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_recursivelyCheckBucketPages checks that the single pass reaches exactly the same pages as recursivelyCheckBucket on 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.

  • TestPrefetchRange covers the alignment of the prefetched ranges.
  • Diff: 396 lines (+297 / -99) across 4 files Reviewer: tjungblu Review duration: 10213m
    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
    0.48
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 48.2% (MEDIUM) · Higher = larger blast radius
    48.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 19% · Safe to merge
    19%
    Repository