perf(sorted_set): merge-walk symmetric_difference without intermediate sets#3819
Draft
bobzhang wants to merge 1 commit into
Draft
perf(sorted_set): merge-walk symmetric_difference without intermediate sets#3819bobzhang wants to merge 1 commit into
bobzhang wants to merge 1 commit into
Conversation
…e sets Resolves the TODO: symmetric_difference built two difference sets and then a union — five tree traversals, two full tree copies inside union, and a full recount of the result size. Replace it with a single in-order merge walk over both iterators, adding elements present in exactly one set. Set size stays correct because the result is built through `add`. Adds interleaved and proper-subset test cases to exercise both advance paths of the merge walk. Reviewed by Codex CLI (codex-cli 0.144.1): "No findings. Approved. The merge correctly emits the smaller head, skips equal heads, and drains either tail... add maintains size correctly." Signed-off-by: Codex CLI <codex@openai.com> Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Collaborator
Coverage Report for CI Build 5181Coverage increased (+0.009%) to 91.277%Details
Uncovered ChangesNo uncovered changes found. Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
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.
Resolves the
TODO: Optimize this function to avoid creating two intermediate setsinsorted_set/set.mbt.Before
symmetric_differencecomputedself.difference(other),other.difference(self), thenunioned them — roughly five tree traversals, two fullcopy_trees insideunion, plusunion's O(n+m) size-recount pass, all to produce one result set.After
A single in-order merge walk over both sets' pull iterators (
SortedSet::iteris an explicit-stack ascending traversal;Iter::nextis a consuming pull), adding elements present in exactly one set. No intermediate sets, no tree copies, one pass. Size bookkeeping stays correct because the result is built throughadd.Adds two test cases (interleaved elements, proper subset) to exercise both advance paths of the merge walk, alongside the existing basic/empty/identical coverage.
Review
Reviewed by Codex CLI (codex-cli 0.144.1): "No findings. Approved. The merge correctly emits the smaller head, skips equal heads, and drains either tail, preserving exact symmetric-difference semantics.
iteris ascending in-order;nextconsumes one pull.addmaintains size correctly."Signed-off-by: Codex CLI codex@openai.com
Validation
moon checkclean,moon fmtapplied, no.mbtichangesmoon test: 6717 passed, 0 failed (sorted_set 58/58, incl. 2 new)🤖 Generated with Claude Code