Skip to content

Commit

Permalink
Replace some stackallocs with collection expressions (#93126)
Browse files Browse the repository at this point in the history
These will result in using an InlineArray rather than a localloc
  • Loading branch information
stephentoub authored Oct 6, 2023
1 parent ffd90b5 commit 110d84d
Show file tree
Hide file tree
Showing 17 changed files with 35 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ internal static unsafe int[] ListAllPids()
/// <param name="pid">The PID of the process</param>
public static unsafe string GetProcPath(int pid)
{
Span<int> sysctlName = stackalloc int[] { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, pid };
Span<int> sysctlName = [CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, pid];
byte* pBuffer = null;
int bytesLength = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ public override string ToString()
AssertValid();

// Make local copy of data to avoid modifying input.
Span<uint> rgulNumeric = stackalloc uint[4] { _data1, _data2, _data3, _data4 };
Span<uint> rgulNumeric = [_data1, _data2, _data3, _data4];
int culLen = _bLen;
Span<char> pszTmp = stackalloc char[s_NUMERIC_MAX_PRECISION + 1]; //Local Character buffer to hold
pszTmp.Clear(); //the decimal digits, from the
Expand Down Expand Up @@ -1281,8 +1281,8 @@ public static explicit operator decimal(SqlDecimal x)
culOp1 = x._bLen;
culOp2 = y._bLen;

Span<uint> rglData1 = stackalloc uint[4] { x._data1, x._data2, x._data3, x._data4 };
Span<uint> rglData2 = stackalloc uint[4] { y._data1, y._data2, y._data3, y._data4 };
Span<uint> rglData1 = [x._data1, x._data2, x._data3, x._data4];
Span<uint> rglData2 = [y._data1, y._data2, y._data3, y._data4];

if (fOpSignPos)
{
Expand Down Expand Up @@ -1467,8 +1467,8 @@ public static explicit operator decimal(SqlDecimal x)

// II) Perform multiplication

ReadOnlySpan<uint> rglData1 = stackalloc uint[4] { x._data1, x._data2, x._data3, x._data4 };
ReadOnlySpan<uint> rglData2 = stackalloc uint[4] { y._data1, y._data2, y._data3, y._data4 };
ReadOnlySpan<uint> rglData1 = [x._data1, x._data2, x._data3, x._data4];
ReadOnlySpan<uint> rglData2 = [y._data1, y._data2, y._data3, y._data4];

//Local buffer to hold the result of multiplication.
//Longer than CReNumeBuf because full precision of multiplication is carried out
Expand Down Expand Up @@ -1701,8 +1701,8 @@ public static explicit operator decimal(SqlDecimal x)

// Step2: Actual Computation

Span<uint> rgulData1 = stackalloc uint[4] { x._data1, x._data2, x._data3, x._data4 };
Span<uint> rgulData2 = stackalloc uint[4] { y._data1, y._data2, y._data3, y._data4 };
Span<uint> rgulData1 = [x._data1, x._data2, x._data3, x._data4];
Span<uint> rgulData2 = [y._data1, y._data2, y._data3, y._data4];

// Buffers for arbitrary precision divide
Span<uint> rgulR = stackalloc uint[s_cNumeMax + 1];
Expand Down Expand Up @@ -1809,7 +1809,7 @@ private void AssertValid()
Debug.Assert(CLenFromPrec(_bPrec) >= _bLen, "CLenFromPrec(m_bPrec) >= m_bLen", "In AssertValid");
Debug.Assert(_bLen <= s_cNumeMax, "m_bLen <= x_cNumeMax", "In AssertValid");

ReadOnlySpan<uint> rglData = stackalloc uint[4] { _data1, _data2, _data3, _data4 };
ReadOnlySpan<uint> rglData = [_data1, _data2, _data3, _data4];

// highest UI4 is non-0 unless value "zero"
if (rglData[_bLen - 1] == 0)
Expand Down Expand Up @@ -2080,7 +2080,7 @@ private byte BActualPrec()
}
else
{
Span<uint> rgulU = stackalloc uint[4] { _data1, _data2, _data3, _data4 };
Span<uint> rgulU = [_data1, _data2, _data3, _data4];
Prec = 0;
do
{
Expand Down Expand Up @@ -2111,7 +2111,7 @@ private void AddULong(uint ulAdd)
int iData; // which UI4 in this we are on
int iDataMax = _bLen; // # of UI4s in this

Span<uint> rguiData = stackalloc uint[4] { _data1, _data2, _data3, _data4 };
Span<uint> rguiData = [_data1, _data2, _data3, _data4];

// Add, starting at the LS UI4 until out of UI4s or no carry
iData = 0;
Expand Down Expand Up @@ -2156,7 +2156,7 @@ private void MultByULong(uint uiMultiplier)
ulong dwlNextAccum = 0; // accumulation past dwlAccum
int iData; // which UI4 in *This we are on.

Span<uint> rguiData = stackalloc uint[4] { _data1, _data2, _data3, _data4 };
Span<uint> rguiData = [_data1, _data2, _data3, _data4];

for (iData = 0; iData < iDataMax; iData++)
{
Expand Down Expand Up @@ -2216,7 +2216,7 @@ private uint DivByULong(uint iDivisor)
throw new DivideByZeroException(SQLResource.DivideByZeroMessage);

// Copy into array, so that we can iterate through the data
Span<uint> rguiData = stackalloc uint[4] { _data1, _data2, _data3, _data4 };
Span<uint> rguiData = [_data1, _data2, _data3, _data4];

// Start from the MS UI4 of quotient, divide by divisor, placing result
// in quotient and carrying the remainder.
Expand Down Expand Up @@ -2426,8 +2426,8 @@ private int LAbsCmp(SqlDecimal snumOp)
if (culOp != culThis)
return (culThis > culOp) ? 1 : -1;

ReadOnlySpan<uint> rglData1 = stackalloc uint[4] { _data1, _data2, _data3, _data4 };
ReadOnlySpan<uint> rglData2 = stackalloc uint[4] { snumOp._data1, snumOp._data2, snumOp._data3, snumOp._data4 };
ReadOnlySpan<uint> rglData1 = [_data1, _data2, _data3, _data4];
ReadOnlySpan<uint> rglData2 = [snumOp._data1, snumOp._data2, snumOp._data3, snumOp._data4];

// Loop through numeric value checking each byte for differences.
iData = culOp - 1;
Expand Down
12 changes: 6 additions & 6 deletions src/libraries/System.Private.CoreLib/src/System/Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public static unsafe void Copy(Array sourceArray, int sourceIndex, Array destina
if (Rank != indices.Length)
ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RankIndices);

return InternalGetValue(GetFlattenedIndex(new ReadOnlySpan<int>(indices)));
return InternalGetValue(GetFlattenedIndex(indices));
}

public object? GetValue(int index)
Expand All @@ -323,15 +323,15 @@ public static unsafe void Copy(Array sourceArray, int sourceIndex, Array destina
if (Rank != 2)
ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_Need2DArray);

return InternalGetValue(GetFlattenedIndex(stackalloc int[] { index1, index2 }));
return InternalGetValue(GetFlattenedIndex([index1, index2]));
}

public object? GetValue(int index1, int index2, int index3)
{
if (Rank != 3)
ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_Need3DArray);

return InternalGetValue(GetFlattenedIndex(stackalloc int[] { index1, index2, index3 }));
return InternalGetValue(GetFlattenedIndex([index1, index2, index3]));
}

public void SetValue(object? value, int index)
Expand All @@ -347,15 +347,15 @@ public void SetValue(object? value, int index1, int index2)
if (Rank != 2)
ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_Need2DArray);

InternalSetValue(value, GetFlattenedIndex(stackalloc int[] { index1, index2 }));
InternalSetValue(value, GetFlattenedIndex([index1, index2]));
}

