- Frugal Cafe
- Posts
- FC05 Total disregard of proper data structure and algorithm design: Linq.Enumerable
FC05 Total disregard of proper data structure and algorithm design: Linq.Enumerable
Linq Is Not Quick
When I found really bad/complex/costly LINQ.Enumerable expressions, I would remind developers that Linq.Enumerable is total disregard of proper data structure and algorithm design.
Here is one such stack found in a Lenovo utility program on a laptop:
There is a complex LINQ expression causing 6.6% allocations due to HashSet enumerator. HashSet is designed for O(1) lookup, so enumeration is the wrong pattern.
Source code:
Here Executable is HashSet:
First, we need to change the comparer:
Now we can change original code to:
That is the proper way to use HashSet, no heap allocation for enumerator is needed, no LINQ.Enumerable extension methods needed.
Remember the Linq in Linq.Enumerable really stands for Linq Is Not Quick.