4 ms·
RoslynLinqRewrite – Compiles C# by rewriting and optimising LINQ expressions
- duaneb 10y agoLooks cool! > No support for F# Why is this? Is it a macro (as opposed to working with bytecode directly)?
- wluu 10y agoBecause 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 https://github.com/dotnet/roslyn
- CurtHagenlocher 10y agoIf only C# had LISP-style macros... .
- mwsherman 10y agoLooks 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.
- mwsherman 10y agoOh, 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.
- ygra 10y agoIt 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).
- nhebb 10y ago>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;
- OskarS 10y agoYeah, what the hell? That wont even compile. Is this thing even real? Why wouldn't the just use the actual output of their tool?
- matthewwarren 10y agoIt's just a typo on the README and it's been fixed now. The tool works fine, I've tried it out myself
- OskarS 10y agoOk, good. A thing like that doesn't inspire confidence, but presumably it was just a typo then.
- ygra 10y agoThey 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.
- olmo 10y agoIt'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
- platz 10y agoHow would the provided example be better realized as a dictionary?
- SideburnsOfDoom 10y agoAll true, but the example is a demo that doesn't do anything useful, and not typical code.
- jsingleton 10y agoUsing 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/9781785881893/6 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-Performance/tree/master/Chapter%206 https://github.com/PacktPublishing/ASP.NET-Core-1.0-High-Per...
- yread 10y agoIt 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
- SideburnsOfDoom 10y agoIf this happens as a step during each compilation then you probably do keep readability of the input source code.
- yread 10y agoSure, but when you start debugging you will see something else happening than what is in the input source code.
- damieng 10y agoWorth 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.
- matthewwarren 10y agoThis project only works with IEnumerable, not IQueryable, see https://github.com/antiufo/roslyn-linq-rewrite/blob/09216a3b2ab53474a93db861435c056523f194d0/RoslynLinqRewrite/RoslynLinqRewrite/LinqRewriter.Rules.cs#L167-L181 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.