[wip] db/datastruct/btindex: fix OOM in Build() — stream nodes, use off-heap EF#21778
Open
AskAlexSharov wants to merge 6 commits into
Open
[wip] db/datastruct/btindex: fix OOM in Build() — stream nodes, use off-heap EF#21778AskAlexSharov wants to merge 6 commits into
AskAlexSharov wants to merge 6 commits into
Conversation
…ndex build NewEliasFano allocates a multi-GB contiguous bit array on the Go heap for large snapshots (mainnet: ~2B keys → ~3.5 GB). NewEliasFanoOffHeap backs the same arrays with a mmap'd temp file so the OS pages it, avoiding the heap spike.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the BtIndex on-disk format and build process to avoid mainnet-scale OOMs during B-tree index construction by (1) moving Elias–Fano (EF) build buffers off-heap and (2) streaming node records directly to the output instead of accumulating them in memory.
Changes:
- Switch EF construction in
BtIndexWriter.Build()toeliasfano32.NewEliasFanoOffHeap(...)(mmap-backed temp file) to eliminate large Go-heap spikes. - Change the index file layout to v1 (
[version][nodesCount][nodes...][EF]) so node entries can be streamed during ETL Load. - Update index open logic to support both v0 and v1 layouts, and tighten the test assertion to catch regressions where too many nodes are stored.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
db/datastruct/btindex/btree_index.go |
Writes v1 header + streams nodes during Build; reads v0/v1 formats on open; uses off-heap EF builder. |
db/datastruct/btindex/btree_index_test.go |
Adds an upper bound assertion for the number of cached nodes. |
db/datastruct/btindex/bps_tree.go |
Removes node-list encoding helper; updates node decoding helper to return bytes consumed. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
based on: #21777
.btfile format changeFixes two sources of OOM during BTree index build for mainnet-scale snapshots:
Primary (~3.5 GB heap spike):
NewEliasFano(keysWritten, maxOffset)allocates the full EF bit arrays on the Go heap. Replaced withNewEliasFanoOffHeapwhich backs them with a mmap'd temp file — OS pages it, Go heap is unaffected.Secondary (~576 MB):
nodes := make([]Node, 0, keysWritten/M)accumulated all M-th keys in memory before writing. Eliminated by swapping the file layout so nodes stream directly to the output file during ETL Load, with EF appended last.New file format (v1):
[0x01 version][M(8)][nodesCount(8)][nodes...][EF]Old file format (v0):
[EF][nodesCount(8)][nodes...]Mis now stored in the file header so the reader is self-contained —OpenBtreeIndexWithDecompressorreadsMfrom v1 files and ignores the caller-supplied parameter. TheBtIndex.M()accessor exposes it.