-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Add System.Memory reference #31162
Add System.Memory reference #31162
Conversation
/// <param name="isAscii">True if the sequence contains only characters in the ASCII range.</param> | ||
/// <returns>The FNV-1a hash of <paramref name="data"/></returns> | ||
internal static unsafe int GetFNVHashCode(byte* data, int length, out bool isAscii) | ||
internal static int GetFNVHashCode(ReadOnlySpan<byte> data, out bool isAscii) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this makes me so happy :)
Also replaces some unsafe code with Span<T>.
ef844ba
to
3e3206e
Compare
return true; | ||
} | ||
internal static bool TextEquals(string array, ReadOnlySpan<char> text) | ||
=> text.Equals(array.AsSpan(), StringComparison.Ordinal); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be SequenceEquals.
Nevermind!
{ | ||
#if DEBUG | ||
for (var i = 0; i < length; i++) | ||
for (var i = 0; i < ascii.Length; i++) | ||
{ | ||
Debug.Assert((ascii[i] & 0x80) == 0, "The byte* input to this method must be valid ASCII."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
byte* [](start = 58, length = 5)
This message is out-of-date
return true; | ||
} | ||
internal static bool TextEquals(string array, ReadOnlySpan<char> text) | ||
=> text.Equals(array.AsSpan(), StringComparison.Ordinal); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
text.Equals [](start = 15, length = 11)
I didn't understand how this .Equals
works.
ReadOnlySpan.Equals
seems to throw an exception (source )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Spans can't be boxed anyway, so that method couldn't be the target)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
@@ -118,7 +118,7 @@ internal T FindItem(char[] chars, int start, int len, int hashCode) | |||
|
|||
if (text != null && localSlot.HashCode == hashCode) | |||
{ | |||
if (StringTable.TextEquals(text, chars, start, len)) | |||
if (StringTable.TextEquals(text, chars.AsSpan(start, len))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AsSpan [](start = 55, length = 6)
AsReadOnlySpan
? (also below)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's an implicit conversion, doesn't matter.
I assume the benchmarks in the description were for a previous iteration with a broader change. |
<ExpectedDependency Include="System.Runtime.CompilerServices.Unsafe" DevEnvLib="lib/netstandard2.0/System.Runtime.CompilerServices.Unsafe.dll"/> | ||
<ExpectedDependency Include="System.Text.Encoding.CodePages" DevEnvLib="lib/netstandard2.0/System.Text.Encoding.CodePages.dll"/> | ||
<ExpectedDependency Include="System.Numerics.Vectors" DevEnvLib="lib/netstandard2.0/System.Numerics.Vectors.dll" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
System.Numerics.Vectors [](start = 33, length = 23)
Does this relate to System.Memory?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dependent reference.
@@ -223,6 +223,8 @@ public class TestAnalyzer : DiagnosticAnalyzer | |||
|
|||
dir.CopyFile(typeof(System.Reflection.Metadata.MetadataReader).Assembly.Location); | |||
dir.CopyFile(typeof(AppDomainUtils).Assembly.Location); | |||
dir.CopyFile(typeof(Memory<byte>).Assembly.Location); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
byte [](start = 39, length = 4)
nit: the byte
is probably not needed: typeof(Memory<>)
(here and in TestHelpers.cs
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM Thanks (iteration 3) with some clarifying questions
Allows us to replace some unsafe code with Span. I anticipate more complex
uses in the future.
Here are the benchmarks.
Before
After
There does not appear to be any statistically significant difference.