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.
[ Parent
| on HN ]
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 nhebb on Wednesday September 21, 2016 @02:43AM
(#12545930)
>It is, for the example.
It doesn't even look right for the example. This line:
num += num2 + 3;
Should be: num += _linqitems[i] + 3;
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 OskarS on Wednesday September 21, 2016 @03:12AM
(#12546014)
Yeah, what the hell? That wont even compile. Is this thing even real? Why wouldn't the just use the actual output of their tool?
Re:RoslynLinqRewrite – Compiles C# by rewriting and optimisi...
by matthewwarren on Wednesday September 21, 2016 @05:56AM
(#12546733)
It's just a typo on the README and it's been fixed now.
The tool works fine, I've tried it out myself
Re:RoslynLinqRewrite – Compiles C# by rewriting and optimisi...
by OskarS on Wednesday September 21, 2016 @09:02AM
(#12547754)
Ok, good. A thing like that doesn't inspire confidence, but presumably it was just a typo then.
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 jsingleton on Wednesday September 21, 2016 @07:55AM
(#12547302)
Using the best structure is a good idea but simple LINQ is very readable if performance isn't a big concern.
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...
Re:RoslynLinqRewrite – Compiles C# by rewriting and optimisi...
by platz on Wednesday September 21, 2016 @01:37AM
(#12545679)
How would the provided example be better realized as a dictionary?
Re:RoslynLinqRewrite – Compiles C# by rewriting and optimisi...
by SideburnsOfDoom on Wednesday September 21, 2016 @03:59AM
(#12546230)
All true, but the example is a demo that doesn't do anything useful, and not typical code.