public void SetValue(object? value, int index1, int index2, int index3)
{
if (Rank != 3)
ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_Need3DArray);

InternalSetValue(value, GetFlattenedIndex(stackalloc int[] { index1, index2, index3 }));
InternalSetValue(value, GetFlattenedIndex([index1, index2, index3]));
}

public void SetValue(object? value, params int[] indices)
Expand All @@ -365,7 +365,7 @@ public void SetValue(object? value, params int[] indices)
if (Rank != indices.Length)
ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RankIndices);

InternalSetValue(value, GetFlattenedIndex(new ReadOnlySpan<int>(indices)));
InternalSetValue(value, GetFlattenedIndex(indices));
}

public object? GetValue(long index)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static string[] GetLogicalDrives()
int count = BitOperations.PopCount((uint)drives);

string[] result = new string[count];
Span<char> root = stackalloc char[] { 'A', ':', '\\' };
Span<char> root = ['A', ':', '\\'];
uint d = (uint)drives;
count = 0;
while (d != 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ private static void SurrogateToUpperNLS(char h, char l, out char hr, out char lr
Debug.Assert(char.IsHighSurrogate(h));
Debug.Assert(char.IsLowSurrogate(l));

Span<char> chars = stackalloc char[] { h, l };
ReadOnlySpan<char> chars = [h, l];
Span<char> destination = stackalloc char[2];

int written = Ordinal.ToUpperOrdinal(chars, destination);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,7 @@ public override void WriteString(string? text)

public override void WriteSurrogateCharEntity(char lowChar, char highChar)
{
WriteString(new string(stackalloc char[2] { highChar, lowChar }));
WriteString(new string([highChar, lowChar]));
}

public override void WriteValue(bool value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -865,8 +865,7 @@ private string GetCharText()
if (ch > char.MaxValue)
{
SurrogateChar surrogate = new SurrogateChar(ch);
Span<char> chars = stackalloc char[2] { surrogate.HighChar, surrogate.LowChar };
return new string(chars);
return new string([surrogate.HighChar, surrogate.LowChar]);
}

return ((char)ch).ToString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1126,8 +1126,7 @@ public override void WriteSurrogateCharEntity(char lowChar, char highChar)

if (_attributeValue != null)
{
Span<char> chars = stackalloc char[2] { highChar, lowChar };
WriteAttributeText(new string(chars));
WriteAttributeText(new string([highChar, lowChar]));
}

if (!_isXmlnsAttribute)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ public override void WriteCharEntity(int ch)
if (ch > char.MaxValue)
{
SurrogateChar sch = new SurrogateChar(ch);
WriteTextImpl(stackalloc char[] { sch.HighChar, sch.LowChar });
WriteTextImpl([sch.HighChar, sch.LowChar]);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,7 @@ public override void WriteString(string? text)

public override void WriteSurrogateCharEntity(char lowCh, char highCh)
{
ReadOnlySpan<char> entity = stackalloc char[] { highCh, lowCh };
AddString(new string(entity));
AddString(new string([highCh, lowCh]));
}

public override void WriteValue(DateTimeOffset value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,7 @@ public override void WriteCharEntity(char ch)
/// </summary>
public override void WriteSurrogateCharEntity(char lowChar, char highChar)
{
ReadOnlySpan<char> chars = stackalloc char[] { highChar, lowChar };
WriteString(new string(chars), TextBlockType.Text);
WriteString(new string([highChar, lowChar]), TextBlockType.Text);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@ public override void WriteCharEntity(char ch)
// Forward call to WriteString(string).
public override void WriteSurrogateCharEntity(char lowChar, char highChar)
{
ReadOnlySpan<char> entity = stackalloc char[] { lowChar, highChar };
WriteString(new string(entity));
WriteString(new string([lowChar, highChar]));
}

// Forward call to WriteString(string).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ public override Task WriteCharEntityAsync(char ch)
// Forward call to WriteString(string).
public override Task WriteSurrogateCharEntityAsync(char lowChar, char highChar)
{
ReadOnlySpan<char> entity = stackalloc char[] { lowChar, highChar };
return WriteStringAsync(new string(entity));
return WriteStringAsync(new string([lowChar, highChar]));
}

// Forward call to WriteString(string).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,7 @@ public override void WriteString(string? text)

public override void WriteSurrogateCharEntity(char lowCh, char highCh)
{
ReadOnlySpan<char> entity = stackalloc char[] { highCh, lowCh };
WriteString(new string(entity));
WriteString(new string([highCh, lowCh]));
}

public override void WriteChars(char[] buffer, int index, int count)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ internal static string[] BuildCharExceptionArgs(char invChar, char nextChar)
if (XmlCharType.IsHighSurrogate(invChar) && nextChar != 0)
{
int combinedChar = XmlCharType.CombineSurrogateChar(nextChar, invChar);
aStringList[0] = new string(stackalloc char[] { invChar, nextChar });
aStringList[0] = new string([invChar, nextChar]);
aStringList[1] = $"0x{combinedChar:X2}";
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ internal unsafe SP800108HmacCounterKdfImplementationCng(ReadOnlySpan<byte> key,
else
{
// CNG requires a non-null pointer even when the length is zero.
symmetricKeyMaterial = stackalloc byte[] { 0 };
symmetricKeyMaterial = [0];
symmetricKeyMaterialLength = 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ internal static void DeriveBytesOneShot(
{
Span<byte> iBuffer = stackalloc byte[sizeof(uint)];
Span<byte> lBuffer = stackalloc byte[sizeof(uint)];
ReadOnlySpan<byte> zero = stackalloc byte[] { 0 };
ReadOnlySpan<byte> zero = [0];
Span<byte> hmacBuffer = stackalloc byte[512 / 8]; // Largest HMAC supported is SHA512
int hmacBufferWritten = 0;

Expand Down

0 comments on commit 110d84d

Please sign in to comment.