diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Infrastructure/MemoryPoolIterator.cs b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Infrastructure/MemoryPoolIterator.cs index b0b6837f6..08718d6db 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Infrastructure/MemoryPoolIterator.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Infrastructure/MemoryPoolIterator.cs @@ -268,41 +268,40 @@ public unsafe int Seek( if (following >= _vectorSpan) { var byte0Equals = Vector.Equals(new Vector(array, index), byte0Vector); - - if (byte0Equals.Equals(Vector.Zero)) + if (!byte0Equals.Equals(Vector.Zero)) { - if (bytesScanned + _vectorSpan >= limit) + _block = block; + + var firstEqualByteIndex = FindFirstEqualByte(byte0Equals); + var vectorBytesScanned = firstEqualByteIndex + 1; + + if (bytesScanned + vectorBytesScanned > limit) { - _block = block; // Ensure iterator is left at limit position _index = index + (limit - bytesScanned); bytesScanned = limit; return -1; } - bytesScanned += _vectorSpan; - following -= _vectorSpan; - index += _vectorSpan; - continue; - } - - _block = block; + _index = index + firstEqualByteIndex; + bytesScanned += vectorBytesScanned; - var firstEqualByteIndex = FindFirstEqualByte(ref byte0Equals); - var vectorBytesScanned = firstEqualByteIndex + 1; + return byte0; + } - if (bytesScanned + vectorBytesScanned > limit) + if (bytesScanned + _vectorSpan >= limit) { + _block = block; // Ensure iterator is left at limit position _index = index + (limit - bytesScanned); bytesScanned = limit; return -1; } - _index = index + firstEqualByteIndex; - bytesScanned += vectorBytesScanned; - - return byte0; + bytesScanned += _vectorSpan; + following -= _vectorSpan; + index += _vectorSpan; + continue; } // Need unit tests to test Vector path #if !DEBUG @@ -377,35 +376,34 @@ public unsafe int Seek( { var byte0Equals = Vector.Equals(new Vector(array, index), byte0Vector); - if (byte0Equals.Equals(Vector.Zero)) + if (!byte0Equals.Equals(Vector.Zero)) { - if (block == limit.Block && index + _vectorSpan > limit.Index) + _block = block; + + var firstEqualByteIndex = FindFirstEqualByte(byte0Equals); + + if (_block == limit.Block && index + firstEqualByteIndex > limit.Index) { - _block = block; // Ensure iterator is left at limit position _index = limit.Index; return -1; } - following -= _vectorSpan; - index += _vectorSpan; - continue; - } + _index = index + firstEqualByteIndex; - _block = block; - - var firstEqualByteIndex = FindFirstEqualByte(ref byte0Equals); + return byte0; + } - if (_block == limit.Block && index + firstEqualByteIndex > limit.Index) + if (block == limit.Block && index + _vectorSpan > limit.Index) { + _block = block; // Ensure iterator is left at limit position _index = limit.Index; return -1; } - _index = index + firstEqualByteIndex; - - return byte0; + following -= _vectorSpan; + index += _vectorSpan; } // Need unit tests to test Vector path #if !DEBUG @@ -488,16 +486,17 @@ public unsafe int Seek( if (following >= _vectorSpan) { var data = new Vector(array, index); - var byte0Equals = Vector.Equals(data, byte0Vector); - var byte1Equals = Vector.Equals(data, byte1Vector); + var byte0Equals = Vector.Equals(data, byte0Vector); if (!byte0Equals.Equals(Vector.Zero)) { - byte0Index = FindFirstEqualByte(ref byte0Equals); + byte0Index = FindFirstEqualByte(byte0Equals); } + + var byte1Equals = Vector.Equals(data, byte1Vector); if (!byte1Equals.Equals(Vector.Zero)) { - byte1Index = FindFirstEqualByte(ref byte1Equals); + byte1Index = FindFirstEqualByte(byte1Equals); } if (byte0Index == int.MaxValue && byte1Index == int.MaxValue) @@ -631,21 +630,23 @@ public unsafe int Seek( if (following >= _vectorSpan) { var data = new Vector(array, index); - var byte0Equals = Vector.Equals(data, byte0Vector); - var byte1Equals = Vector.Equals(data, byte1Vector); - var byte2Equals = Vector.Equals(data, byte2Vector); + var byte0Equals = Vector.Equals(data, byte0Vector); if (!byte0Equals.Equals(Vector.Zero)) { - byte0Index = FindFirstEqualByte(ref byte0Equals); + byte0Index = FindFirstEqualByte(byte0Equals); } + + var byte1Equals = Vector.Equals(data, byte1Vector); if (!byte1Equals.Equals(Vector.Zero)) { - byte1Index = FindFirstEqualByte(ref byte1Equals); + byte1Index = FindFirstEqualByte(byte1Equals); } + + var byte2Equals = Vector.Equals(data, byte2Vector); if (!byte2Equals.Equals(Vector.Zero)) { - byte2Index = FindFirstEqualByte(ref byte2Equals); + byte2Index = FindFirstEqualByte(byte2Equals); } if (byte0Index == int.MaxValue && byte1Index == int.MaxValue && byte2Index == int.MaxValue) @@ -747,9 +748,9 @@ public unsafe int Seek( /// /// The first index of the result vector /// byteEquals = 0 - internal static int FindFirstEqualByte(ref Vector byteEquals) + internal static int FindFirstEqualByte(Vector byteEquals) { - if (!BitConverter.IsLittleEndian) return FindFirstEqualByteSlow(ref byteEquals); + if (!BitConverter.IsLittleEndian) return FindFirstEqualByteSlow(byteEquals); // Quasi-tree search var vector64 = Vector.AsVectorInt64(byteEquals); @@ -771,7 +772,7 @@ internal static int FindFirstEqualByte(ref Vector byteEquals) } // Internal for testing - internal static int FindFirstEqualByteSlow(ref Vector byteEquals) + internal static int FindFirstEqualByteSlow(Vector byteEquals) { // Quasi-tree search var vector64 = Vector.AsVectorInt64(byteEquals);