feat: Equaler/Disjointer/Subsetter predicate fast paths #minor - #43
Merged
Conversation
Same optional-interface pattern as the set-algebra and Maxer/Minner hooks: package-level Equal, Disjoint, and Subset consult their first operand (Superset is Subset with the operands swapped, so it consults its second operand's Subsetter) and fall back to the generic path on decline. - SortedSet: short-circuiting two-pointer scans of the two sorted slices; Equal is a direct slices.Equal - BitSet: word-wise compare / AND / AND NOT with early exit, correct across differing retained spans (missing words compare as zero); cardinality pre-checks keep the common unequal/too-big cases O(1) - typed-nil receivers and operands decline to the generic path - rapid differential tests against Map-based generic results with explicitly drawn equal/subset/superset/disjoint operand shapes, span-shape and sign-extreme BitSet cases, third-party stub coverage, nil-receiver pins Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RLrWSEKLMPuf1kq1KDCoxs
Contributor
There was a problem hiding this comment.
Pull request overview
Adds optional predicate-optimization interfaces to the sets package so Equal, Disjoint, Subset, and Superset can take fast paths when the operand types support them, matching the existing “declining boolean” pattern used for set algebra and Max/Min.
Changes:
- Introduces new optional interfaces
Equaler,Disjointer, andSubsetter, and wires them into the package-level predicate functions (includingSupersetvia swapped operands). - Implements optimized predicate methods for
SortedSet(slice/scan-based) andBitSet(word-wise with early exits and span-aware comparisons). - Adds differential + targeted tests to validate fast paths, fallback behavior, external implementations, and typed-nil decline behavior; updates docs to describe the new hooks.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| sorted.go | Implements Equaler/Disjointer/Subsetter fast paths for SortedSet using slice equality and two-pointer scans. |
| sorted_test.go | Adds rapid differential tests for predicate fast paths and verifies decline behavior for non-SortedSet operands. |
| set.go | Defines the new optional predicate interfaces and updates Subset/Equal/Disjoint to consult them (and Superset via Subset(b, a)). |
| README.MD | Documents the new predicate optional interfaces and how they’re consulted. |
| coverage_test.go | Adds third-party-style stub tests proving package-level functions honor external implementations and decline on request; adds typed-nil predicate tests. |
| CLAUDE.md | Updates architecture/docs to include the new predicate optimization interfaces. |
| bitset.go | Implements Equaler/Disjointer/Subsetter fast paths for BitSet with span-aware word operations. |
| bitset_test.go | Adds rapid differential tests plus targeted span-shape cases for BitSet predicate fast paths and decline behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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
Second item from the optional-interface survey (follow-up to #42): the predicate functions now have optimization hooks, using the same declining-boolean pattern as the set-algebra and
Maxer/Minnerinterfaces.New optional
Equaler/Disjointer/SubsetterinterfacesPackage-level
Equal,Disjoint, andSubsetconsult their first operand and fall back to the generic element-wise path on decline.SupersetisSubsetwith the operands swapped, so it consults its second operand'sSubsetter— no fourth interface needed.Predicates short-circuit and allocate nothing, so the wins are larger than the algebra ones:
Equalis a directslices.Equal;Disjoint/Subsetare short-circuiting two-pointer scans — O(N+M) worst case, no hashingEqualis a cardinality check + word compare,DisjointANDs the span overlap,Subsetis word-wise AND NOT — all O(W) with early exitCorrectness subtleties handled: BitSet spans retained after Removes (equal sets can have different spans — words missing from a span compare as zero; for
Equal, matching words over the receiver's span plus equal cardinalities imply no bits outside it), and typed-nil receivers/operands decline to the generic path rather than dereferencing backing storage (the class of bug review caught on #42).Testing
TestSortedSet_PredicateOps/TestBitSet_PredicateOps: rapid differential tests of all four predicates against Map-based generic results, with equal/subset/superset/disjoint operand shapes drawn explicitly (independent draws would rarely produce them); mixed operands prove the fallback; targeted BitSet cases cover differing retained spans, non-overlapping spans, bits outside the other's span, and sign-bit mapping extremesTestPredicateOptionalInterfaces: third-party-style stub proving the package-level functions honor external implementations (includingSupersetvia the second operand) and fall back on declineTestNilReceiverPredicates: pins the typed-nil decline behaviorgo vet,staticcheck, andgo test -race ./...all clean🤖 Generated with Claude Code
https://claude.ai/code/session_01RLrWSEKLMPuf1kq1KDCoxs