perf(chain): add secondary indexes to ProposalMap, eliminate O(n) linear scans#38
Open
simonzg wants to merge 2 commits into
Open
perf(chain): add secondary indexes to ProposalMap, eliminate O(n) linear scans#38simonzg wants to merge 2 commits into
simonzg wants to merge 2 commits into
Conversation
…ear scans GetOneByEscortQC, GetDraftByNum and PruneUpTo all performed full O(n) scans over the entire proposal map on every call. GetOneByEscortQC is on the critical consensus path: called for every incoming vote to match the QC to a known in-flight proposal. With N proposals in flight, each vote paid O(N) scan cost. Add two secondary indexes maintained alongside the primary map: byNum map[uint32][]*DraftBlock — index by block number byHeightRound map[uint64]*DraftBlock — index by height<<32|round GetOneByEscortQC: O(n) scan → O(1) map lookup GetDraftByNum: O(n) scan → O(1) map lookup PruneUpTo: O(n) scan → O(height-range) using byNum All three indexes are kept in sync in Add, PruneUpTo, and CleanAll. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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
ProposalMapstores in-flight HotStuff consensus proposals in a singlemap[Bytes32]*DraftBlockkeyed by block ID. Three query methods perform full O(n) linear scans of the entire map on every call:GetOneByEscortQCGetDraftByNumPruneUpToGetOneByEscortQCis the most critical: it runs on every vote received during consensus to match the vote's QC back to a known in-flight proposal. With N proposals in-flight (can be dozens during busy view changes), every single vote pays O(N) scan cost — a direct tax on consensus throughput.What Changed
Two secondary indexes are maintained alongside the primary
proposalsmap:GetOneByEscortQC:O(n)full scan →O(1)lookup inbyHeightRoundGetDraftByNum:O(n)full scan →O(1)lookup inbyNumPruneUpTo:O(n)scan+delete → iterates only the relevant height buckets inbyNumAll three indexes are kept in sync in
Add,PruneUpTo, andCleanAll. No external API changes.Expected Impact
PruneUpTo) no longer scans every in-flight proposal.