Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ReadOnlySpan properties over array fields #56845

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Compilers/CSharp/Portable/Parser/QuickScanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,14 @@ private SyntaxToken QuickScanSyntaxToken()

//localize frequently accessed fields
var charWindow = TextWindow.CharacterWindow;
var charPropLength = s_charProperties.Length;
var charPropLength = CharProperties.Length;

for (; i < n; i++)
{
char c = charWindow[i];
int uc = unchecked((int)c);

var flags = uc < charPropLength ? (CharFlags)s_charProperties[uc] : CharFlags.Complex;
var flags = uc < charPropLength ? (CharFlags)CharProperties[uc] : CharFlags.Complex;

state = (QuickScanState)s_stateTransitions[(int)state, (int)flags];
// NOTE: that Bad > Done and it is the only state like that
Expand Down Expand Up @@ -273,7 +273,7 @@ private SyntaxToken CreateQuickToken()
// # is marked complex as it may start directives.
// PERF: Use byte instead of CharFlags so the compiler can use array literal initialization.
// The most natural type choice, Enum arrays, are not blittable due to a CLR limitation.
private static readonly byte[] s_charProperties = new[]
private static ReadOnlySpan<byte> CharProperties => new[]
jaredpar marked this conversation as resolved.
Show resolved Hide resolved
{
// 0 .. 31
(byte)CharFlags.Complex, (byte)CharFlags.Complex, (byte)CharFlags.Complex, (byte)CharFlags.Complex, (byte)CharFlags.Complex, (byte)CharFlags.Complex, (byte)CharFlags.Complex, (byte)CharFlags.Complex,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public void OperandTypes()
}
}

AssertEx.Equal(OneByteOperandTypes, InstructionOperandTypes.OneByte);
AssertEx.Equal(TwoByteOperandTypes, InstructionOperandTypes.TwoByte);
AssertEx.Equal(OneByteOperandTypes, InstructionOperandTypes.OneByte.ToArray());
AssertEx.Equal(TwoByteOperandTypes, InstructionOperandTypes.TwoByte.ToArray());
RikkiGibson marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Immutable;
using System.Reflection.Emit;

Expand All @@ -23,7 +24,7 @@ internal static OperandType ReadOperandType(ImmutableArray<byte> il, ref int pos
}

// internal for testing
internal static readonly byte[] OneByte = new byte[]
internal static ReadOnlySpan<byte> OneByte => new byte[]
jaredpar marked this conversation as resolved.
Show resolved Hide resolved
{
(byte)OperandType.InlineNone, // nop
(byte)OperandType.InlineNone, // break
Expand Down Expand Up @@ -283,7 +284,7 @@ internal static OperandType ReadOperandType(ImmutableArray<byte> il, ref int pos
};

// internal for testing
internal static readonly byte[] TwoByte = new byte[]
internal static ReadOnlySpan<byte> TwoByte => new byte[]
jaredpar marked this conversation as resolved.
Show resolved Hide resolved
{
(byte)OperandType.InlineNone, // arglist (0xfe 0x00)
(byte)OperandType.InlineNone, // ceq
Expand Down
23 changes: 12 additions & 11 deletions src/Compilers/Core/Portable/Xml/XmlCharType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// see instructions on how to do that below.
// #define XML10_FIFTH_EDITION

using System;
RikkiGibson marked this conversation as resolved.
Show resolved Hide resolved
using System.Diagnostics;

namespace Microsoft.CodeAnalysis
Expand Down Expand Up @@ -474,7 +475,7 @@ internal static class XmlCharType
0xd0, 0xd0, 0xd0, 0xd0, 0xd0, 0xd0, 0x00, 0x00,
};

private static byte charProperties(char i)
private static byte GetCharProperties(char i)
{
// The index entry, table, identifies the start of the appropriate 256-entry table within s_charProperties
byte table = s_charPropertiesIndex[i >> innerSizeBits];
Expand Down Expand Up @@ -910,7 +911,7 @@ private static byte charProperties(char i)

public static bool IsWhiteSpace(char ch)
{
return (charProperties(ch) & fWhitespace) != 0;
return (GetCharProperties(ch) & fWhitespace) != 0;
}

public static bool IsExtender(char ch)
Expand All @@ -920,7 +921,7 @@ public static bool IsExtender(char ch)

public static bool IsNCNameSingleChar(char ch)
{
return (charProperties(ch) & fNCNameSC) != 0;
return (GetCharProperties(ch) & fNCNameSC) != 0;
}

#if XML10_FIFTH_EDITION
Expand Down Expand Up @@ -955,7 +956,7 @@ public static bool IsNCNameLowSurrogateChar(char lowChar)

public static bool IsStartNCNameSingleChar(char ch)
{
return (charProperties(ch) & fNCStartNameSC) != 0;
return (GetCharProperties(ch) & fNCStartNameSC) != 0;
}

#if XML10_FIFTH_EDITION
Expand All @@ -982,7 +983,7 @@ public static bool IsStartNameSingleChar(char ch)

public static bool IsCharData(char ch)
{
return (charProperties(ch) & fCharData) != 0;
return (GetCharProperties(ch) & fCharData) != 0;
}

// [13] PubidChar ::= #x20 | #xD | #xA | [a-zA-Z0-9] | [-'()+,./:=?;!*#@$_%] Section 2.3 of spec
Expand All @@ -998,26 +999,26 @@ public static bool IsPubidChar(char ch)
// TextChar = CharData - { 0xA, 0xD, '<', '&', ']' }
internal static bool IsTextChar(char ch)
{
return (charProperties(ch) & fText) != 0;
return (GetCharProperties(ch) & fText) != 0;
}

// AttrValueChar = CharData - { 0xA, 0xD, 0x9, '<', '>', '&', '\'', '"' }
internal static bool IsAttributeValueChar(char ch)
{
return (charProperties(ch) & fAttrValue) != 0;
return (GetCharProperties(ch) & fAttrValue) != 0;
}

// XML 1.0 Fourth Edition definitions
//
public static bool IsLetter(char ch)
{
return (charProperties(ch) & fLetter) != 0;
return (GetCharProperties(ch) & fLetter) != 0;
}

// This method uses the XML 4th edition name character ranges
public static bool IsNCNameCharXml4e(char ch)
{
return (charProperties(ch) & fNCNameXml4e) != 0;
return (GetCharProperties(ch) & fNCNameXml4e) != 0;
}

// This method uses the XML 4th edition name character ranges
Expand Down Expand Up @@ -1089,7 +1090,7 @@ internal static int IsOnlyWhitespaceWithPos(string str)
{
for (int i = 0; i < str.Length; i++)
{
if ((charProperties(str[i]) & fWhitespace) == 0)
if ((GetCharProperties(str[i]) & fWhitespace) == 0)
{
return i;
}
Expand All @@ -1104,7 +1105,7 @@ internal static int IsOnlyCharData(string str)
{
for (int i = 0; i < str.Length; i++)
{
if ((charProperties(str[i]) & fCharData) == 0)
if ((GetCharProperties(str[i]) & fCharData) == 0)
{
if (i + 1 >= str.Length || !(XmlCharType.IsHighSurrogate(str[i]) && XmlCharType.IsLowSurrogate(str[i + 1])))
{
Expand Down