4 ms·
> AVX instructions, which is implemented for quite a few LINQ methods Are you sure? Any examples of such methods? And does AVX actually helps? I don’t think t
by Const-me 3y ago
> AVX instructions, which is implemented for quite a few LINQ methods
Are you sure? Any examples of such methods? And does AVX actually helps?
I don’t think that’s possible because IMO AVX and other SIMD can only help for dense inputs. The C# type is ReadOnlySpan, however ReadOnlySpan doesn’t implement IEnumerable and therefore incompatible with LINQ.
There’s even an alternative LINQ to workaround https://github.com/NetFabric/NetFabric.Hyperlinq https://github.com/NetFabric/NetFabric.Hyperlinq but that thing is a third-party library most people aren’t using.
- neonsunset 3y ago- https://github.com/dotnet/runtime/blob/main/src/libraries/System.Linq/src/System/Linq/MaxMin.cs https://github.com/dotnet/runtime/blob/main/src/libraries/Sy... - https://github.com/dotnet/runtime/blob/main/src/libraries/System.Linq/src/System/Linq/Sum.cs#L75 https://github.com/dotnet/runtime/blob/main/src/libraries/Sy... - https://github.com/dotnet/runtime/blob/main/src/libraries/System.Linq/src/System/Linq/Range.SpeedOpt.cs#L39 https://github.com/dotnet/runtime/blob/main/src/libraries/Sy...
- Const-me 3y agoInteresting, I never heard about that. Was merged to master in February 2022, I wonder which release version includes the changes? Still, the support seems very limited. They simply probe argument type for arrays and lists. Any other IEnumerable gonna return false from TryGetSpan, which reverts to the legacy scalar implementation.
- merb 3y agopretty sure thats dotnet 7 only stuff. I can vaguely remember a blog post about it: https://devblogs.microsoft.com/dotnet/performance_improvements_in_net_7/#linq https://devblogs.microsoft.com/dotnet/performance_improvemen... (I think this is the one?) it's faster in bigger arrays/lists but smaller ones barely make a difference, even the linq vs non-linq make basically only noise difference as far as I remember.
- neonsunset 3y agoAlgorithms that work on non-contiguous or synthesized data are not subject to vectorization in any language, aside from select cases where LLVM is able to inline and auto-vectorize loops in Rust for certain simple cases of map->collect. What is your argument then?
- Const-me 3y agoMy point is the support is very limited, only works for a few trivial use cases, like sum/min/max/average of arrays of a few selected types. I believe it’s technically possible to vectorize more complicated stuff in C#, just the runtime library is not doing that. For an example, look at how Eigen C++ library https://eigen.tuxfamily.org/index.php?title=Main_Page https://eigen.tuxfamily.org/index.php?title=Main_Page does their math. Under the hood, they wrap inputs into classes which supply SIMD registers, then do math on these registers. Eigen does that in compile-time with template metaprogramming. A hypothetical C# implementation could do similar things using generics and/or runtime code generation. LINQ from the standard library was never designed for high-performance compute, but I think it might be possible to design similar API for that.
- neonsunset 3y agoHow is that relevant to the vast majority of the code targeted by LINQ? The niche scenario you have outlined is partially covered by a recent System.Numerics.Tensors package update (even though I believe it would have been best if there was a community-maintained package with comparable quality for a variety of reasons). The goal of LINQ itself is to offer optimal codepaths when it can within the constraints of the current design (naturally, you could improve it significantly if not for backwards compatibility with the previous 15 or so years of .NET codebases). The argument that it's not good because it's not the tool to do BLAS is just nonsensical. There is, however, an IL optimizer that can further vectorize certain common patterns and rewrite LINQ calls into open-coded loops: https://github.com/dubiousconst282/DistIL https://github.com/dubiousconst282/DistIL
- Const-me 3y ago