Skip to content

Commit 87cd893

Browse files
authored
Replace a few stackallocs with collection expressions (#105121)
1 parent 9dbd151 commit 87cd893

File tree

13 files changed

+30
-32
lines changed

13 files changed

+30
-32
lines changed

src/libraries/Common/src/Extensions/Logging/DebuggerDisplayFormatting.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ internal static string DebuggerToString(string name, ILogger logger)
3131

3232
internal static LogLevel? CalculateEnabledLogLevel(ILogger logger)
3333
{
34-
ReadOnlySpan<LogLevel> logLevels = stackalloc LogLevel[]
35-
{
34+
ReadOnlySpan<LogLevel> logLevels =
35+
[
3636
LogLevel.Critical,
3737
LogLevel.Error,
3838
LogLevel.Warning,
3939
LogLevel.Information,
4040
LogLevel.Debug,
4141
LogLevel.Trace,
42-
};
42+
];
4343

4444
LogLevel? minimumLevel = null;
4545

src/libraries/Common/src/System/Net/Security/MD4.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,10 @@ internal static void HashData(ReadOnlySpan<byte> source, Span<byte> destination)
6666

6767
Span<byte> buffer = stackalloc byte[64];
6868
buffer.Clear();
69+
6970
// Initialize the context
70-
Span<uint> state = stackalloc uint[4] { 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476 };
71-
Span<uint> count = stackalloc uint[2] { 0, 0 };
71+
Span<uint> state = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476];
72+
Span<uint> count = [0, 0];
7273

7374
HashCore(source, state, count, buffer);
7475

src/libraries/Microsoft.Bcl.Cryptography/src/System/Security/Cryptography/SP800108HmacCounterKdfImplementationCng.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ internal unsafe SP800108HmacCounterKdfImplementationCng(ReadOnlySpan<byte> key,
3838
else
3939
{
4040
// CNG requires a non-null pointer even when the length is zero.
41-
symmetricKeyMaterial = stackalloc byte[] { 0 };
41+
symmetricKeyMaterial = [0];
4242
symmetricKeyMaterialLength = 0;
4343
}
4444

@@ -82,7 +82,7 @@ internal unsafe SP800108HmacCounterKdfImplementationCng(byte[] key, HashAlgorith
8282
else
8383
{
8484
// CNG requires a non-null pointer even when the length is zero.
85-
symmetricKeyMaterial = stackalloc byte[] { 0 };
85+
symmetricKeyMaterial = [0];
8686
symmetricKeyMaterialLength = 0;
8787
}
8888

src/libraries/Microsoft.Extensions.Logging/src/Logger.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,15 +238,15 @@ private sealed class LoggerProviderDebugView(string providerName, MessageLogger?
238238
return null;
239239
}
240240

241-
ReadOnlySpan<LogLevel> logLevels = stackalloc LogLevel[]
242-
{
241+
ReadOnlySpan<LogLevel> logLevels =
242+
[
243243
LogLevel.Critical,
244244
LogLevel.Error,
245245
LogLevel.Warning,
246246
LogLevel.Information,
247247
LogLevel.Debug,
248248
LogLevel.Trace,
249-
};
249+
];
250250

251251
LogLevel? minimumLevel = null;
252252

src/libraries/System.Private.CoreLib/src/System/StartupHookProvider.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,13 @@ private static unsafe void CallStartupHook(char* pStartupHookPart)
8585

8686
private static void ParseStartupHook(ref StartupHookNameOrPath startupHook, string startupHookPart)
8787
{
88-
ReadOnlySpan<char> disallowedSimpleAssemblyNameChars = stackalloc char[4]
89-
{
88+
ReadOnlySpan<char> disallowedSimpleAssemblyNameChars =
89+
[
9090
Path.DirectorySeparatorChar,
9191
Path.AltDirectorySeparatorChar,
9292
' ',
9393
','
94-
};
94+
];
9595

9696
if (string.IsNullOrEmpty(startupHookPart))
9797
{

src/libraries/System.Private.DataContractSerialization/src/System/Xml/XmlBufferReader.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -396,13 +396,13 @@ public decimal ReadDecimal()
396396
{
397397
byte[] buffer = GetBuffer(ValueHandleLength.Decimal, out int offset);
398398
ReadOnlySpan<byte> bytes = buffer.AsSpan(offset, sizeof(decimal));
399-
ReadOnlySpan<int> span = stackalloc int[4]
400-
{
399+
ReadOnlySpan<int> span =
400+
[
401401
BinaryPrimitives.ReadInt32LittleEndian(bytes.Slice(8, 4)),
402402
BinaryPrimitives.ReadInt32LittleEndian(bytes.Slice(12, 4)),
403403
BinaryPrimitives.ReadInt32LittleEndian(bytes.Slice(4, 4)),
404404
BinaryPrimitives.ReadInt32LittleEndian(bytes.Slice(0, 4))
405-
};
405+
];
406406

407407
Advance(ValueHandleLength.Decimal);
408408
return new decimal(span);
@@ -975,13 +975,13 @@ public decimal GetDecimal(int offset)
975975
else
976976
{
977977
ReadOnlySpan<byte> bytes = _buffer.AsSpan(offset, sizeof(decimal));
978-
ReadOnlySpan<int> span = stackalloc int[4]
979-
{
978+
ReadOnlySpan<int> span =
979+
[
980980
BinaryPrimitives.ReadInt32LittleEndian(bytes.Slice(8, 4)),
981981
BinaryPrimitives.ReadInt32LittleEndian(bytes.Slice(12, 4)),
982982
BinaryPrimitives.ReadInt32LittleEndian(bytes.Slice(4, 4)),
983983
BinaryPrimitives.ReadInt32LittleEndian(bytes.Slice(0, 4))
984-
};
984+
];
985985

986986
return new decimal(span);
987987
}

src/libraries/System.Security.Cryptography.Cose/src/System/Security/Cryptography/Cose/CoseHelpers.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ internal static void WriteByteStringLength(IncrementalHash hasher, ulong value)
4747
if (value < (byte)CborAdditionalInfo.Additional8BitData)
4848
{
4949
initialByte = new CborInitialByte(MajorType, (CborAdditionalInfo)value);
50-
hasher.AppendData(stackalloc byte[] { initialByte.InitialByte });
50+
hasher.AppendData([initialByte.InitialByte]);
5151
}
5252
else if (value <= byte.MaxValue)
5353
{
5454
initialByte = new CborInitialByte(MajorType, CborAdditionalInfo.Additional8BitData);
55-
hasher.AppendData(stackalloc byte[] { initialByte.InitialByte, (byte)value });
55+
hasher.AppendData([initialByte.InitialByte, (byte)value]);
5656
}
5757
else if (value <= ushort.MaxValue)
5858
{

src/libraries/System.Security.Cryptography.Pkcs/src/System/Security/Cryptography/Pkcs/CmsSignature.RSA.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,7 @@ protected override RSASignaturePadding GetSignaturePadding(
186186
return RSASignaturePadding.Pkcs1;
187187
}
188188

189-
Span<byte> expectedParameters = stackalloc byte[2];
190-
expectedParameters[0] = 0x05;
191-
expectedParameters[1] = 0x00;
189+
ReadOnlySpan<byte> expectedParameters = [0x05, 0x00];
192190

193191
if (expectedParameters.SequenceEqual(signatureParameters.Value.Span))
194192
{

src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/Pbkdf2Implementation.Windows.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ private static unsafe void FillKeyDerivation(
6060
if (password.IsEmpty)
6161
{
6262
// CNG won't accept a null pointer for the password.
63-
symmetricKeyMaterial = stackalloc byte[1];
64-
symmetricKeyMaterialLength = 0;
63+
symmetricKeyMaterial = [0];
6564
clearSpan = default;
6665
}
6766
else if (password.Length <= hashBlockSizeBytes)

src/libraries/System.Text.Encodings.Web/src/System/Text/Encodings/Web/DefaultJavaScriptEncoder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ private DefaultJavaScriptEncoder(TextEncoderSettings settings, bool allowMinimal
3131

3232
_innerEncoder = allowMinimalJsonEscaping
3333
? new OptimizedInboxTextEncoder(EscaperImplementation.SingletonMinimallyEscaped, settings.GetAllowedCodePointsBitmap(), forbidHtmlSensitiveCharacters: false,
34-
extraCharactersToEscape: stackalloc char[] { '\"', '\\' })
34+
extraCharactersToEscape: ['\"', '\\'])
3535
: new OptimizedInboxTextEncoder(EscaperImplementation.Singleton, settings.GetAllowedCodePointsBitmap(), forbidHtmlSensitiveCharacters: true,
36-
extraCharactersToEscape: stackalloc char[] { '\\', '`' });
36+
extraCharactersToEscape: ['\\', '`']);
3737
}
3838

3939
public override int MaxOutputCharactersPerInputCharacter => 6; // "\uXXXX" for a single char ("\uXXXX\uYYYY" [12 chars] for supplementary scalar value)

0 commit comments

Comments
 (0)