mirror of
https://github.com/bitcoin/bitcoin
synced 2026-12-04 10:47:36 +00:00
Use O(log n) skiplist construction in CBlockIndex #37015
No reviewers
Labels
No labels
Android
Backport
Block storage
Brainstorming
Bug
Build system
CI failed
Consensus
Data corruption
Descriptors
Docs
DrahtBot Guix build requested
Feature
Fuzzing
GUI
Good First Review
IPC
Insufficient Review
Kernel
Linux/Unix
Mempool
Mining
Needs Backport (31.x)
Needs Backport (32.x)
Needs Benchmark
Needs Conceptual Review
Needs backport (30.x)
Needs rebase
Needs release note
Net
P2P
PSBT
Privacy
Private Broadcast
Questions and Help
RPC/REST/ZMQ
Refactoring
Resource usage
Review club
Scripts and tools
Settings
TX fees and policy
Tests
Tracking Issue
UTXO Db and Indexes
Up for grabs
Upstream
Utils/log/libs
Validation
Waiting for author
Wallet
Windows
good first issue
interfaces
macOS
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
bitcoin/bitcoin!37015
Loading…
Reference in a new issue
No description provided.
Delete branch "202611_skiplist"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
CBlockIndexcontains, in addition to thepprevpointer to it parent block, apskippointer that points further back added in #4420. This allowsGetAncestor(), and since #33515 alsoLastCommonAncestor()to locate ancestors and fork points in faster than linear time.Contrary to what I believed when #4420 was written, the scheme is not
\mathcal{O}(\log n)but\mathcal{O}(\log^2 n)(see https://github.com/bitcoin/bitcoin/pull/33661#discussion_r2783298594). However, true\mathcal{O}(\log n)schemes exist: Fable 5.2 found the paper "An applicative random-access stack" by Eugene W. Myers (1983)... and then improved it by a factor 1.5. That is what is implemented here.The idea is to lay out all the heights in a binary tree (not the block tree, this is just to arrange what heights point to which other ones):
For inner nodes,
pskippoints to the left child (blue) andpprevto the right child (orange). For leaf nodes,pprevpoints to one below (light blue), andpskippoints to the leftmost leaf descendant of it (light orange). Going from one height to another is simply followingpskipwhen it doesn't overshoot the target, andpprevotherwise.The scheme guarantees that any jump from a height
Htakes at most2 \\, \lfloor\log_2(H+1)\rfloor - 1steps. The proof is explained in the comments, and (a tighter version of) the formula tested in the unit tests.The number of pointers followed to go
dblocks back, over all heights from 500000 to 1499999 (before / after):dThe following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
External sites
Reviews
See the guideline and AI policy for information on the review process.
A summary of reviews will appear here.
Conflicts
Reviewers, this pull request conflicts with the following ones:
src/and treat them as errors by hebasto)If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.
Earlier concerns neither republished nor resolved: 1 suggestion. See the Run archive linked from the Review report for their assessments.
🔵 Suggestion · Immediate-parent lookups pay for unnecessary
TreeNodesetupLocatorEntriesand time-based sequence-lock checks pay forTreeNodesetup even when the answer ispprev; a direct return removes that work without changing results or step counts, though its wall-clock benefit is unmeasured.Location:
src/chain.cpp:321(head).🔵 Suggestion · Pointer counts leave the tracked-walk trade-off unmeasured
The supplied pointer counts do not measure
TreeNodesetup versus avoided cache misses, so they cannot establish whether the final walk improves on the simpler traversal for hot short and cold long lookups.Location:
src/chain.cpp:218(head).🔵 Suggestion · Step-count instrumentation arrives after the scheme replacement
Although the final tree tests the bounds, the series adds step counting only after replacing the old walk, separating the bounds from their assertions and preventing a same-counter comparison across the scheme change.
Location: src/chain.cpp:166 (commit).
Review coverage was incomplete.
Reviewed:
7ee87e299243· Review report · Previous reportspermalink
Can this just be a function?
#4220 in the description should probably be #4420
The reason for having it as a class is the last commit, which allows computing the pskip values everywhere along
GetAncestor's walk path before dereferencingpskip.But your description of the algorithm is much shorter than what I had. I'll use that in the 2nd commit, and turn it into a class later.
I have updated the initial commit to use this approach, plus added Python reference code to generate a SHA256 of the first 2M skip heights, which is verified in the unit tests.
The last two commits deviate from it somewhat, by using it just in the construction of the
TreeNodeclass, which can do more than just compute the skip height, but also "walk" the tree (updating its state to match followingpprevorpskipbut without dereferencing). It's a nontrivial change, and it's really only beneficial when the filter is not in CPU cache, as its goal is avoiding RAM lookup by trading them for a bit more CPU. I could be talked into dropping the last commit (and the preparation commit before it).