8000 perf(query): fix O(n²) aggregation grouping (issue #31) by adityamukho · Pull Request #71 · project-minigraf/minigraf · GitHub
[go: up one dir, main page]

Skip to content

perf(query): fix O(n²) aggregation grouping (issue #31) - #71

Merged
adityamukho merged 1 commit into
mainfrom
fix/issue-31-aggregation-grouping-perf
Apr 3, 2026
Merged

adityamukho merged 1 commit into
mainfrom
fix/issue-31-aggregation-grouping-perf

Conversation

@adityamukho
Copy link
Copy Markdown
Collaborator

Summary

  • Replace Vec + position() scan with BTreeMap<Vec<Value>, Vec<Binding>> for grouping
  • Complexity reduced from O(b × g) to O(b × log g) where b = bindings, g = groups
  • Value already implements Ord, so Vec<Value> works as BTreeMap key

Issue: groups.iter().position() is O(g) per binding, making total
complexity O(b × g) where b = bindings and g = groups.

Fix: Use BTreeMap<Vec<Value>, Vec<Binding>> keyed by group key.
- Complexity reduced from O(b × g) to O(b × log g)
- Value already implements Ord, so Vec<Value> works as BTreeMap key
@adityamukho
adityamukho merged commit 1fb87ae into main Apr 3, 2026
@adityamukho
adityamukho deleted the fix/issue-31-aggregation-grouping-perf branch April 3, 2026 03:32
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>
@adityamukho adityamukho mentioned this pull request Oct 25, 2026
10 tasks done
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.

1 participant

0