-
Couldn't load subscription status.
- Fork 5.2k
Remove unsafe from EqualStartingCharacterCount #121102
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
Conversation
|
@EgorBot -windows_intel -intel -arm using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
public class Benchmarks
{
static void Main(string[] args)
{
BenchmarkSwitcher.FromAssembly(typeof(Benchmarks).Assembly).Run(args);
}
public static IEnumerable<object> TestData()
{
yield return new object[] { @"C:\a", @"C:\a\b" };
yield return new object[] { @"C:\a\", @"C:\a\b\c" };
yield return new object[] {
@"C:\projects\runtime\src\libraries",
@"C:\projects\runtime\src\libraries\System.Text.Json\tests"
};
yield return new object[] {
@"C:\a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p",
@"C:\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"
};
yield return new object[] {
@"C:\projects\runtime-main2\src\libraries\Microsoft.Extensions.DependencyInjection.Specification.Tests\src\Fakes",
@"C:\projects\runtime-main2\src\libraries\Microsoft.Extensions.DependencyInjection.Specification.Tests\src\"
};
yield return new object[] { @"C:\foo\bar", @"D:\foo\bar" };
yield return new object[] { @"C:\foo\bar\baz", @"C:\foo\bar\..\qux\." };
yield return new object[] { @"\\server\share\folder1", @"\\server\share\folder1\folder2\folder3" };
yield return new object[] {
@"C:\Program Files\MyApp",
@"C:\Program Files\MyApp\bin\Debug\net8.0"
};
}
[Benchmark]
[ArgumentsSource(nameof(TestData))]
public string GetFoo(string relativeTo, string path) =>
Path.GetRelativePath(relativeTo, path);
} |
|
@EgorBot -intel -arm using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
public class Benchmarks
{
static void Main(string[] args)
{
BenchmarkSwitcher.FromAssembly(typeof(Benchmarks).Assembly).Run(args);
}
public static IEnumerable<object> TestData()
{
yield return new object[] { @"C:\a", @"C:\a\b" };
yield return new object[] { @"C:\a\", @"C:\a\b\c" };
yield return new object[] {
@"C:\projects\runtime\src\libraries",
@"C:\projects\runtime\src\libraries\System.Text.Json\tests"
};
yield return new object[] {
@"C:\a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p",
@"C:\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"
};
yield return new object[] {
@"C:\projects\runtime-main2\src\libraries\Microsoft.Extensions.DependencyInjection.Specification.Tests\src\Fakes",
@"C:\projects\runtime-main2\src\libraries\Microsoft.Extensions.DependencyInjection.Specification.Tests\src\"
};
yield return new object[] { @"C:\foo\bar", @"D:\foo\bar" };
yield return new object[] { @"C:\foo\bar\baz", @"C:\foo\bar\..\qux\." };
yield return new object[] { @"\\server\share\folder1", @"\\server\share\folder1\folder2\folder3" };
yield return new object[] {
@"C:\Program Files\MyApp",
@"C:\Program Files\MyApp\bin\Debug\net8.0"
};
}
[Benchmark]
[ArgumentsSource(nameof(TestData))]
public string GetFoo(string relativeTo, string path) =>
Path.GetRelativePath(relativeTo, path);
} |
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.
Pull Request Overview
This PR modernizes the EqualStartingCharacterCount method by removing unsafe pointer arithmetic and replacing it with safe span-based operations. The implementation now leverages CommonPrefixLength for case-sensitive comparisons and falls back to a simple loop with char.ToUpperInvariant for case-insensitive scenarios.
Key changes:
- Replaced unsafe pointer-based character comparison with safe span operations
- Utilized
AsSpan().CommonPrefixLength()for case-sensitive prefix matching - Simplified case-insensitive comparison logic using indexed access instead of pointer arithmetic
|
Tagging subscribers to this area: @dotnet/area-system-runtime |
fbac4b0 to
d7b1de6
Compare
Co-authored-by: xtqqczze <45661989+xtqqczze@users.noreply.github.com>
|
/ba-g deadletter |
I wasn't able to detect any difference for
Path.GetRelativePathand the codegen is fairly close.if we want extra perf, we can vectorize this routine trivially for Linux/macOS (where paths are case-sensitives).
Given this API allocates a new string, it probably isn't supposed to be used on a hot path.