8000 perf(query): optimize CountDistinct/SumDistinct from O(n²) to O(n log n) by adityamukho · Pull Request #66 · project-minigraf/minigraf · GitHub
[go: up one dir, main page]

Skip to content

perf(query): optimize CountDistinct/SumDistinct from O(n²) to O(n log n) - #66

Merged
adityamukho merged 1 commit into
mainfrom
fix/issue-50-count-distinct-performance
Apr 3, 2026
Merged

adityamukho merged 1 commit into
mainfrom
fix/issue-50-count-distinct-performance

Conversation

@adityamukho
Copy link
Copy Markdown
Collaborator

Summary

  • Add Eq, PartialOrd, and Ord implementations to Value enum for sorting support
  • Replace Vec.contains O(n) deduplication with sorting + dedup O(n log n)
  • This significantly improves performance for large groups

Fixes #50 - CountDistinct/SumDistinct was O(n²) due to Vec.contains.

- Add Eq, PartialOrd, Ord implementations to Value enum for sorting
- Replace Vec.contains O(n) deduplication with sorting + dedup O(n log n)
- This improves performance significantly for large groups

Closes #50
@adityamukho
adityamukho merged commit 89464f8 into main Apr 3, 2026
10 checks passed
@adityamukho
adityamukho deleted the fix/issue-50-count-distinct-performance branch April 3, 2026 03:03
@codecov
codecov Bot commented Apr 3, 2026
Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 21 lines in your changes missing coverage. Please review.
✅ Project coverage is 84.38%. Comparing base (eee4ad7) to head (3948b6f).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
src/graph/types.rs 0.00% 21 Missing ⚠️

❌ Your patch status has failed because the patch coverage (0.00%) is below the target coverage (75.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main      #66      +/-   ##
==========================================
- Coverage   84.79%   84.38%   -0.41%     
==========================================
  Files          25       25              
  Lines        4353     4374      +21     
==========================================
  Hits         3691     3691              
- Misses        662      683      +21     
Files with missing lines Coverage Δ
src/graph/types.rs 61.19% <0.00%> (-27.94%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

adityamukho added a commit that referenced this pull request Apr 3, 2026
…or Value

Value::Ord's cross-variant fallthrough compared stack-frame pointer addresses,
which is non-deterministic across call contexts and violates Rust's Ord contract
(BTreeMap/BTreeSet require a consistent total order). This caused incorrect
aggregation grouping (PR #71) and wrong OR/OrJoin deduplication (PR #68).

Fix: use a stable u8 discriminant per variant for cross-variant ordering.

Also fix the O(n²) Vec::contains deduplication in count-distinct and sum-distinct
(issue #50, which PR #66 claimed to fix but did not): replace with BTreeSet,
which is now safe because Value::Ord is a correct total order.

Adds two regression tests:
- value_ord_cross_variant_is_antisymmetric: proves the old code violated
  antisymmetry (cmp(a,b) must equal reverse(cmp(b,a)))
- value_ord_cross_variant_is_stable: cross-variant ordering must depend only
  on the variant type, not the inner value

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CountDistinct/SumDistinct is O(n²)

1 participant

0