From dca08659e81b2d0bc840e78461c25e428eb97c2e Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Sat, 10 Jun 2023 07:38:35 -0400 Subject: [PATCH] Simplify ManagedWebSocket.ApplyMask (#87331) The alignment code isn't necessary or implemented ideally and can make shorter lengths slower to process. Deleting it for now, and if we decide it's valuable to add back forced-alignment later, we can do so in a more optimal manner. --- .../src/System/Net/WebSockets/ManagedWebSocket.cs | 61 +++++----------------- 1 file changed, 13 insertions(+), 48 deletions(-) diff --git a/src/libraries/System.Net.WebSockets/src/System/Net/WebSockets/ManagedWebSocket.cs b/src/libraries/System.Net.WebSockets/src/System/Net/WebSockets/ManagedWebSocket.cs index 114cbbe..3b24288 100644 --- a/src/libraries/System.Net.WebSockets/src/System/Net/WebSockets/ManagedWebSocket.cs +++ b/src/libraries/System.Net.WebSockets/src/System/Net/WebSockets/ManagedWebSocket.cs @@ -1451,61 +1451,26 @@ namespace System.Net.WebSockets { byte* toMaskPtr = toMaskBeg; byte* toMaskEnd = toMaskBeg + toMask.Length; - byte* maskPtr = (byte*)&mask; if (toMaskEnd - toMaskPtr >= sizeof(int)) { - // align our pointer to sizeof(int) - - while ((ulong)toMaskPtr % sizeof(int) != 0) - { - Debug.Assert(toMaskPtr < toMaskEnd); - - *toMaskPtr++ ^= maskPtr[maskIndex]; - maskIndex = (maskIndex + 1) & 3; - } - - int rolledMask; - if (BitConverter.IsLittleEndian) - { - rolledMask = (int)BitOperations.RotateRight((uint)mask, maskIndex * 8); - } - else - { - rolledMask = (int)BitOperations.RotateLeft((uint)mask, maskIndex * 8); - } - - // use SIMD if possible. + int rolledMask = BitConverter.IsLittleEndian ? + (int)BitOperations.RotateRight((uint)mask, maskIndex * 8) : + (int)BitOperations.RotateLeft((uint)mask, maskIndex * 8); - if (Vector.IsHardwareAccelerated && Vector.Count % sizeof(int) == 0 && (toMaskEnd - toMaskPtr) >= Vector.Count) + // Process Vector.Count bytes at a time. + if (Vector.IsHardwareAccelerated && (toMaskEnd - toMaskPtr) >= Vector.Count) { - // align our pointer to Vector.Count - - while ((ulong)toMaskPtr % (uint)Vector.Count != 0) - { - Debug.Assert(toMaskPtr < toMaskEnd); - - *(int*)toMaskPtr ^= rolledMask; - toMaskPtr += sizeof(int); - } - - // use SIMD. - - if (toMaskEnd - toMaskPtr >= Vector.Count) + Vector maskVector = Vector.AsVectorByte(new Vector(rolledMask)); + do { - Vector maskVector = Vector.AsVectorByte(new Vector(rolledMask)); - - do - { - *(Vector*)toMaskPtr ^= maskVector; - toMaskPtr += Vector.Count; - } - while (toMaskEnd - toMaskPtr >= Vector.Count); + *(Vector*)toMaskPtr ^= maskVector; + toMaskPtr += Vector.Count; } + while (toMaskEnd - toMaskPtr >= Vector.Count); } - // process remaining data (or all, if couldn't use SIMD) 4 bytes at a time. - + // Process 4 bytes at a time. while (toMaskEnd - toMaskPtr >= sizeof(int)) { *(int*)toMaskPtr ^= rolledMask; @@ -1513,8 +1478,8 @@ namespace System.Net.WebSockets } } - // do any remaining data a byte at a time. - + // Process 1 byte at a time. + byte* maskPtr = (byte*)&mask; while (toMaskPtr != toMaskEnd) { *toMaskPtr++ ^= maskPtr[maskIndex]; -- 2.7.4