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
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.
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
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`).
This branch has not been deployed
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.