8000 perf(query): fix O(n²) contains_fact in semi-naive evaluation (issue #29) by adityamukho · Pull Request #72 · project-minigraf/minigraf · GitHub
[go: up one dir, main page]

Skip to content

perf(query): fix O(n²) contains_fact in semi-naive evaluation (issue #29) - #72

Merged
adityamukho merged 1 commit into
mainfrom
fix/issue-29-contains-fact-perf
Apr 3, 2026
Merged

adityamukho merged 1 commit into
mainfrom
fix/issue-29-contains-fact-perf

Conversation

@adityamukho
Copy link
Copy Markdown
Collaborator

Summary

  • Replace Vec + linear scan with BTreeSet for tracking seen facts
  • Use encode_value() to convert Value to canonical bytes as the key
  • Complexity reduced from O(n²) to O(n log n) where n = derived facts
  • Removed now-unused contains_fact method

Issue: contains_fact iterates through all seen facts for each new fact,
making total complexity O(n²) in semi-naive evaluation.

Fix: Use BTreeSet with canonical value encoding as key.
- Replace Vec<(Uuid, String, Value)> with BTreeSet<(Uuid, String, Vec<u8>)>
- Use encode_value() to convert Value to canonical bytes
- Complexity reduced from O(n²) to O(n log n)
- Removed now-unused contains_fact method
@adityamukho
adityamukho merged commit 7554548 into main Apr 3, 2026
@adityamukho
adityamukho deleted the fix/issue-29-contains-fact-perf branch April 3, 2026 03:35
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