Skip to content

perf(sorted_set): compute union size from split drops instead of recounting#3829

Draft
bobzhang wants to merge 1 commit into
mainfrom
hongbo/perf-union-size
Draft

perf(sorted_set): compute union size from split drops instead of recounting#3829
bobzhang wants to merge 1 commit into
mainfrom
hongbo/perf-union-size

Conversation

@bobzhang

Copy link
Copy Markdown
Contributor

Partially addresses #3824 — resolves the TODO: optimize this. Avoid counting the size of the set in union.

Before

After building the merged tree, union recomputed the result size with a full each() traversal (an extra O(n+m) pass over the freshly built tree).

After

An element common to both sets is dropped exactly once, at the comp == 0 branch of split. split now returns its drop count (0 or 1) as a third tuple component, union accumulates them, and the size is arithmetic: |self| + |src| − dups. The recount pass is gone; split stays pure (no Ref cell).

Tests: the split whitebox tests now also pin drop counts for present/absent/empty splits, and new union tests assert exact sizes for overlapping, disjoint, and identical operands, cross-checked against an actual element count.

Not addressed here (per the issue): the defensive copy_tree of both operands, which is inherent to the mutable node design — rotations mutate in place, so subtree sharing would corrupt the sources.

Review

Reviewed by Codex CLI (codex-cli 0.144.1): "Approved; no findings. Inductively, split creates disjoint <, equal, and > partitions; each src node follows one recursion branch and can be dropped only at its unique matching self pivot. All self values are rejoined, while join* and rotations preserve every value. Thus dups = |self ∩ src| exactly."

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

Validation

  • moon check clean, moon fmt applied, no .mbti changes
  • moon test: 6716 passed, 0 failed (sorted_set 57/57)

🤖 Generated with Claude Code

…unting

Resolves the TODO in union: after merging, the result size was
recomputed by a full each() traversal of the merged tree. An element
common to both sets is dropped exactly once, at the `comp == 0` branch
of split, so split now reports its drop count (0 or 1) as a third
tuple component and union accumulates them:
size = |self| + |src| - dups. This removes the O(n+m) recount pass.

The split whitebox tests now also pin the drop count for present,
absent, and empty-tree splits, and new union tests assert exact sizes
for overlapping, disjoint, and identical operands, cross-checked
against an element count.

Partially addresses #3824 (the defensive copy_tree of both operands is
inherent to the mutable node design and stays).
Reviewed by Codex CLI (codex-cli 0.144.1): "Approved; no findings.
Inductively, split creates disjoint partitions; each src node can be
dropped only at its unique matching self pivot... Thus dups equals
the intersection size exactly."

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 5186

Coverage remained the same at 91.268%

Details

  • Coverage remained the same as the base build.
  • Patch coverage: 8 of 8 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: 17212
Covered Lines: 15709
Line Coverage: 91.27%
Coverage Strength: 198861.19 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