-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ReadOnlySequence<T> GetOffset does not function correctly with non-zero starting indices #75866
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Tagging subscribers to this area: @dotnet/area-system-buffers Issue DetailsDescription
Reproduction Stepsusing System;
using System.Buffers;
var seq = new ReadOnlySequence<byte>(new byte[10], 1, 10 - 1);
// GetOffset: Returns the offset of a position within this sequence from the *start*.
var offset = seq.GetOffset(seq.Start);
if (offset != 0)
throw new Exception("Bugged, was: " + offset); https://dotnetfiddle.net/lpUi0v Expected behaviorEffectively the roundtrip test I specified in the reproduction steps should have been in the test suite. Given the xml docs on specifically note "Returns the offset of a position within this sequence from the start" I would expect the offset to be 0 here. This does not just happen with seq.Start but any position from that moment on. Actual behaviorYou get the starting index. Regression?No response Known WorkaroundsNo response ConfigurationThis has been bugged ever since ReadOnlySequence was shipped. Other informationNo response
|
@vcsjones I can make a PR to fix this if this is indeed unexpected behavior. |
@NinoFloris I would wait for one of the area owners to weigh in first. |
Looking into this now |
In the code, what's happening is we are adding There are a few possibilities here:
|
@NinoFloris After some discussion I think 2.a. is correct here. If you would like to put up a fix (with a unit test preferably), feel free, otherwise I can tackle it. Thanks for pointing out this bug! |
Haven't had time to follow up on this yet. I just wanted to add a small update that I'm no longer confident this is a bug. When I tried to implement 2.a., there were a lot of test cases that failed, and most of them seem to be written in such a way that implies the current behavior is intended. |
Its leaking a hell of a lot of internal indices etc for it to be intended IMO |
I was hit by this problem too. The confusion is acute because I.e. the returned value from SequencePosition currentPosition = ... // e.g. a saved position or position of SequenceReader
long distance = readOnlySequence.GetOffset(currentPosition) - readOnlySequence.GetOffset(readOnlySequence.Start);
// distance now has the value the caller was expecting to receive from readOnlySequence.GetOffset(currentPosition) alone The current behaviour may not be ideal but some code may now depend upon it. A few possibilities I thought of for improvement:
public long GetDistance(SequencePosition start, SequencePosition end)
|
Another issue with Example:
|
At the very least, the documentation should be clarified because it is not clear/consistent I'm not sure about deprecating/removing GetOffset since it's the "inverse" (modulo the round trip issue) of GetPosition. Here is the API request for it where it was added for that purpose: #27158 (the video of the review). Since it's unlikely we take these APIs away, I think GetDistance is not needed (you can convert to offset and subtract). But feel free to make an API request issue for GetDistance if you think it is still useful for avoiding confusion. |
Description
Calling
long ReadOnlySequence<T>.GetOffset(SequencePosition position)
does not function correctly if the sequence was constructed with a segment/array having a non-zero starting index.Reproduction Steps
https://dotnetfiddle.net/lpUi0v
Expected behavior
Given the xml docs specifically note "Returns the offset of a position within this sequence from the start" I would expect the offset to be 0 when passing in seq.Start.
We're using GetOffset to Rewind a SequenceReader to a SequencePosition previously saved. SequenceReader is missing a method to do so directly however (would an api proposal for this be accepted?). So we take the offset of the position, subtracting it from Consumed and pass in the expected count to
SequenceReader.Rewind
.Care also needs to be taken not to pass in 0, see #68774
Configuration
This is an issue ever since ReadOnlySequence.GetOffset was shipped in #771.
A version of the roundtrip test I specified in the reproduction steps should have been in the test suite.
The text was updated successfully, but these errors were encountered: