Skip to content

Commit

Permalink
Now that we dont leak the SequencePosition flags anymore, we dont nee…
Browse files Browse the repository at this point in the history
…d to bitmask the arguments before processing. (#49921)
  • Loading branch information
Kuinox authored May 9, 2021
1 parent 555789d commit 3297e7e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal bool TryGetBuffer(in SequencePosition position, out ReadOnlyMemory<T> m

SequenceType type = GetSequenceType();
object? endObject = _endObject;
int startIndex = GetIndex(position);
int startIndex = position.GetInteger();
int endIndex = GetIndex(_endInteger);

if (type == SequenceType.MultiSegment)
Expand Down Expand Up @@ -273,7 +273,7 @@ private SequencePosition Seek(in SequencePosition start, long offset)
{
object? startObject = start.GetObject();
object? endObject = _endObject;
int startIndex = GetIndex(start);
int startIndex = start.GetInteger();
int endIndex = GetIndex(_endInteger);

if (startObject != endObject)
Expand Down Expand Up @@ -333,7 +333,7 @@ private static SequencePosition SeekMultiSegment(ReadOnlySequenceSegment<T>? cur

private void BoundsCheck(in SequencePosition position, bool positionIsNotNull)
{
uint sliceStartIndex = (uint)GetIndex(position);
uint sliceStartIndex = (uint)position.GetInteger();

object? startObject = _startObject;
object? endObject = _endObject;
Expand Down Expand Up @@ -461,9 +461,6 @@ private SequenceType GetSequenceType()
return (SequenceType)(-(2 * (_startInteger >> 31) + (_endInteger >> 31)));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int GetIndex(in SequencePosition position) => position.GetInteger() & ReadOnlySequence.IndexBitMask;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int GetIndex(int Integer) => Integer & ReadOnlySequence.IndexBitMask;

Expand All @@ -476,9 +473,9 @@ private ReadOnlySequence<T> SliceImpl(in SequencePosition start, in SequencePosi

return new ReadOnlySequence<T>(
start.GetObject(),
GetIndex(start) | (_startInteger & ReadOnlySequence.FlagBitMask),
start.GetInteger() | (_startInteger & ReadOnlySequence.FlagBitMask),
end.GetObject(),
GetIndex(end) | (_endInteger & ReadOnlySequence.FlagBitMask)
end.GetInteger() | (_endInteger & ReadOnlySequence.FlagBitMask)
);
}

Expand All @@ -491,7 +488,7 @@ private ReadOnlySequence<T> SliceImpl(in SequencePosition start)

return new ReadOnlySequence<T>(
start.GetObject(),
GetIndex(start) | (_startInteger & ReadOnlySequence.FlagBitMask),
start.GetInteger() | (_startInteger & ReadOnlySequence.FlagBitMask),
_endObject,
_endInteger
);
Expand Down
35 changes: 11 additions & 24 deletions src/libraries/System.Memory/src/System/Buffers/ReadOnlySequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ public ReadOnlySequence(ReadOnlySequenceSegment<T> startSegment, int startIndex,

_startObject = startSegment;
_endObject = endSegment;
_startInteger = ReadOnlySequence.SegmentToSequenceStart(startIndex);
_endInteger = ReadOnlySequence.SegmentToSequenceEnd(endIndex);
_startInteger = startIndex;
_endInteger = endIndex;
}

/// <summary>
Expand All @@ -119,7 +119,7 @@ public ReadOnlySequence(T[] array)

_startObject = array;
_endObject = array;
_startInteger = ReadOnlySequence.ArrayToSequenceStart(0);
_startInteger = 0;
_endInteger = ReadOnlySequence.ArrayToSequenceEnd(array.Length);
}

Expand All @@ -135,7 +135,7 @@ public ReadOnlySequence(T[] array, int start, int length)

_startObject = array;
_endObject = array;
_startInteger = ReadOnlySequence.ArrayToSequenceStart(start);
_startInteger = start;
_endInteger = ReadOnlySequence.ArrayToSequenceEnd(start + length);
}

Expand All @@ -150,15 +150,15 @@ public ReadOnlySequence(ReadOnlyMemory<T> memory)
_startObject = manager;
_endObject = manager;
_startInteger = ReadOnlySequence.MemoryManagerToSequenceStart(index);
_endInteger = ReadOnlySequence.MemoryManagerToSequenceEnd(length);
_endInteger = length;
}
else if (MemoryMarshal.TryGetArray(memory, out ArraySegment<T> segment))
{
T[]? array = segment.Array;
int start = segment.Offset;
_startObject = array;
_endObject = array;
_startInteger = ReadOnlySequence.ArrayToSequenceStart(start);
_startInteger = start;
_endInteger = ReadOnlySequence.ArrayToSequenceEnd(start + segment.Count);
}
else if (typeof(T) == typeof(char))
Expand Down Expand Up @@ -224,7 +224,7 @@ public ReadOnlySequence<T> Slice(long start, long length)

begin = SeekMultiSegment(startSegment.Next!, endObject!, endIndex, start - currentLength, ExceptionArgument.start);

int beginIndex = GetIndex(begin);
int beginIndex = begin.GetInteger();
object beginObject = begin.GetObject()!;

if (beginObject != endObject)
Expand Down Expand Up @@ -275,7 +275,7 @@ public ReadOnlySequence<T> Slice(long start, SequencePosition end)
uint endIndex = (uint)GetIndex(_endInteger);
object? endObject = _endObject;

uint sliceEndIndex = (uint)GetIndex(end);
uint sliceEndIndex = (uint)end.GetInteger();
object? sliceEndObject = end.GetObject();

if (sliceEndObject == null)
Expand Down Expand Up @@ -352,7 +352,7 @@ public ReadOnlySequence<T> Slice(SequencePosition start, long length)
object? endObject = _endObject;

// Check start before length
uint sliceStartIndex = (uint)GetIndex(start);
uint sliceStartIndex = (uint)start.GetInteger();
object? sliceStartObject = start.GetObject();

if (sliceStartObject == null)
Expand Down Expand Up @@ -453,7 +453,7 @@ public ReadOnlySequence<T> Slice(SequencePosition start, long length)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ReadOnlySequence<T> Slice(SequencePosition start, SequencePosition end)
{
BoundsCheck((uint)GetIndex(start), start.GetObject(), (uint)GetIndex(end), end.GetObject());
BoundsCheck((uint)start.GetInteger(), start.GetObject(), (uint)end.GetInteger(), end.GetObject());
return SliceImpl(start, end);
}

Expand Down Expand Up @@ -540,7 +540,7 @@ public long GetOffset(SequencePosition position)
object? startObject = _startObject;
object? endObject = _endObject;

uint positionIndex = (uint)GetIndex(position);
uint positionIndex = (uint)position.GetInteger();

// if sequence object is null we suppose start segment
if (positionIsNull)
Expand Down Expand Up @@ -667,31 +667,18 @@ internal static class ReadOnlySequence
public const int FlagBitMask = 1 << 31;
public const int IndexBitMask = ~FlagBitMask;

public const int SegmentStartMask = 0;
public const int SegmentEndMask = 0;

public const int ArrayStartMask = 0;
public const int ArrayEndMask = FlagBitMask;

public const int MemoryManagerStartMask = FlagBitMask;
public const int MemoryManagerEndMask = 0;

public const int StringStartMask = FlagBitMask;
public const int StringEndMask = FlagBitMask;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int SegmentToSequenceStart(int startIndex) => startIndex | SegmentStartMask;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int SegmentToSequenceEnd(int endIndex) => endIndex | SegmentEndMask;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int ArrayToSequenceStart(int startIndex) => startIndex | ArrayStartMask;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int ArrayToSequenceEnd(int endIndex) => endIndex | ArrayEndMask;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int MemoryManagerToSequenceStart(int startIndex) => startIndex | MemoryManagerStartMask;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int MemoryManagerToSequenceEnd(int endIndex) => endIndex | MemoryManagerEndMask;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int StringToSequenceStart(int startIndex) => startIndex | StringStartMask;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int StringToSequenceEnd(int endIndex) => endIndex | StringEndMask;
Expand Down

0 comments on commit 3297e7e

Please sign in to comment.