Skip to content

Commit 2897f5e

Browse files
authored
Use Roslyn support for RuntimeHelpers.CreateSpan (or field caching downlevel) (#79461)
1 parent eb76787 commit 2897f5e

File tree

48 files changed

+424
-469
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+424
-469
lines changed

src/libraries/Common/src/System/Security/Cryptography/Asn1/Rc2CbcParameters.manual.cs

+8-18
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace System.Security.Cryptography.Asn1
1212
// smallest supported by .NET that's not really a problem.
1313
internal partial struct Rc2CbcParameters
1414
{
15-
private static readonly byte[] s_rc2EkbEncoding =
15+
private static ReadOnlySpan<byte> Rc2EkbEncoding => new byte[]
1616
{
1717
0xbd, 0x56, 0xea, 0xf2, 0xa2, 0xf1, 0xac, 0x2a, 0xb0, 0x93, 0xd1, 0x9c, 0x1b, 0x33, 0xfd, 0xd0,
1818
0x30, 0x04, 0xb6, 0xdc, 0x7d, 0xdf, 0x32, 0x4b, 0xf7, 0xcb, 0x45, 0x9b, 0x31, 0xbb, 0x21, 0x5a,
@@ -34,26 +34,16 @@ internal partial struct Rc2CbcParameters
3434

3535
internal Rc2CbcParameters(ReadOnlyMemory<byte> iv, int keySize)
3636
{
37-
if (keySize > byte.MaxValue)
38-
{
39-
Rc2Version = keySize;
40-
}
41-
else
42-
{
43-
Rc2Version = s_rc2EkbEncoding[keySize];
44-
}
37+
Rc2Version = keySize > byte.MaxValue ?
38+
keySize :
39+
Rc2EkbEncoding[keySize];
4540

4641
Iv = iv;
4742
}
4843

49-
internal int GetEffectiveKeyBits()
50-
{
51-
if (Rc2Version > byte.MaxValue)
52-
{
53-
return Rc2Version;
54-
}
55-
56-
return Array.IndexOf(s_rc2EkbEncoding, (byte)Rc2Version);
57-
}
44+
internal int GetEffectiveKeyBits() =>
45+
Rc2Version > byte.MaxValue ?
46+
Rc2Version :
47+
Rc2EkbEncoding.IndexOf((byte)Rc2Version);
5848
}
5949
}

src/libraries/Common/src/System/Security/IdentityHelper.cs

+9-16
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,6 @@ namespace System.Security
1515
/// </summary>
1616
internal static class IdentityHelper
1717
{
18-
private static readonly char[] s_base32Char =
19-
{
20-
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
21-
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
22-
'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
23-
'y', 'z', '0', '1', '2', '3', '4', '5'
24-
};
25-
2618
/// <summary>
2719
/// Gives a hash equivalent to what Url.Normalize() gives.
2820
/// </summary>
@@ -95,6 +87,7 @@ internal static string ToBase32StringSuitableForDirName(byte[] buff)
9587
// Create l chars using the last 5 bits of each byte.
9688
// Consume 3 MSB bits 5 bytes at a time.
9789

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

10699
// Consume the 5 Least significant bits of each byte
107-
sb.Append(s_base32Char[b0 & 0x1F]);
108-
sb.Append(s_base32Char[b1 & 0x1F]);
109-
sb.Append(s_base32Char[b2 & 0x1F]);
110-
sb.Append(s_base32Char[b3 & 0x1F]);
111-
sb.Append(s_base32Char[b4 & 0x1F]);
100+
sb.Append((char)base32Chars[b0 & 0x1F]);
101+
sb.Append((char)base32Chars[b1 & 0x1F]);
102+
sb.Append((char)base32Chars[b2 & 0x1F]);
103+
sb.Append((char)base32Chars[b3 & 0x1F]);
104+
sb.Append((char)base32Chars[b4 & 0x1F]);
112105

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

118-
sb.Append(s_base32Char[(
111+
sb.Append((char)base32Chars[(
119112
((b1 & 0xE0) >> 5) |
120113
((b4 & 0x60) >> 2))]);
121114

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

133-
sb.Append(s_base32Char[b2]);
126+
sb.Append((char)base32Chars[b2]);
134127

135128
} while (i < l);
136129

src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLDateTime.cs

+9-12
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ public struct SqlDateTime : INullable, IComparable, IXmlSerializable, IEquatable
3636
public static readonly int SQLTicksPerHour = SQLTicksPerMinute * 60;
3737
private static readonly int s_SQLTicksPerDay = SQLTicksPerHour * 24;
3838

39-
private const long s_ticksPerSecond = TimeSpan.TicksPerMillisecond * 1000;
40-
4139
private static readonly DateTime s_SQLBaseDate = new DateTime(1900, 1, 1);
4240
private static readonly long s_SQLBaseDateTicks = s_SQLBaseDate.Ticks;
4341

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

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

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

60-
private static readonly DateTime s_minDateTime = new DateTime(1753, 1, 1);
61-
private static readonly DateTime s_maxDateTime = DateTime.MaxValue;
62-
private static readonly TimeSpan s_minTimeSpan = s_minDateTime.Subtract(s_SQLBaseDate);
63-
private static readonly TimeSpan s_maxTimeSpan = s_maxDateTime.Subtract(s_SQLBaseDate);
64-
private const string s_ISO8601_DateTimeFormat = "yyyy-MM-ddTHH:mm:ss.fff";
57+
private static readonly TimeSpan s_minTimeSpan = new DateTime(1753, 1, 1).Subtract(s_SQLBaseDate);
58+
private static readonly TimeSpan s_maxTimeSpan = DateTime.MaxValue.Subtract(s_SQLBaseDate);
59+
private const string ISO8601_DateTimeFormat = "yyyy-MM-ddTHH:mm:ss.fff";
6560

6661
// These formats are valid styles in SQL Server (style 9, 12, 13, 14)
6762
// but couldn't be recognized by the default parse. Needs to call
@@ -105,7 +100,9 @@ public SqlDateTime(int year, int month, int day, int hour, int minute, int secon
105100
{
106101
if (year >= s_minYear && year <= s_maxYear && month >= 1 && month <= 12)
107102
{
108-
int[] days = IsLeapYear(year) ? s_daysToMonth366 : s_daysToMonth365;
103+
ReadOnlySpan<int> days = IsLeapYear(year) ?
104+
DaysToMonth366 :
105+
DaysToMonth365;
109106
if (day >= 1 && day <= days[month] - days[month - 1])
110107
{
111108
int y = year - 1;
@@ -670,7 +667,7 @@ void IXmlSerializable.WriteXml(XmlWriter writer)
670667
}
671668
else
672669
{
673-
writer.WriteString(XmlConvert.ToString(Value, s_ISO8601_DateTimeFormat));
670+
writer.WriteString(XmlConvert.ToString(Value, ISO8601_DateTimeFormat));
674671
}
675672
}
676673

src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLDecimal.cs

+31-31
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public struct SqlDecimal : INullable, IComparable, IXmlSerializable, IEquatable<
7171
private const byte s_cNumeDivScaleMin = 6; // Minimum result scale of numeric division
7272

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

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

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

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

256-
private static readonly uint[] s_decimalHelpersHiHi = {
256+
private static ReadOnlySpan<uint> DecimalHelpersHiHi => new uint[] {
257257
0x00000000, // precision:2, value:10
258258
0x00000000, // precision:3, value:100
259259
0x00000000, // precision:4, value:1000
@@ -313,31 +313,31 @@ private byte CalculatePrecision()
313313
{
314314
int tableIndex;
315315
byte precision;
316-
uint[] decimalHelpers;
316+
ReadOnlySpan<uint> decimalHelpers;
317317
uint decimalPart;
318318

319319
if (_data4 != 0)
320320
{
321321
tableIndex = HelperTableStartIndexHiHi;
322-
decimalHelpers = s_decimalHelpersHiHi;
322+
decimalHelpers = DecimalHelpersHiHi;
323323
decimalPart = _data4;
324324
}
325325
else if (_data3 != 0)
326326
{
327327
tableIndex = HelperTableStartIndexHi;
328-
decimalHelpers = s_decimalHelpersHi;
328+
decimalHelpers = DecimalHelpersHi;
329329
decimalPart = _data3;
330330
}
331331
else if (_data2 != 0)
332332
{
333333
tableIndex = HelperTableStartIndexMid;
334-
decimalHelpers = s_decimalHelpersMid;
334+
decimalHelpers = DecimalHelpersMid;
335335
decimalPart = _data2;
336336
}
337337
else
338338
{
339339
tableIndex = HelperTableStartIndexLo;
340-
decimalHelpers = s_decimalHelpersLo;
340+
decimalHelpers = DecimalHelpersLo;
341341
decimalPart = _data1;
342342
}
343343

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

431431
int tableIndex = checked((precision - 1));
432-
if (_data4 < s_decimalHelpersHiHi[tableIndex])
432+
if (_data4 < DecimalHelpersHiHi[tableIndex])
433433
{
434434
return true;
435435
}
436-
else if (_data4 == s_decimalHelpersHiHi[tableIndex])
436+
else if (_data4 == DecimalHelpersHiHi[tableIndex])
437437
{
438-
if (_data3 < s_decimalHelpersHi[tableIndex])
438+
if (_data3 < DecimalHelpersHi[tableIndex])
439439
{
440440
return true;
441441
}
442-
else if (_data3 == s_decimalHelpersHi[tableIndex])
442+
else if (_data3 == DecimalHelpersHi[tableIndex])
443443
{
444-
if (_data2 < s_decimalHelpersMid[tableIndex])
444+
if (_data2 < DecimalHelpersMid[tableIndex])
445445
{
446446
return true;
447447
}
448-
else if (_data2 == s_decimalHelpersMid[tableIndex])
448+
else if (_data2 == DecimalHelpersMid[tableIndex])
449449
{
450-
if (_data1 < s_decimalHelpersLo[tableIndex])
450+
if (_data1 < DecimalHelpersLo[tableIndex])
451451
{
452452
return true;
453453
}
@@ -754,9 +754,9 @@ public SqlDecimal(double dVal) : this(false)
754754
{
755755
ulLenDelta = (ulLen >= 9) ? 9 : ulLen;
756756

757-
dFrac *= s_rgulShiftBase[(int)ulLenDelta - 1];
757+
dFrac *= RgulShiftBase[(int)ulLenDelta - 1];
758758
ulLen -= ulLenDelta;
759-
MultByULong(s_rgulShiftBase[(int)ulLenDelta - 1]);
759+
MultByULong(RgulShiftBase[(int)ulLenDelta - 1]);
760760
AddULong((uint)dFrac);
761761
dFrac -= Math.Floor(dFrac);
762762
}
@@ -1542,12 +1542,12 @@ public static explicit operator decimal(SqlDecimal x)
15421542
{
15431543
if (lScaleAdjust <= -9)
15441544
{
1545-
ulShiftBase = s_rgulShiftBase[8];
1545+
ulShiftBase = RgulShiftBase[8];
15461546
lScaleAdjust += 9;
15471547
}
15481548
else
15491549
{
1550-
ulShiftBase = s_rgulShiftBase[-lScaleAdjust - 1];
1550+
ulShiftBase = RgulShiftBase[-lScaleAdjust - 1];
15511551
lScaleAdjust = 0;
15521552
}
15531553
MpDiv1(rgulRes, ref culRes, ulShiftBase, out ulRem);
@@ -2304,12 +2304,12 @@ internal void AdjustScale(int digits, bool fRound)
23042304
//if lAdjust>=9, downshift by 10^9 each time, otherwise by the full amount
23052305
if (lAdjust >= 9)
23062306
{
2307-
ulShiftBase = s_rgulShiftBase[8];
2307+
ulShiftBase = RgulShiftBase[8];
23082308
lAdjust -= 9;
23092309
}
23102310
else
23112311
{
2312-
ulShiftBase = s_rgulShiftBase[lAdjust - 1];
2312+
ulShiftBase = RgulShiftBase[lAdjust - 1];
23132313
lAdjust = 0;
23142314
}
23152315
MultByULong(ulShiftBase);
@@ -2321,12 +2321,12 @@ internal void AdjustScale(int digits, bool fRound)
23212321
{
23222322
if (lAdjust <= -9)
23232323
{
2324-
ulShiftBase = s_rgulShiftBase[8];
2324+
ulShiftBase = RgulShiftBase[8];
23252325
lAdjust += 9;
23262326
}
23272327
else
23282328
{
2329-
ulShiftBase = s_rgulShiftBase[-lAdjust - 1];
2329+
ulShiftBase = RgulShiftBase[-lAdjust - 1];
23302330
lAdjust = 0;
23312331
}
23322332
ulRem = DivByULong(ulShiftBase);
@@ -3051,12 +3051,12 @@ private void MakeInteger(out bool fFraction)
30513051
{
30523052
if (iAdjust >= 9)
30533053
{
3054-
ulRem = DivByULong(s_rgulShiftBase[8]);
3054+
ulRem = DivByULong(RgulShiftBase[8]);
30553055
iAdjust -= 9;
30563056
}
30573057
else
30583058
{
3059-
ulRem = DivByULong(s_rgulShiftBase[iAdjust - 1]);
3059+
ulRem = DivByULong(RgulShiftBase[iAdjust - 1]);
30603060
iAdjust = 0;
30613061
}
30623062

@@ -3189,14 +3189,14 @@ private static SqlDecimal Round(SqlDecimal n, int lPosition, bool fTruncate)
31893189
{
31903190
if (lAdjust >= 9)
31913191
{
3192-
ulRem = n.DivByULong(s_rgulShiftBase[8]);
3193-
ulLastDivBase = s_rgulShiftBase[8];
3192+
ulRem = n.DivByULong(RgulShiftBase[8]);
3193+
ulLastDivBase = RgulShiftBase[8];
31943194
lAdjust -= 9;
31953195
}
31963196
else
31973197
{
3198-
ulRem = n.DivByULong(s_rgulShiftBase[lAdjust - 1]);
3199-
ulLastDivBase = s_rgulShiftBase[lAdjust - 1];
3198+
ulRem = n.DivByULong(RgulShiftBase[lAdjust - 1]);
3199+
ulLastDivBase = RgulShiftBase[lAdjust - 1];
32003200
lAdjust = 0;
32013201
}
32023202
}

0 commit comments

Comments
 (0)