Linq-like extension functions for Arrays, Span<T>, and List<T> that are faster and allocate less.
Linq-like extension functions for Arrays, Span<T>, and List<T> that are faster and allocate less.
UPDATE: As of .NET 9 this library is mostly unecessary, as .NET9 had made most of these optimizations part of the native LINQ library, but if you find some useful helper functions, enjoy!
A set of Linq-Like extension methods for arrays[], Span, and List that are faster and allocate less. LinqFaster is designed
to work well alongside Linq, rather than replace it completely. Extension method names mirror Linq names with an extra character, e.g. SelectF. If you have code that is often doing small Linq operations and immediatelly calling ToArray() or ToList(), or you are often using Min,Max,Sum,Average,or Aggregate, LinqFaster may provide massive cpu and memory usage reductions. See the documentation section below for more details.
LinqFaster now includes all relevant Linq extension methods, and many SIMD and Parallel enhanced extension methods are available as well in separate projects and nuget packages. The projects have no dependencies on each other so you can pick and choose at will.
The base LinqFaster project is now compatible with Unity3D
64bit Win 10, 2 Core I7 Mobile
| Method | TEST_SIZE | Mean | Allocated |
|---|---|---|---|
| OrderByLinq | 100000 | 24,436.0135 us | 1.2 MB |
| OrderByFast | 100000 | 5,612.7712 us | 802.1 kB |
| MinLinq | 100000 | 548.9181 us | 48 B |
| MinFast | 100000 | 69.2122 us | 0 B |
| MinFastSIMD | 100000 | 14.5291 us | 0 B |
| SumLinq | 100000 | 541.3823 us | 48 B |
| SumFast | 100000 | 53.8166 us | 0 B |
| SumFastSIMD | 100000 | 9.7636 us | 0 B |
| SumFastSIMDParallel | 100000 | 3.7074 us | 1.11 kB |
More detailed info and benchmarks available in the benchmarks file which will be continually updated.
As well, all functions are properly documented so as to be explorable via intellisense.
…See MSDN documentation on System.Numerics.Vectors for more detail on SIMD in C#.
……See MSDN documentation on System.Numerics.Vectors for more detail on SIMD in C#.
…These are purely imperative implementations of the same higher order functions that Linq provides, but unlike Linq they are not lazily evaluated. This means that when chaining functions together such as:
var a = data.Where(predicate).Select(transform).Aggregate(foo);
//or
var b = data.Select(selector).Sum();
Linq would not do any work until the calls to Sum() or Aggregate(), and thus iterate over the collection only once and
allocate very little. LinqFaster used this way would iterate over the collection each time and allocate
much more. Sometimes the net result will still be faster overall but the better approach is to
use the combined LinqFaster operations such as SelectWhere, WhereSelect, and WhereAggregate.
For example the expressions above would become:
var a = data.WhereAggregate(predicate,transform,foo);
// and
var b = data.Sum(selector);
This gets you the best of both worlds. The speed of memory locality and no allocations at all. In short, think about how you are transforming your data. In some cases normal Linq may be the better choice.
While most of the functions strive to provide indentical results to Linq, the OrderBy methods are not a stable sort, while in Linq they are.
No open issues yet, or sync has not completed.