diff --git a/src/libraries/System.Memory/src/System/Buffers/ReadOnlySequence.Helpers.cs b/src/libraries/System.Memory/src/System/Buffers/ReadOnlySequence.Helpers.cs index 62ea2342c2881..930f5b6e71b87 100644 --- a/src/libraries/System.Memory/src/System/Buffers/ReadOnlySequence.Helpers.cs +++ b/src/libraries/System.Memory/src/System/Buffers/ReadOnlySequence.Helpers.cs @@ -25,7 +25,7 @@ internal bool TryGetBuffer(in SequencePosition position, out ReadOnlyMemory m SequenceType type = GetSequenceType(); object? endObject = _endObject; - int startIndex = GetIndex(position); + int startIndex = position.GetInteger(); int endIndex = GetIndex(_endInteger); if (type == SequenceType.MultiSegment) @@ -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) @@ -333,7 +333,7 @@ private static SequencePosition SeekMultiSegment(ReadOnlySequenceSegment? 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; @@ -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; @@ -476,9 +473,9 @@ private ReadOnlySequence SliceImpl(in SequencePosition start, in SequencePosi return new ReadOnlySequence( start.GetObject(), - GetIndex(start) | (_startInteger & ReadOnlySequence.FlagBitMask), + start.GetInteger() | (_startInteger & ReadOnlySequence.FlagBitMask), end.GetObject(), - GetIndex(end) | (_endInteger & ReadOnlySequence.FlagBitMask) + end.GetInteger() | (_endInteger & ReadOnlySequence.FlagBitMask) ); } @@ -491,7 +488,7 @@ private ReadOnlySequence SliceImpl(in SequencePosition start) return new ReadOnlySequence( start.GetObject(), - GetIndex(start) | (_startInteger & ReadOnlySequence.FlagBitMask), + start.GetInteger() | (_startInteger & ReadOnlySequence.FlagBitMask), _endObject, _endInteger ); diff --git a/src/libraries/System.Memory/src/System/Buffers/ReadOnlySequence.cs b/src/libraries/System.Memory/src/System/Buffers/ReadOnlySequence.cs index fa69d4d733971..ff260b6aabbea 100644 --- a/src/libraries/System.Memory/src/System/Buffers/ReadOnlySequence.cs +++ b/src/libraries/System.Memory/src/System/Buffers/ReadOnlySequence.cs @@ -105,8 +105,8 @@ public ReadOnlySequence(ReadOnlySequenceSegment startSegment, int startIndex, _startObject = startSegment; _endObject = endSegment; - _startInteger = ReadOnlySequence.SegmentToSequenceStart(startIndex); - _endInteger = ReadOnlySequence.SegmentToSequenceEnd(endIndex); + _startInteger = startIndex; + _endInteger = endIndex; } /// @@ -119,7 +119,7 @@ public ReadOnlySequence(T[] array) _startObject = array; _endObject = array; - _startInteger = ReadOnlySequence.ArrayToSequenceStart(0); + _startInteger = 0; _endInteger = ReadOnlySequence.ArrayToSequenceEnd(array.Length); } @@ -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); } @@ -150,7 +150,7 @@ public ReadOnlySequence(ReadOnlyMemory memory) _startObject = manager; _endObject = manager; _startInteger = ReadOnlySequence.MemoryManagerToSequenceStart(index); - _endInteger = ReadOnlySequence.MemoryManagerToSequenceEnd(length); + _endInteger = length; } else if (MemoryMarshal.TryGetArray(memory, out ArraySegment segment)) { @@ -158,7 +158,7 @@ public ReadOnlySequence(ReadOnlyMemory memory) 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)) @@ -224,7 +224,7 @@ public ReadOnlySequence 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) @@ -275,7 +275,7 @@ public ReadOnlySequence 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) @@ -352,7 +352,7 @@ public ReadOnlySequence 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) @@ -453,7 +453,7 @@ public ReadOnlySequence Slice(SequencePosition start, long length) [MethodImpl(MethodImplOptions.AggressiveInlining)] public ReadOnlySequence 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); } @@ -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) @@ -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;