-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Description
The following issue was filed twice at Roslyn, dotnet/roslyn#7580 and dotnet/roslyn#275 where @gafter suggested that this optimization should be done by the JIT rather than by the C# compiler.
This is why I am copy-pasting the issue in this repo (minus some comments that were only relevant to Roslyn).
LINQ (the Enumerable flavor) is a huge productivity boost.
Unfortunately it comes with an additional runtime cost: more delegates calls, often lambda captures, more allocations...
This means that on hot paths, LINQ may not be an appropriate choice. Actually, I've read that Roslyn avoids LINQ on hot paths for this very reason.
In many (most?) cases, the compiler could turn the LINQ code into optimized for
(foreach
) loops. The proof that this is possible is that LinqOptimizer from Nessos does just that, at runtime.
I suggest that Roslyn performs the transformation done by LinqOptimizer at compiler-time when it can (i.e. no call to unknown methods, no unsupported construct). If it can't, it bails out to the library-based approach used today.
Benefits:
- everyone gets a speed boost (15x on some queries) and reduced memory usage for free.
- even people who don't know about LinqOptimizer (i.e. almost everyone).
- this makes LINQ usable again in more situations.
- transformation happens at compile-time. Today this can be done with LinqOptimizer, but at the cost of creating Expression trees and compiling them at runtime. :(
Main drawback is certainly that this is a large and complex code transformation to include and support in the compiler. I think that it fits well with the current theme of creating a leaner, more performant language with reduced memory allocations; and I hope you'd think it's worthy of including in the compiler proper.
category:proposal
theme:optimization
skill-level:expert
cost:extra-large