Repository navigation
🐛 Fix SequenceSet#[] -> nil for invalid start - #777
Merged
nevans merged 3 commits intoOct 10, 2026
Merged
Conversation
nevans
commented
Oct 3, 2026
SequenceSet#slice -> nil for invalid start
nevans
added this pull request to stack #775
October 3, 2026 00:38
nevans
force-pushed
the
sequence_set/fix-slice-empty-set-from-cardinality
branch
from
October 3, 2026 13:42
da8d0f6 to
23dd26a
Compare
Collaborator
Author
|
This is a much less serious bug than the other two prior to it in the stack. And the naive implementation currently in this PR does come with a non-trivial extra performance cost. It might be better to pull the performance fix into this PR. |
SequenceSet#slice -> nil for invalid startSequenceSet#[] -> nil for invalid start
This merges together both `#slice_range` short-circuit cases where we can immediately deduce that the range is empty. NOTE that this refactoring does _not_ fix an existing bug: the same-sign detection works for positive..positive and negative..negative, but it messes up when beginning or ending with zero. But fixing that bug causes more scenarios to return empty when they should return nil`ZZ.
This fixes `SequenceSet#slice` so it always returns `nil` when the starting offset is out-of-range, changing a couple situations where it previously returned an empty set: when an exclusive range end is zero, and when range.end is less than same-sign range.begin. Note that `set[cardinality..]` is _not_ considered invalid. This intentionally mimics the behavior of `Array#slice`. This can be refactored more, to simplify and reduce code duplication, and for performance.
By short-circuiting the check that `offset.abs <= cardinality`, this is a small performance improvement over the naive approach. A future refactoring should combine this with the start offset scan (`min = sorted_set_num_at`).
nevans
force-pushed
the
sequence_set/fix-slice-empty-set-from-cardinality
branch
from
October 10, 2026 03:15
feeb0d6 to
fd01186
Compare
Base automatically changed from
sequence_set/fix-slice-range-pos-to-out-of-range-neg
to
master
October 10, 2026 03:25
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.
This fixes
SequenceSet#sliceso it always returnsnilwhen the starting offset is out-of-range, changing a couple situations where it previously returned an empty set: when an exclusive range end is zero, and when range.end is less than same-sign range.begin.Note that starting at cardinality is not considered invalid, eg:
set[cardinality, length]orset[cardinality..int]This intentionally mimics the behavior ofArray#slice.This can be refactored more, to simplify and reduce code duplication, and for performance.
This could be seen as a minor breaking change. But I'm treating it as a bug fix because:
SequenceSet#slicewas always intended to mimicArray#slice.nilor a set.SequenceSet#slicealready returnsnilin other nearly identical situations.So, I think it's unlikely there is much (or any) code that depends on this bug. And any code that does depend on it is (arguably) buggy already, and should be easy to fix.