Skip to content

Commit

Permalink
Use Roslyn support for RuntimeHelpers.CreateSpan (or field caching do…
Browse files Browse the repository at this point in the history
…wnlevel) (#79461)
  • Loading branch information
stephentoub authored Jan 13, 2023
1 parent eb76787 commit 2897f5e
Show file tree
Hide file tree
Showing 48 changed files with 424 additions and 469 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace System.Security.Cryptography.Asn1
// smallest supported by .NET that's not really a problem.
internal partial struct Rc2CbcParameters
{
private static readonly byte[] s_rc2EkbEncoding =
private static ReadOnlySpan<byte> Rc2EkbEncoding => new byte[]
{
0xbd, 0x56, 0xea, 0xf2, 0xa2, 0xf1, 0xac, 0x2a, 0xb0, 0x93, 0xd1, 0x9c, 0x1b, 0x33, 0xfd, 0xd0,
0x30, 0x04, 0xb6, 0xdc, 0x7d, 0xdf, 0x32, 0x4b, 0xf7, 0xcb, 0x45, 0x9b, 0x31, 0xbb, 0x21, 0x5a,
Expand All @@ -34,26 +34,16 @@ internal partial struct Rc2CbcParameters

internal Rc2CbcParameters(ReadOnlyMemory<byte> iv, int keySize)
{
if (keySize > byte.MaxValue)
{
Rc2Version = keySize;
}
else
{
Rc2Version = s_rc2EkbEncoding[keySize];
}
Rc2Version = keySize > byte.MaxValue ?
keySize :
Rc2EkbEncoding[keySize];

Iv = iv;
}

internal int GetEffectiveKeyBits()
{
if (Rc2Version > byte.MaxValue)
{
return Rc2Version;
}

return Array.IndexOf(s_rc2EkbEncoding, (byte)Rc2Version);
}
internal int GetEffectiveKeyBits() =>
Rc2Version > byte.MaxValue ?
Rc2Version :
Rc2EkbEncoding.IndexOf((byte)Rc2Version);
}
}
25 changes: 9 additions & 16 deletions src/libraries/Common/src/System/Security/IdentityHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@ namespace System.Security
/// </summary>
internal static class IdentityHelper
{
private static readonly char[] s_base32Char =
{
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z', '0', '1', '2', '3', '4', '5'
};

/// <summary>
/// Gives a hash equivalent to what Url.Normalize() gives.
/// </summary>
Expand Down Expand Up @@ -95,6 +87,7 @@ internal static string ToBase32StringSuitableForDirName(byte[] buff)
// Create l chars using the last 5 bits of each byte.
// Consume 3 MSB bits 5 bytes at a time.

ReadOnlySpan<byte> base32Chars = "abcdefghijklmnopqrstuvwxyz012345"u8;
do
{
byte b0 = (i < l) ? buff[i++] : (byte)0;
Expand All @@ -104,18 +97,18 @@ internal static string ToBase32StringSuitableForDirName(byte[] buff)
byte b4 = (i < l) ? buff[i++] : (byte)0;

// Consume the 5 Least significant bits of each byte
sb.Append(s_base32Char[b0 & 0x1F]);
sb.Append(s_base32Char[b1 & 0x1F]);
sb.Append(s_base32Char[b2 & 0x1F]);
sb.Append(s_base32Char[b3 & 0x1F]);
sb.Append(s_base32Char[b4 & 0x1F]);
sb.Append((char)base32Chars[b0 & 0x1F]);
sb.Append((char)base32Chars[b1 & 0x1F]);
sb.Append((char)base32Chars[b2 & 0x1F]);
sb.Append((char)base32Chars[b3 & 0x1F]);
sb.Append((char)base32Chars[b4 & 0x1F]);

// Consume 3 MSB of b0, b1, MSB bits 6, 7 of b3, b4
sb.Append(s_base32Char[(
sb.Append((char)base32Chars[(
((b0 & 0xE0) >> 5) |
((b3 & 0x60) >> 2))]);

sb.Append(s_base32Char[(
sb.Append((char)base32Chars[(
((b1 & 0xE0) >> 5) |
((b4 & 0x60) >> 2))]);

Expand All @@ -130,7 +123,7 @@ internal static string ToBase32StringSuitableForDirName(byte[] buff)
if ((b4 & 0x80) != 0)
b2 |= 0x10;

sb.Append(s_base32Char[b2]);
sb.Append((char)base32Chars[b2]);

} while (i < l);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ public struct SqlDateTime : INullable, IComparable, IXmlSerializable, IEquatable
public static readonly int SQLTicksPerHour = SQLTicksPerMinute * 60;
private static readonly int s_SQLTicksPerDay = SQLTicksPerHour * 24;

private const long s_ticksPerSecond = TimeSpan.TicksPerMillisecond * 1000;

private static readonly DateTime s_SQLBaseDate = new DateTime(1900, 1, 1);
private static readonly long s_SQLBaseDateTicks = s_SQLBaseDate.Ticks;

Expand All @@ -51,17 +49,14 @@ public struct SqlDateTime : INullable, IComparable, IXmlSerializable, IEquatable

private const int s_dayBase = 693595; // Jan 1 1900 is this many days from Jan 1 0001


private static readonly int[] s_daysToMonth365 = new int[] {
private static ReadOnlySpan<int> DaysToMonth365 => new int[] {
0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
private static readonly int[] s_daysToMonth366 = new int[] {
private static ReadOnlySpan<int> DaysToMonth366 => new int[] {
0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366};

private static readonly DateTime s_minDateTime = new DateTime(1753, 1, 1);
private static readonly DateTime s_maxDateTime = DateTime.MaxValue;
private static readonly TimeSpan s_minTimeSpan = s_minDateTime.Subtract(s_SQLBaseDate);
private static readonly TimeSpan s_maxTimeSpan = s_maxDateTime.Subtract(s_SQLBaseDate);
private const string s_ISO8601_DateTimeFormat = "yyyy-MM-ddTHH:mm:ss.fff";
private static readonly TimeSpan s_minTimeSpan = new DateTime(1753, 1, 1).Subtract(s_SQLBaseDate);
private static readonly TimeSpan s_maxTimeSpan = DateTime.MaxValue.Subtract(s_SQLBaseDate);
private const string ISO8601_DateTimeFormat = "yyyy-MM-ddTHH:mm:ss.fff";

// These formats are valid styles in SQL Server (style 9, 12, 13, 14)
// but couldn't be recognized by the default parse. Needs to call
Expand Down Expand Up @@ -105,7 +100,9 @@ public SqlDateTime(int year, int month, int day, int hour, int minute, int secon
{
if (year >= s_minYear && year <= s_maxYear && month >= 1 && month <= 12)
{
int[] days = IsLeapYear(year) ? s_daysToMonth366 : s_daysToMonth365;
ReadOnlySpan<int> days = IsLeapYear(year) ?
DaysToMonth366 :
DaysToMonth365;
if (day >= 1 && day <= days[month] - days[month - 1])
{
int y = year - 1;
Expand Down Expand Up @@ -670,7 +667,7 @@ void IXmlSerializable.WriteXml(XmlWriter writer)
}
else
{
writer.WriteString(XmlConvert.ToString(Value, s_ISO8601_DateTimeFormat));
writer.WriteString(XmlConvert.ToString(Value, ISO8601_DateTimeFormat));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public struct SqlDecimal : INullable, IComparable, IXmlSerializable, IEquatable<
private const byte s_cNumeDivScaleMin = 6; // Minimum result scale of numeric division

// Array of multipliers for lAdjust and Ceiling/Floor.
private static readonly uint[] s_rgulShiftBase = new uint[9] {
private static ReadOnlySpan<uint> RgulShiftBase => new uint[9] {
10,
10 * 10,
10 * 10 * 10,
Expand Down Expand Up @@ -130,7 +130,7 @@ public static void CreateDecimalHelperTable()
#endregion

#region DecimalHelperTable
private static readonly uint[] s_decimalHelpersLo = {
private static ReadOnlySpan<uint> DecimalHelpersLo => new uint[] {
0x0000000a, // precision:2, value:10
0x00000064, // precision:3, value:100
0x000003e8, // precision:4, value:1000
Expand Down Expand Up @@ -171,7 +171,7 @@ public static void CreateDecimalHelperTable()
0x00000000, // precision:38+1, value:99999999999999999999999999999999999999+1
};

private static readonly uint[] s_decimalHelpersMid = {
private static ReadOnlySpan<uint> DecimalHelpersMid => new uint[] {
0x00000000, // precision:2, value:10
0x00000000, // precision:3, value:100
0x00000000, // precision:4, value:1000
Expand Down Expand Up @@ -212,7 +212,7 @@ public static void CreateDecimalHelperTable()
0x098a2240, // precision:38+1, value:99999999999999999999999999999999999999+1
};

private static readonly uint[] s_decimalHelpersHi = {
private static ReadOnlySpan<uint> DecimalHelpersHi => new uint[] {
0x00000000, // precision:2, value:10
0x00000000, // precision:3, value:100
0x00000000, // precision:4, value:1000
Expand Down Expand Up @@ -253,7 +253,7 @@ public static void CreateDecimalHelperTable()
0x5a86c47a, // precision:38+1, value:99999999999999999999999999999999999999+1
};

private static readonly uint[] s_decimalHelpersHiHi = {
private static ReadOnlySpan<uint> DecimalHelpersHiHi => new uint[] {
0x00000000, // precision:2, value:10
0x00000000, // precision:3, value:100
0x00000000, // precision:4, value:1000
Expand Down Expand Up @@ -313,31 +313,31 @@ private byte CalculatePrecision()
{
int tableIndex;
byte precision;
uint[] decimalHelpers;
ReadOnlySpan<uint> decimalHelpers;
uint decimalPart;

if (_data4 != 0)
{
tableIndex = HelperTableStartIndexHiHi;
decimalHelpers = s_decimalHelpersHiHi;
decimalHelpers = DecimalHelpersHiHi;
decimalPart = _data4;
}
else if (_data3 != 0)
{
tableIndex = HelperTableStartIndexHi;
decimalHelpers = s_decimalHelpersHi;
decimalHelpers = DecimalHelpersHi;
decimalPart = _data3;
}
else if (_data2 != 0)
{
tableIndex = HelperTableStartIndexMid;
decimalHelpers = s_decimalHelpersMid;
decimalHelpers = DecimalHelpersMid;
decimalPart = _data2;
}
else
{
tableIndex = HelperTableStartIndexLo;
decimalHelpers = s_decimalHelpersLo;
decimalHelpers = DecimalHelpersLo;
decimalPart = _data1;
}

Expand Down Expand Up @@ -429,25 +429,25 @@ private bool VerifyPrecision(byte precision)
Debug.Assert(precision <= MaxPrecision, "Precision > MaxPrecision");

int tableIndex = checked((precision - 1));
if (_data4 < s_decimalHelpersHiHi[tableIndex])
if (_data4 < DecimalHelpersHiHi[tableIndex])
{
return true;
}
else if (_data4 == s_decimalHelpersHiHi[tableIndex])
else if (_data4 == DecimalHelpersHiHi[tableIndex])
{
if (_data3 < s_decimalHelpersHi[tableIndex])
if (_data3 < DecimalHelpersHi[tableIndex])
{
return true;
}
else if (_data3 == s_decimalHelpersHi[tableIndex])
else if (_data3 == DecimalHelpersHi[tableIndex])
{
if (_data2 < s_decimalHelpersMid[tableIndex])
if (_data2 < DecimalHelpersMid[tableIndex])
{
return true;
}
else if (_data2 == s_decimalHelpersMid[tableIndex])
else if (_data2 == DecimalHelpersMid[tableIndex])
{
if (_data1 < s_decimalHelpersLo[tableIndex])
if (_data1 < DecimalHelpersLo[tableIndex])
{
return true;
}
Expand Down Expand Up @@ -754,9 +754,9 @@ public SqlDecimal(double dVal) : this(false)
{
ulLenDelta = (ulLen >= 9) ? 9 : ulLen;

dFrac *= s_rgulShiftBase[(int)ulLenDelta - 1];
dFrac *= RgulShiftBase[(int)ulLenDelta - 1];
ulLen -= ulLenDelta;
MultByULong(s_rgulShiftBase[(int)ulLenDelta - 1]);
MultByULong(RgulShiftBase[(int)ulLenDelta - 1]);
AddULong((uint)dFrac);
dFrac -= Math.Floor(dFrac);
}
Expand Down Expand Up @@ -1542,12 +1542,12 @@ public static explicit operator decimal(SqlDecimal x)
{
if (lScaleAdjust <= -9)
{
ulShiftBase = s_rgulShiftBase[8];
ulShiftBase = RgulShiftBase[8];
lScaleAdjust += 9;
}
else
{
ulShiftBase = s_rgulShiftBase[-lScaleAdjust - 1];
ulShiftBase = RgulShiftBase[-lScaleAdjust - 1];
lScaleAdjust = 0;
}
MpDiv1(rgulRes, ref culRes, ulShiftBase, out ulRem);
Expand Down Expand Up @@ -2304,12 +2304,12 @@ internal void AdjustScale(int digits, bool fRound)
//if lAdjust>=9, downshift by 10^9 each time, otherwise by the full amount
if (lAdjust >= 9)
{
ulShiftBase = s_rgulShiftBase[8];
ulShiftBase = RgulShiftBase[8];
lAdjust -= 9;
}
else
{
ulShiftBase = s_rgulShiftBase[lAdjust - 1];
ulShiftBase = RgulShiftBase[lAdjust - 1];
lAdjust = 0;
}
MultByULong(ulShiftBase);
Expand All @@ -2321,12 +2321,12 @@ internal void AdjustScale(int digits, bool fRound)
{
if (lAdjust <= -9)
{
ulShiftBase = s_rgulShiftBase[8];
ulShiftBase = RgulShiftBase[8];
lAdjust += 9;
}
else
{
ulShiftBase = s_rgulShiftBase[-lAdjust - 1];
ulShiftBase = RgulShiftBase[-lAdjust - 1];
lAdjust = 0;
}
ulRem = DivByULong(ulShiftBase);
Expand Down Expand Up @@ -3051,12 +3051,12 @@ private void MakeInteger(out bool fFraction)
{
if (iAdjust >= 9)
{
ulRem = DivByULong(s_rgulShiftBase[8]);
ulRem = DivByULong(RgulShiftBase[8]);
iAdjust -= 9;
}
else
{
ulRem = DivByULong(s_rgulShiftBase[iAdjust - 1]);
ulRem = DivByULong(RgulShiftBase[iAdjust - 1]);
iAdjust = 0;
}

Expand Down Expand Up @@ -3189,14 +3189,14 @@ private static SqlDecimal Round(SqlDecimal n, int lPosition, bool fTruncate)
{
if (lAdjust >= 9)
{
ulRem = n.DivByULong(s_rgulShiftBase[8]);
ulLastDivBase = s_rgulShiftBase[8];
ulRem = n.DivByULong(RgulShiftBase[8]);
ulLastDivBase = RgulShiftBase[8];
lAdjust -= 9;
}
else
{
ulRem = n.DivByULong(s_rgulShiftBase[lAdjust - 1]);
ulLastDivBase = s_rgulShiftBase[lAdjust - 1];
ulRem = n.DivByULong(RgulShiftBase[lAdjust - 1]);
ulLastDivBase = RgulShiftBase[lAdjust - 1];
lAdjust = 0;
}
}
Expand Down
Loading

0 comments on commit 2897f5e

Please sign in to comment.