perf(#34): hoist find_next_pending_index to eliminate O(N²) gas complexity in get_all_milestones_view#67
Open
d3vobed wants to merge 1 commit into
Conversation
…(N²) gas complexity in get_all_milestones_view get_all_milestones_view called views::get_milestone_by_index per milestone, and get_milestone_by_index invoked find_next_pending_index which linearly scanned every milestone to find the first non-released one. With N milestones this produced N + (N × N) ≈ N² storage reads. Fix: call find_next_pending_index once at the top of get_all_milestones_view and inline the MilestoneView construction in the loop, reusing the precomputed value for is_next_pending. Behavioural semantics of is_next_pending are unchanged — identical comparison, identical result. All 142 tests pass, including existing snapshot fixtures. Closes OrbitChainLabs#34
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.
Summary
get_all_milestones_viewcalledviews::get_milestone_by_indexfor each milestone, andget_milestone_by_indexinvokedfind_next_pending_indexwhich linearly scanned every milestone to find the first non-released one. With N milestones this produced N + (N × N) ≈ N² storage reads.Change
Hoist
find_next_pending_index— compute it once at the top ofget_all_milestones_viewand inline theMilestoneViewconstruction, reusing the precomputed value foris_next_pending:for i in 0..N→get_milestone_by_index→find_next_pending_index(full scan)next_pending = find_next_pending_index(env)(1 scan)for i in 0..N→next_pending == i(O(1))The same fix can be applied to any other view function that iterates milestones and per-row calls
find_next_pending_index.Verification
is_next_pendingsemantics are identical (same comparison, same value)get_all_milestones)MilestoneViewshape are untouchedCloses #34