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

Optimize System.SpanHelpers using arm64 intrinsics #33707

Closed
BruceForstall opened this issue Mar 18, 2020 · 2 comments
Closed

Optimize System.SpanHelpers using arm64 intrinsics #33707

BruceForstall opened this issue Mar 18, 2020 · 2 comments

Comments

@BruceForstall
Copy link
Member

This item tracks the conversion of the System.SpanHelpers class to use arm64 intrinsics.

Related: #33308

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-System.Memory untriaged New issue has not been triaged by the area owner labels Mar 18, 2020
@BruceForstall BruceForstall added this to To do in Hardware Intrinsics via automation Mar 18, 2020
@BruceForstall BruceForstall added this to the 5.0 milestone Mar 18, 2020
@GrabYourPitchforks
Copy link
Member

A nit about these functions that might not be immediately obvious:

SpanHelpers.IndexOf(ref byte, ...) and SpanHelpers.IndexOf(ref char, ...) are used as the workhorses for the strlen and wcslen implementations. The caller lies about the length parameter and assumes that if the null character first occurs at page n, the IndexOf method will never attempt to dereference page n + 1. We have some limited unit tests for this scenario as shown below, but I wanted to make whoever attempts to intrinsicify this method aware of this behavior.

[Fact]
public static unsafe void Ctor_CharPtr_DoesNotAccessInvalidPage()
{
// Allocates a buffer of all 'x' followed by a null terminator,
// then attempts to create a string instance from this at various offsets.
const int MaxCharCount = 128;
using BoundedMemory<char> boundedMemory = BoundedMemory.Allocate<char>(MaxCharCount);
boundedMemory.Span.Fill('x');
boundedMemory.Span[MaxCharCount - 1] = '\0';
boundedMemory.MakeReadonly();
using MemoryHandle memoryHandle = boundedMemory.Memory.Pin();
for (int i = 0; i < MaxCharCount; i++)
{
string expectedString = new string('x', MaxCharCount - i - 1);
string actualString = new string((char*)memoryHandle.Pointer + i);
Assert.Equal(expectedString, actualString);
}
}

@kunalspathak
Copy link
Member

IndexOf(char), IndexOf(byte) , IndexOfAny(byte) are now optimized with ARM64 intrinsics. We didn't optimize SequenceCompareTo(byte) and SequenceEqual(byte) because the existing SIMD vector implementation is fast enough and comparable with ARM64 intrinsics.

Hardware Intrinsics automation moved this from In progress to Done Jun 25, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Development

No branches or pull requests

4 participants