Perf: replace O(n) linear scans with O(log n) binary search across sorted element collections.#50
Open
mohammedahmed18 wants to merge 7 commits into
Conversation
…lement collections.
volodimyr
reviewed
May 1, 2026
volodimyr
reviewed
May 1, 2026
Author
|
please take another look @volodimyr |
Contributor
|
@mohammedahmed18 do you have benchmarks you could add to the repo? |
…eries, concurrent read/write, deletes, and multi-list operations
Author
|
@glaslos added the benchmarking file, let me know if there are other changes you want me to make |
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.
Profiling Findings
CPU profiling of the UpdateElements (heaviest write path) revealed:
runtime.memmoveat 16.2% from slice copies in Insert/DeleteElements.Insertat 14.5% flat / 27.5% cumulative -- O(n) linear scanElements.Deleteat 11.5% flat / 20.5% cumulative -- O(n) linear scanElements.FindIdxat 7.2% -- O(n) linear scan for element ID lookupOptimizations
elements.go:Elements.Insert-- O(n) to O(log n) for finding insertion pointElements.FindScoreIdx-- O(n) to O(log n) for score lookupElements.FirstElementsWithScore-- O(n) to O(log n) per scoreElements.Add-- was: FindIdx (O(n)) + Delete (O(n)) + Insert (O(n)); now: single scan + binary insertElements.Update-- was: Delete (O(n)) + Insert (O(n)); now: combined delete + binary insertsegment.go:6. Binary search in
Segment.FilterIdx-- O(n) to O(log n) for finding element range within segment7. Binary search in
Segment.FilterScoresIdx-- O(n) to O(log n) for score rangeBenchstat Results (statistically significant, p < 0.05)