Remove unsafe from EqualStartingCharacterCount#121102
Merged
Merged
Conversation
Member
Author
stephentoub
reviewed
Oct 27, 2025
stephentoub
approved these changes
Oct 27, 2025
Member
Author
|
@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);
} |
xtqqczze
reviewed
Oct 27, 2025
Member
Author
|
@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);
} |
stephentoub
reviewed
Oct 27, 2025
Contributor
There was a problem hiding this comment.
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
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-runtime |
fbac4b0 to
d7b1de6
Compare
stephentoub
reviewed
Oct 27, 2025
xtqqczze
reviewed
Oct 27, 2025
Co-authored-by: xtqqczze <[email protected]>
Member
Author
|
/ba-g deadletter |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.