From 57a2712a4d1d1e5f91ee7f6a1a1f745d547a15b7 Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Thu, 18 Jul 2024 21:41:00 -0800 Subject: [PATCH] Revert "Replace a few stackallocs with collection expressions (#105121)" (#105128) This reverts commit 87cd8935bca05bc44448e00d1276ccbfce49769b. --- .../Extensions/Logging/DebuggerDisplayFormatting.cs | 6 +++--- src/libraries/Common/src/System/Net/Security/MD4.cs | 5 ++--- .../SP800108HmacCounterKdfImplementationCng.cs | 4 ++-- .../Microsoft.Extensions.Logging/src/Logger.cs | 6 +++--- .../src/System/StartupHookProvider.cs | 6 +++--- .../src/System/Xml/XmlBufferReader.cs | 12 ++++++------ .../System/Security/Cryptography/Cose/CoseHelpers.cs | 4 ++-- .../Security/Cryptography/Pkcs/CmsSignature.RSA.cs | 4 +++- .../Cryptography/Pbkdf2Implementation.Windows.cs | 3 ++- .../Text/Encodings/Web/DefaultJavaScriptEncoder.cs | 4 ++-- .../System/Text/Encodings/Web/DefaultUrlEncoder.cs | 4 ++-- .../Encodings/Web/OptimizedInboxTextEncoder.Ascii.cs | 2 +- .../System/Text/RegularExpressions/RegexCharClass.cs | 2 +- 13 files changed, 32 insertions(+), 30 deletions(-) diff --git a/src/libraries/Common/src/Extensions/Logging/DebuggerDisplayFormatting.cs b/src/libraries/Common/src/Extensions/Logging/DebuggerDisplayFormatting.cs index b5160095f1eb4f..a72def069cbe19 100644 --- a/src/libraries/Common/src/Extensions/Logging/DebuggerDisplayFormatting.cs +++ b/src/libraries/Common/src/Extensions/Logging/DebuggerDisplayFormatting.cs @@ -31,15 +31,15 @@ internal static string DebuggerToString(string name, ILogger logger) internal static LogLevel? CalculateEnabledLogLevel(ILogger logger) { - ReadOnlySpan logLevels = - [ + ReadOnlySpan logLevels = stackalloc LogLevel[] + { LogLevel.Critical, LogLevel.Error, LogLevel.Warning, LogLevel.Information, LogLevel.Debug, LogLevel.Trace, - ]; + }; LogLevel? minimumLevel = null; diff --git a/src/libraries/Common/src/System/Net/Security/MD4.cs b/src/libraries/Common/src/System/Net/Security/MD4.cs index dd075d324f4fa2..61a61613d06499 100644 --- a/src/libraries/Common/src/System/Net/Security/MD4.cs +++ b/src/libraries/Common/src/System/Net/Security/MD4.cs @@ -66,10 +66,9 @@ internal static void HashData(ReadOnlySpan source, Span destination) Span buffer = stackalloc byte[64]; buffer.Clear(); - // Initialize the context - Span state = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476]; - Span count = [0, 0]; + Span state = stackalloc uint[4] { 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476 }; + Span count = stackalloc uint[2] { 0, 0 }; HashCore(source, state, count, buffer); diff --git a/src/libraries/Microsoft.Bcl.Cryptography/src/System/Security/Cryptography/SP800108HmacCounterKdfImplementationCng.cs b/src/libraries/Microsoft.Bcl.Cryptography/src/System/Security/Cryptography/SP800108HmacCounterKdfImplementationCng.cs index 6bbd2f7f1b7fde..4370fd79662e0a 100644 --- a/src/libraries/Microsoft.Bcl.Cryptography/src/System/Security/Cryptography/SP800108HmacCounterKdfImplementationCng.cs +++ b/src/libraries/Microsoft.Bcl.Cryptography/src/System/Security/Cryptography/SP800108HmacCounterKdfImplementationCng.cs @@ -38,7 +38,7 @@ internal unsafe SP800108HmacCounterKdfImplementationCng(ReadOnlySpan key, else { // CNG requires a non-null pointer even when the length is zero. - symmetricKeyMaterial = [0]; + symmetricKeyMaterial = stackalloc byte[] { 0 }; symmetricKeyMaterialLength = 0; } @@ -82,7 +82,7 @@ internal unsafe SP800108HmacCounterKdfImplementationCng(byte[] key, HashAlgorith else { // CNG requires a non-null pointer even when the length is zero. - symmetricKeyMaterial = [0]; + symmetricKeyMaterial = stackalloc byte[] { 0 }; symmetricKeyMaterialLength = 0; } diff --git a/src/libraries/Microsoft.Extensions.Logging/src/Logger.cs b/src/libraries/Microsoft.Extensions.Logging/src/Logger.cs index b695329fb88970..cf00d35ee31b21 100644 --- a/src/libraries/Microsoft.Extensions.Logging/src/Logger.cs +++ b/src/libraries/Microsoft.Extensions.Logging/src/Logger.cs @@ -238,15 +238,15 @@ private sealed class LoggerProviderDebugView(string providerName, MessageLogger? return null; } - ReadOnlySpan logLevels = - [ + ReadOnlySpan logLevels = stackalloc LogLevel[] + { LogLevel.Critical, LogLevel.Error, LogLevel.Warning, LogLevel.Information, LogLevel.Debug, LogLevel.Trace, - ]; + }; LogLevel? minimumLevel = null; diff --git a/src/libraries/System.Private.CoreLib/src/System/StartupHookProvider.cs b/src/libraries/System.Private.CoreLib/src/System/StartupHookProvider.cs index baa65f9e40ebe6..ed2269d99f6690 100644 --- a/src/libraries/System.Private.CoreLib/src/System/StartupHookProvider.cs +++ b/src/libraries/System.Private.CoreLib/src/System/StartupHookProvider.cs @@ -85,13 +85,13 @@ private static unsafe void CallStartupHook(char* pStartupHookPart) private static void ParseStartupHook(ref StartupHookNameOrPath startupHook, string startupHookPart) { - ReadOnlySpan disallowedSimpleAssemblyNameChars = - [ + ReadOnlySpan disallowedSimpleAssemblyNameChars = stackalloc char[4] + { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar, ' ', ',' - ]; + }; if (string.IsNullOrEmpty(startupHookPart)) { diff --git a/src/libraries/System.Private.DataContractSerialization/src/System/Xml/XmlBufferReader.cs b/src/libraries/System.Private.DataContractSerialization/src/System/Xml/XmlBufferReader.cs index d063d729297e95..780f4827f534af 100644 --- a/src/libraries/System.Private.DataContractSerialization/src/System/Xml/XmlBufferReader.cs +++ b/src/libraries/System.Private.DataContractSerialization/src/System/Xml/XmlBufferReader.cs @@ -396,13 +396,13 @@ public decimal ReadDecimal() { byte[] buffer = GetBuffer(ValueHandleLength.Decimal, out int offset); ReadOnlySpan bytes = buffer.AsSpan(offset, sizeof(decimal)); - ReadOnlySpan span = - [ + ReadOnlySpan span = stackalloc int[4] + { BinaryPrimitives.ReadInt32LittleEndian(bytes.Slice(8, 4)), BinaryPrimitives.ReadInt32LittleEndian(bytes.Slice(12, 4)), BinaryPrimitives.ReadInt32LittleEndian(bytes.Slice(4, 4)), BinaryPrimitives.ReadInt32LittleEndian(bytes.Slice(0, 4)) - ]; + }; Advance(ValueHandleLength.Decimal); return new decimal(span); @@ -975,13 +975,13 @@ public decimal GetDecimal(int offset) else { ReadOnlySpan bytes = _buffer.AsSpan(offset, sizeof(decimal)); - ReadOnlySpan span = - [ + ReadOnlySpan span = stackalloc int[4] + { BinaryPrimitives.ReadInt32LittleEndian(bytes.Slice(8, 4)), BinaryPrimitives.ReadInt32LittleEndian(bytes.Slice(12, 4)), BinaryPrimitives.ReadInt32LittleEndian(bytes.Slice(4, 4)), BinaryPrimitives.ReadInt32LittleEndian(bytes.Slice(0, 4)) - ]; + }; return new decimal(span); } diff --git a/src/libraries/System.Security.Cryptography.Cose/src/System/Security/Cryptography/Cose/CoseHelpers.cs b/src/libraries/System.Security.Cryptography.Cose/src/System/Security/Cryptography/Cose/CoseHelpers.cs index 3fb45ab8259e40..12e9fd9a787368 100644 --- a/src/libraries/System.Security.Cryptography.Cose/src/System/Security/Cryptography/Cose/CoseHelpers.cs +++ b/src/libraries/System.Security.Cryptography.Cose/src/System/Security/Cryptography/Cose/CoseHelpers.cs @@ -47,12 +47,12 @@ internal static void WriteByteStringLength(IncrementalHash hasher, ulong value) if (value < (byte)CborAdditionalInfo.Additional8BitData) { initialByte = new CborInitialByte(MajorType, (CborAdditionalInfo)value); - hasher.AppendData([initialByte.InitialByte]); + hasher.AppendData(stackalloc byte[] { initialByte.InitialByte }); } else if (value <= byte.MaxValue) { initialByte = new CborInitialByte(MajorType, CborAdditionalInfo.Additional8BitData); - hasher.AppendData([initialByte.InitialByte, (byte)value]); + hasher.AppendData(stackalloc byte[] { initialByte.InitialByte, (byte)value }); } else if (value <= ushort.MaxValue) { diff --git a/src/libraries/System.Security.Cryptography.Pkcs/src/System/Security/Cryptography/Pkcs/CmsSignature.RSA.cs b/src/libraries/System.Security.Cryptography.Pkcs/src/System/Security/Cryptography/Pkcs/CmsSignature.RSA.cs index e0459dc899b7c5..80aa6abb9085a3 100644 --- a/src/libraries/System.Security.Cryptography.Pkcs/src/System/Security/Cryptography/Pkcs/CmsSignature.RSA.cs +++ b/src/libraries/System.Security.Cryptography.Pkcs/src/System/Security/Cryptography/Pkcs/CmsSignature.RSA.cs @@ -186,7 +186,9 @@ protected override RSASignaturePadding GetSignaturePadding( return RSASignaturePadding.Pkcs1; } - ReadOnlySpan expectedParameters = [0x05, 0x00]; + Span expectedParameters = stackalloc byte[2]; + expectedParameters[0] = 0x05; + expectedParameters[1] = 0x00; if (expectedParameters.SequenceEqual(signatureParameters.Value.Span)) { diff --git a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/Pbkdf2Implementation.Windows.cs b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/Pbkdf2Implementation.Windows.cs index ec067bd47473d2..c40d105152cf80 100644 --- a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/Pbkdf2Implementation.Windows.cs +++ b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/Pbkdf2Implementation.Windows.cs @@ -60,7 +60,8 @@ private static unsafe void FillKeyDerivation( if (password.IsEmpty) { // CNG won't accept a null pointer for the password. - symmetricKeyMaterial = [0]; + symmetricKeyMaterial = stackalloc byte[1]; + symmetricKeyMaterialLength = 0; clearSpan = default; } else if (password.Length <= hashBlockSizeBytes) diff --git a/src/libraries/System.Text.Encodings.Web/src/System/Text/Encodings/Web/DefaultJavaScriptEncoder.cs b/src/libraries/System.Text.Encodings.Web/src/System/Text/Encodings/Web/DefaultJavaScriptEncoder.cs index 7c0f1d7bd4f03c..be6d2499feb3c0 100644 --- a/src/libraries/System.Text.Encodings.Web/src/System/Text/Encodings/Web/DefaultJavaScriptEncoder.cs +++ b/src/libraries/System.Text.Encodings.Web/src/System/Text/Encodings/Web/DefaultJavaScriptEncoder.cs @@ -31,9 +31,9 @@ private DefaultJavaScriptEncoder(TextEncoderSettings settings, bool allowMinimal _innerEncoder = allowMinimalJsonEscaping ? new OptimizedInboxTextEncoder(EscaperImplementation.SingletonMinimallyEscaped, settings.GetAllowedCodePointsBitmap(), forbidHtmlSensitiveCharacters: false, - extraCharactersToEscape: ['\"', '\\']) + extraCharactersToEscape: stackalloc char[] { '\"', '\\' }) : new OptimizedInboxTextEncoder(EscaperImplementation.Singleton, settings.GetAllowedCodePointsBitmap(), forbidHtmlSensitiveCharacters: true, - extraCharactersToEscape: ['\\', '`']); + extraCharactersToEscape: stackalloc char[] { '\\', '`' }); } public override int MaxOutputCharactersPerInputCharacter => 6; // "\uXXXX" for a single char ("\uXXXX\uYYYY" [12 chars] for supplementary scalar value) diff --git a/src/libraries/System.Text.Encodings.Web/src/System/Text/Encodings/Web/DefaultUrlEncoder.cs b/src/libraries/System.Text.Encodings.Web/src/System/Text/Encodings/Web/DefaultUrlEncoder.cs index e674f12687f1c5..6b72c982582b7f 100644 --- a/src/libraries/System.Text.Encodings.Web/src/System/Text/Encodings/Web/DefaultUrlEncoder.cs +++ b/src/libraries/System.Text.Encodings.Web/src/System/Text/Encodings/Web/DefaultUrlEncoder.cs @@ -64,7 +64,7 @@ internal DefaultUrlEncoder(TextEncoderSettings settings) // Basic Latin set is: // ALPHA / DIGIT / "-" / "." / "_" / "~" / "!" / "$" / "(" / ")" / "*" / "," / ";" / "@" - _innerEncoder = new OptimizedInboxTextEncoder(EscaperImplementation.Singleton, settings.GetAllowedCodePointsBitmap(), extraCharactersToEscape: [ + _innerEncoder = new OptimizedInboxTextEncoder(EscaperImplementation.Singleton, settings.GetAllowedCodePointsBitmap(), extraCharactersToEscape: stackalloc char[] { ' ', // chars from Basic Latin which aren't already disallowed by the base encoder '#', '%', @@ -96,7 +96,7 @@ internal DefaultUrlEncoder(TextEncoderSettings settings) '\uFFFD', '\uFFFE', '\uFFFF', - ]); + }); } public override int MaxOutputCharactersPerInputCharacter => 9; // "%XX%YY%ZZ" for a single char ("%XX%YY%ZZ%WW" [12 chars] for supplementary scalar value) diff --git a/src/libraries/System.Text.Encodings.Web/src/System/Text/Encodings/Web/OptimizedInboxTextEncoder.Ascii.cs b/src/libraries/System.Text.Encodings.Web/src/System/Text/Encodings/Web/OptimizedInboxTextEncoder.Ascii.cs index cc1c28576e9d63..d2aac7f94f4cfb 100644 --- a/src/libraries/System.Text.Encodings.Web/src/System/Text/Encodings/Web/OptimizedInboxTextEncoder.Ascii.cs +++ b/src/libraries/System.Text.Encodings.Web/src/System/Text/Encodings/Web/OptimizedInboxTextEncoder.Ascii.cs @@ -87,7 +87,7 @@ internal void PopulatePreescapedData(in AllowedBmpCodePointsBitmap allowedCodePo { this = default; // clear all existing data - Span tempBuffer = ['\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0']; + Span tempBuffer = stackalloc char[8] { '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0' }; for (int i = 0; i < 128; i++) { ulong thisPreescapedData; diff --git a/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexCharClass.cs b/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexCharClass.cs index d78bc3c0a7f4a1..e7350da6bac18e 100644 --- a/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexCharClass.cs +++ b/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexCharClass.cs @@ -1541,7 +1541,7 @@ private static RegexCharClass ParseRecursive(string charClass, int start) /// The character for which to create the set. /// The create set string. public static string OneToStringClass(char c) - => CharsToStringClass([c]); + => CharsToStringClass(stackalloc char[1] { c }); internal static unsafe string CharsToStringClass(ReadOnlySpan chars) {