Skip to content

ReadOnlySequence.Slice(int start, int length) has (possibly) unexpected behavior  #67607

Open

Description

@gfr-g

Description

I'm not sure whether this is a bug or expected behavior but calling ReadOnlySequence.Slice(int start, int length) on a sequence with more than one segment with a start index of 0 and length equal to the size of the first segment, returns a sequence with two segments where the second one is empty (this actually happen for slices of any size, as long as they are on a segment border, and regardless of whether we start from the beginning of the first segment or the beginning of any other segment)

Reproduction Steps

using System.Buffers;

var buffer = new byte[4097];

var seg1 = new Seg(buffer.AsMemory(^1), 4096);
var seg0 = new Seg(buffer.AsMemory(..^1), 0);
seg0.SetNext(seg1);

var sequence = new ReadOnlySequence<byte>(seg0, 0, seg1, 1);
var result = sequence.Slice(0, sequence.First.Length);

Console.WriteLine(result.IsSingleSegment); // this prints False  


internal class Seg : ReadOnlySequenceSegment<byte>
{
    public Seg(ReadOnlyMemory<byte> memory, long runningIndex)
    {
        Memory = memory;
        RunningIndex = runningIndex;
    }

    public void SetNext(Seg seg)
    {
        Next = seg;
    }
}

Expected behavior

When slicing a sequence on segments border, I expect to get a sequence which contains only the sliced segments.

Actual behavior

Currently, slicing as described adds an empty segment at the end of the sequence.

Regression?

No response

Known Workarounds

No response

Configuration

NET 6.0 (but I also tried with the latest 7.0 preview)
Windows 10 x64

Other information

No response

Activity

  1. ghost added
    untriaged
    on Apr 5, 2022
  2. changed the title [-]ReadOnlySequence.Slice(int start, int length) has (perhaps) unexpected behavior [/-] [+]ReadOnlySequence.Slice(int start, int length) has (possibly) unexpected behavior [/+] on Apr 6, 2022
  3. added this to the 7.0.0 milestone on Apr 6, 2022
  4. removed
    untriaged
    on Apr 25, 2022
  5. davidfowl commented on May 3, 2022

    @davidfowl
    Member

    I think this is a read only sequence issue.

  6. danmoseley commented on May 3, 2022

    @danmoseley
    Contributor

    Is it possible to give a repro snippet that is only in terms of span/sequence and not pipes?

  7. davidfowl commented on May 3, 2022

    @davidfowl
    Member

    Tangentially related to #30786

  8. davidfowl commented on May 3, 2022

    @davidfowl
    Member

    Is it possible to give a repro snippet that is only in terms of span/sequence and not pipes?

    I updated the issue

  9. modified the milestones: 7.0.0, Future on Jul 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions