-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(Proposal) Inline the Lambda. #275
Comments
How do you propose that the compiler know what is inside the method Aggregate, especially considering that most compilations are against a reference assembly? How would this affect binary compatibility? |
We considered this when originally doing LINQ operators, with the assumption was that we, the compiler writers and the original LINQ API authors, knew precisely the implementation. However, that's not likely to always be true. |
@mattwar, Why not to implement this for the stock Linq operators from System.Linq namespace only? It would just increase deepness of integration of queries into the language :) |
I would implement it via a template function ( #174 ) , and have the compiler build it. |
The motto for the effort could be: |
👍 |
@gafter It could also do some form of internals embedding. ie clone and mutation of the IL of the Existing precedence |
Is this feature somewhat similar to |
@mpawelski That's looks (to me) to more making sure the function is polymorphic. Let's say that |
Wouldn't it better to introduce a short-hand custom attribute applicable to local code instead of yet another keyword? This would be similar to F#'s units-of-measure technique. [Inline] (_h, item)=> HashCode.Combine( _h, item);
//Equivalent of [MethodImpl(MethodImplOptions.AggressiveInlining)] (_h, item)=> HashCode.Combine( _h, item) |
@dsaf, delegate invocations never get inlined, last I checked, so the attribute isn't helping the situation. |
BTW, you can have these optimisations in LINQ if you use the LinqOptimizer library, it does these optimisations:
However you need to re-write your queries like so: var query = (from num in nums.AsQueryExpr()
where num % 2 == 0
select num * num).Sum(); which gets turned into: int sum = 0;
for (int index = 0; index < nums.Length; index++)
{
int num = nums[index];
if (num % 2 == 0)
sum += num * num;
} |
👍 |
@mburbea I meant using an attribute instead of introducing a keyword, rather than what that specific attribute is currently doing. |
What about the language disallows this today? Why is this something that should be done by compilers as opposed to being done by the runtime? |
Since the compiler is already allowed to do this (when it can prove the result is semantically identical), there is nothing to do. Having said that, this is something traditionally done in the JIT. |
Premise
Allow lambda function (in particularly LINQ) to rewritten by the compiler as inline version of the algorithm. No Closures, No Delegates
Syntax
This would be enable by prefixing the lambda with
inline
eginline (x,y)=> ;
Example
The foreach aspect of the pull request #209 could written via Aggregate.
would be rewritten as an inline implementation.
Admittedly the produce code is slightly less efficient (an extra variable).
The text was updated successfully, but these errors were encountered: