3 ms·
This is great and despite what the article says, an actually useful technique. The reason this performs well is the .NET runtime does monomorphization when the
by MarkSweep 2y ago
This is great and despite what the article says, an actually useful technique. The reason this performs well is the .NET runtime does monomorphization when the generic type parameters are value types (primitive types like int or user-defined structs). In modern .NET you would use static abstract methods interface methods to implement this feature. But if you have to run on the old Windows-only .NET Framework, you need to use the approach used in BrainFlood. See this article [1] for more details. An example of an official .NET library that uses this technique on .NET Framework is TensorPrimitives. See this file [2], the structs are defined at the bottom.
[1]: https://blog.stephencleary.com/2022/10/modern-csharp-techniques-3-generic-code-generation.html https://blog.stephencleary.com/2022/10/modern-csharp-techniq...
[2]: https://github.com/dotnet/runtime/blob/main/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netstandard/TensorPrimitives.Single.netstandard.cs https://github.com/dotnet/runtime/blob/main/src/libraries/Sy...