Skip to content

perf(sorted_set): merge-walk symmetric_difference without intermediate sets#3819

Draft
bobzhang wants to merge 1 commit into
mainfrom
hongbo/perf-symmetric-difference
Draft

perf(sorted_set): merge-walk symmetric_difference without intermediate sets#3819
bobzhang wants to merge 1 commit into
mainfrom
hongbo/perf-symmetric-difference

Conversation

@bobzhang

Copy link
Copy Markdown
Contributor

Resolves the TODO: Optimize this function to avoid creating two intermediate sets in sorted_set/set.mbt.

Before

symmetric_difference computed self.difference(other), other.difference(self), then unioned them — roughly five tree traversals, two full copy_trees inside union, plus union'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::iter is an explicit-stack ascending traversal; Iter::next is 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 through add.

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. iter is ascending in-order; next consumes one pull. add maintains size correctly."

Signed-off-by: Codex CLI codex@openai.com

Validation

  • moon check clean, moon fmt applied, no .mbti changes
  • moon test: 6717 passed, 0 failed (sorted_set 58/58, incl. 2 new)

🤖 Generated with Claude Code

…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>
@coveralls

Copy link
Copy Markdown
Collaborator

Coverage Report for CI Build 5181

Coverage increased (+0.009%) to 91.277%

Details

  • Coverage increased (+0.009%) from the base build.
  • Patch coverage: 20 of 20 lines across 1 file are fully covered (100%).
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 17230
Covered Lines: 15727
Line Coverage: 91.28%
Coverage Strength: 190864.03 hits per line

💛 - Coveralls

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.

2 participants