Skip to content

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
Vivino:masterfrom
mohammedahmed18:perf/binary-search-instead-of-linear
Open

Perf: replace O(n) linear scans with O(log n) binary search across sorted element collections.#50
mohammedahmed18 wants to merge 7 commits into
Vivino:masterfrom
mohammedahmed18:perf/binary-search-instead-of-linear

Conversation

@mohammedahmed18

@mohammedahmed18 mohammedahmed18 commented Apr 30, 2026

Copy link
Copy Markdown

Profiling Findings

CPU profiling of the UpdateElements (heaviest write path) revealed:

  • runtime.memmove at 16.2% from slice copies in Insert/Delete
  • Elements.Insert at 14.5% flat / 27.5% cumulative -- O(n) linear scan
  • Elements.Delete at 11.5% flat / 20.5% cumulative -- O(n) linear scan
  • Elements.FindIdx at 7.2% -- O(n) linear scan for element ID lookup
  • GC pressure at 13.8% from frequent allocations

Optimizations

elements.go:

  1. Binary search in Elements.Insert -- O(n) to O(log n) for finding insertion point
  2. Binary search in Elements.FindScoreIdx -- O(n) to O(log n) for score lookup
  3. Binary search in Elements.FirstElementsWithScore -- O(n) to O(log n) per score
  4. Eliminated redundant scans in Elements.Add -- was: FindIdx (O(n)) + Delete (O(n)) + Insert (O(n)); now: single scan + binary insert
  5. Eliminated redundant scans in Elements.Update -- was: Delete (O(n)) + Insert (O(n)); now: combined delete + binary insert

segment.go:
6. Binary search in Segment.FilterIdx -- O(n) to O(log n) for finding element range within segment
7. Binary search in Segment.FilterScoresIdx -- O(n) to O(log n) for score range

Benchstat Results (statistically significant, p < 0.05)

Benchmark Baseline Optimized Improvement
UpdateElements/1K 405.8us 332.0us -18.18%
UpdateElements/10K 9.186ms 7.204ms -21.58%
GetElements/1K 114.0us 112.2us -1.54%
DeleteElements 2.267ms 2.211ms -2.49%
BulkIngestion/10K 7.071ms 7.017ms -0.78%
Geomean (all) 803.8us 770.5us -4.15%

Comment thread elements.go Outdated
Comment thread elements.go Outdated
@mohammedahmed18 mohammedahmed18 requested a review from volodimyr May 1, 2026 13:12
@mohammedahmed18

Copy link
Copy Markdown
Author

please take another look @volodimyr

@glaslos

glaslos commented May 5, 2026

Copy link
Copy Markdown
Contributor

@mohammedahmed18 do you have benchmarks you could add to the repo?

@mohammedahmed18

Copy link
Copy Markdown
Author

@glaslos added the benchmarking file, let me know if there are other changes you want me to make

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants