8000 perf(query): fix O(n²) Or/OrJoin deduplication (issue #35) by adityamukho · Pull Request #68 · project-minigraf/minigraf · GitHub
[go: up one dir, main page]

Skip to content

perf(query): fix O(n²) Or/OrJoin deduplication (issue #35) - #68

Merged
adityamukho merged 1 commit into
mainfrom
fix/issue-35-or-dedup-perf
Apr 3, 2026
Merged

adityamukho merged 1 commit into
mainfrom
fix/issue-35-or-dedup-perf

Conversation

@adityamukho
Copy link
Copy Markdown
Collaborator

Summary

  • Replace Vec::contains() with BTreeSet for deduplication in Or/OrJoin clauses
  • Complexity reduced from O(B × R²) to O(B × R × log R) where B = branches, R = total results
  • Uses Vec<(String, Value)> as BTreeSet key since Value already implements Ord

Issue: result.contains(&b) is O(n) for each branch result, making total
complexity O(B × R²) where B = branches and R = total results.

Fix: Use BTreeSet<Vec<(String, Value)>> for O(log n) deduplication.
- Vec<(String, Value)> implements Ord (lexicographically), so it works as BTreeSet key
- Complexity reduced from O(B × R²) to O(B × R × log R)
@adityamukho
adityamukho merged commit b430885 into main Apr 3, 2026
2 checks passed
@adityamukho
adityamukho deleted the fix/issue-35-or-dedup-perf branch April 3, 2026 03:17
@codecov
codecov Bot commented Apr 3, 2026
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.34%. Comparing base (11ebb1f) to head (a6e7747).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main      #68      +/-   ##
==========================================
+ Coverage   84.21%   84.34%   +0.12%     
==========================================
  Files          25       25              
  Lines        4390     4394       +4     
==========================================
+ Hits         3697     3706       +9     
+ Misses        693      688       -5     
Files with missing lines Coverage Δ
src/query/datalog/executor.rs 93.25% <100.00%> (+0.04%) ⬆️

... and 1 file with indirect coverage changes

🚀 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.

1 participant

0