Re:RoslynLinqRewrite – Compiles C# by rewriting and optimisi...
by mwsherman on Wednesday September 21, 2016 @12:47AM
(#12545486)
Looks nice. A lot of people don’t realize how expensive Linq can be. That said, my preference for most (source) codegen is in the editor – this would be nice as an editor plugin.
Re:RoslynLinqRewrite – Compiles C# by rewriting and optimisi...
by mwsherman on Wednesday September 21, 2016 @12:51AM
(#12545502)
Oh, and a question – is the generated code actually semantically identical? For most uses practical uses, it is, but I think under racy conditions it could give different results.
Re:RoslynLinqRewrite – Compiles C# by rewriting and optimisi...
by ygra on Wednesday September 21, 2016 @01:55AM
(#12545749)
It is, for the example. But it only works because it is known at that point that the input is not just an IEnumerable<int>, but rather an int[]. Knowing that they can generate a for loop, which is quite a bit faster than the enumerator. For an unknown IEnumerable they can still emit the loop and static helper method and cut down on the generated closure, but they cannot remove the IEnumerator usage. They could probably do the same as for the array if it's known that the source IEnumerable is a List<T> (and not a subclass, because GetEnumerator may have been overridden with side-effects).
Effectively this transformation is the same as doing "Convert LINQ to code" and "Extract method" in R# (although those tend to sometimes be unable to codify even simple LINQ expressions).
Re:RoslynLinqRewrite – Compiles C# by rewriting and optimisi...
by ygra on Wednesday September 21, 2016 @03:57AM
(#12546219)
They probably manually inlined a line like
int num2 = _linqitems[i];
from the decompiled output. I'm fairly certain that if the transformation wouldn't result in compiling code, the compiler would refuse to compile it, resulting in no assembly to decompile. They also noted somewhere to have run the System.Linq tests and only one edge case involving Min/Max and NaN failed. Tests likely wouldn't run either if the code didn't compile.
Re:RoslynLinqRewrite – Compiles C# by rewriting and optimisi...
by olmo on Wednesday September 21, 2016 @01:34AM
(#12545672)
It's true, but 95% of the cases you get much better results by using the right data structures (dictionaries) instead of transforming queries to loops. Still is a very cool thing, hopefully is standardized into Roslyn
Re:RoslynLinqRewrite – Compiles C# by rewriting and optimisi...
by damieng on Wednesday September 21, 2016 @05:49AM
(#12546704)
Worth bearing in mind that this only helps if you are using LINQ against in-memory objects (i.e. LINQ to Objects). Depending on how this detects IQueryable providers it will either skip it, break it or worse of all silently 'work' but pull all the remote data into memory for evaluation. I'm hoping it's skip.
I guess this is for exactly the reasons you talk about, i.e. once it's working with Expression tress too much is unknown and any optimisations would be v. risky.
Re:RoslynLinqRewrite – Compiles C# by rewriting and optimisi...
by yread on Wednesday September 21, 2016 @03:41AM
(#12546156)
It would be more interesting to apply it selectively to LINQ on hot code paths and keep LINQ's readability where performance and allocations are not a concern
Effectively this transformation is the same as doing "Convert LINQ to code" and "Extract method" in R# (although those tend to sometimes be unable to codify even simple LINQ expressions).
It doesn't even look right for the example. This line:
Should be:The tool works fine, I've tried it out myself
BTW I cover this in my recent book: https://www.packtpub.com/mapt/book/Application%20Development...
You could try code samples and benchmarks: https://github.com/PacktPublishing/ASP.NET-Core-1.0-High-Per...
I guess this is for exactly the reasons you talk about, i.e. once it's working with Expression tress too much is unknown and any optimisations would be v. risky.
> No support for F#
Why is this? Is it a macro (as opposed to working with bytecode directly)?
[1] `The .NET Compiler Platform ("Roslyn") provides open-source C# and Visual Basic compilers with rich code analysis APIs.` - https://github.com/dotnet/roslyn