-
Notifications
You must be signed in to change notification settings - Fork 48
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
Improve performance for GetNames in .NET 8 #71
Comments
Thanks @silkfire! Huh, that's interesting, I had seen things about the enum perf improvements in .NET 8 and so I had already run the tests in the repository myself, and found that the extension version was still faster... [MemoryDiagnoser]
public class GetNamesBenchmark
{
#if NETFRAMEWORK
[Benchmark(Baseline = true)]
[MethodImpl(MethodImplOptions.NoInlining)]
public string[] EnumGetNames()
{
return Enum.GetNames(typeof(TestEnum));
}
#else
[Benchmark(Baseline = true)]
[MethodImpl(MethodImplOptions.NoInlining)]
public string[] EnumGetNames()
{
return Enum.GetNames<TestEnum>();
}
#endif
[Benchmark]
[MethodImpl(MethodImplOptions.NoInlining)]
public string[] ExtensionsGetNames()
{
return TestEnumExtensions.GetNames();
}
} Which gave these results:
i.e. native version was 14ns, and 7ns for the extension. So directly contradicts Nick's findings 😅 Perhaps it's related to some SIMD work they're doing now? I ran these on a pretty old laptop, which only has 2 cores, if Nick's using something much beefier, maybe it's different. Alternatively, could be related to the enum itself. My test enum only had three values: [EnumExtensions]
public enum TestEnum
{
First = 0,
[Display(Name = "2nd")]
Second = 1,
Third = 2,
} I can try testing with the same one as Nick ( |
Very interesting indeed! I think it's likely that Mr Chapsas' machine is a bit more modern than the one you tested with, as I'm getting similar results (using the
I checked the source code of the native method and it seems it's written using super-optimized code that leverages internal P/Invoke calls to QCall. Something similar is used at least in .NET 6 too. Perhaps that's the only situation where the native method is hard to compete with, but I'm no expert at these micro optimizations so it's hard for me to tell. |
That is interesting. I just ran a test with 7 enum values to make sure that wasn't part of it, and I'm still getting the same overall results
Will have to dig in further 🤔 |
Could it be as you mentioned in your previous reply that more modern processors leverage hardware intrinsics which results in faster execution of the native method? |
Yeah, I'm assuming that's it (will confirm I also repro on my work machine instead of my old personal laptop). I'm guessing the magic happened in this PR 👀 dotnet/runtime#78580 |
In .NET 8, the performance of
Enum
-related methods has been considerably improved, albeit not as fast as your excellent extension.Recently, Nick Chapsas promoted your extension and ran benchmarks on it in .NET 8 and interestingly, the method
GetNames
of the source generator is slower than the native method.See https://youtu.be/UBY4Y6AykdM?si=VzbUusG5YOu1Ke2X&t=418
Could we work out a solution to improve the performance so it's on par or faster than the native equivalent?
The text was updated successfully, but these errors were encountered: