perf(sorted_set): rewrite set algebra ops with split/join tree recursion#3333
Open
perf(sorted_set): rewrite set algebra ops with split/join tree recursion#3333
Conversation
Collaborator
Pull Request Test Coverage Report for Build 3147Details
💛 - Coveralls |
Rewrite intersection, difference, and symmetric_difference to use the split/join approach instead of naive contains+add loops. Before: intersection/difference were O(n * log(m)) where n is the size of one set and m is the other, using contains() + add() per element which each do O(log m) work including tree rebalancing. After: All three operations use split_member + join/join2 tree recursion, achieving O(n * log(m/n + 1)) which is optimal for set operations on balanced trees. Also fix union to track size through recursion instead of doing a full O(n) tree traversal to recount after construction. Helper functions added: - split_member: like split but also reports if pivot was found - join2: join two trees where all left < all right (no pivot) - split_min: extract minimum element from a tree - tree_count: count nodes in a tree Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Add tests that verify the .length() of results from intersection, difference, union, and symmetric_difference are correctly tracked through the split/join recursion. Also add tests for empty set edge cases in intersection and difference. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
d88aca3 to
21fbe71
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
intersection,difference, andsymmetric_differenceusing split/join tree recursion instead of naivecontains+addloops, improving performance from O(mlog(n)) to O(mlog(n/m+1)) for balanced inputs.unionto track size through recursion instead of performing an O(n) recount at the end.union,intersection,difference,symmetric_difference) to ensure the cachedsizefield stays consistent.Test plan
moon test -p moonbitlang/core/sorted_set— all 54 tests pass, including new size verification tests.🤖 Generated with Claude Code