News for hackers. Stuff that matters.
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 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.
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.
Re:RoslynLinqRewrite – Compiles C# by rewriting and optimisi... by matthewwarren on Wednesday September 21, 2016 @09:07AM (#12547781)
This project only works with IEnumerable, not IQueryable, see https://github.com/antiufo/roslyn-linq-rewrite/blob/09216a3b... for instance.

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 duaneb on Tuesday September 20, 2016 @11:25PM (#12545165)
Looks cool!

> No support for F#

Why is this? Is it a macro (as opposed to working with bytecode directly)?

Re:RoslynLinqRewrite – Compiles C# by rewriting and optimisi... by wluu on Wednesday September 21, 2016 @12:48AM (#12545489)
Because Roslyn[1] itself can only understand C# and VB.NET.

[1] `The .NET Compiler Platform ("Roslyn") provides open-source C# and Visual Basic compilers with rich code analysis APIs.` - https://github.com/dotnet/roslyn

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
Re:RoslynLinqRewrite – Compiles C# by rewriting and optimisi... by SideburnsOfDoom on Wednesday September 21, 2016 @03:49AM (#12546186)
If this happens as a step during each compilation then you probably do keep readability of the input source code.
Re:RoslynLinqRewrite – Compiles C# by rewriting and optimisi... by yread on Wednesday September 21, 2016 @09:57AM (#12548169)
Sure, but when you start debugging you will see something else happening than what is in the input source code.
Re:RoslynLinqRewrite – Compiles C# by rewriting and optimisi... by CurtHagenlocher on Tuesday September 20, 2016 @11:28PM (#12545174)
If only C# had LISP-style macros... .