镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

🐛 Fix SequenceSet#slice(cardinality..any) -> empty - #778

Open
nevans wants to merge 1 commit into
sequence_set/fix-slice-empty-set-from-cardinalityfrom
sequence_set/fix-slice-subset-with-invalid-start-index
Open

nevans wants to merge 1 commit into
sequence_set/fix-slice-empty-set-from-cardinalityfrom
sequence_set/fix-slice-subset-with-invalid-start-index

Conversation

@nevans

@nevans nevans commented Oct 3, 2026

Copy link
Copy Markdown
Collaborator

This changes a SequenceSet#slice to return an empty set (rather than nil) in the same circumstance. This mimics Array#slice, which returns an empty array when starting at the array's size (immediately after the last index). Note that SequenceSet#slice indexes the monotonic set numbers, so #cardinality is used instead of #size (which measures the ordered list data).

This could be seen as a minor breaking change. But I'm treating it as a bug fix because:

  • SequenceSet#slice was always intended to mimic Array#slice.
  • The documented API doesn't change, it still returns nil or a set.
  • SequenceSet#slice already returns an empty set sometimes.

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.

@nevans
nevans added this pull request to stack #775 October 3, 2026 22:32
@nevans nevans added the bug Something isn't working label Oct 3, 2026
This changes a `SequenceSet#slice` to return an empty set (rather than
`nil`) in the same circumstance.  This mimics `Array#slice`, which
returns an empty array when starting at the array's size (immediately
after the last index).  Note that `SequenceSet#slice` indexes the
monotonic set numbers, so `#cardinality` is used instead of `#size`
(which measures the ordered list data).

This could be seen as a minor breaking change.  But I'm treating it as a
bugfix because:
* `SequenceSet#slice` was always intended to mimic `Array#slice`.
* The documented API doesn't change, it still returns `nil` or a set.
* `SequenceSet#slice` already returns an empty set sometimes.

So, I think it's unlikely there is much code that depends on this bug.
And any code that _does_ depend on it is (arguably) buggy already, and
should be easy to fix.

Please note that this is done as a minimal change to avoid refactoring
the underlying slice implementation.  Since `#cardinality` isn't cached,
this currently requires scanning every set element a second time, which
is _very_ inefficient for large sets.  Fixing `SequenceSet#slice`
performance is what led to finding and fixing this bug in the first
place.  But, for now I'm prioritizing fixing and testing `#slice`'s
behavior, even if that leads to some reduced performance in cases like
this.
@nevans
nevans force-pushed the sequence_set/fix-slice-subset-with-invalid-start-index branch from 9f4aa22 to a834715 Compare October 3, 2026 22:39

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant