8000 Add identity method to reduce JIT-ing by viceroypenguin · Pull Request #880 · morelinq/MoreLINQ · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add identity method to reduce JIT-ing #880

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

Merged
merged 4 commits into from
Nov 30, 2022
Merged

Add identity method to reduce JIT-ing #880

merged 4 commits into from
Nov 30, 2022

Conversation

viceroypenguin
Copy link
Contributor
@viceroypenguin viceroypenguin commented Nov 13, 2022

Currently, the C# compiler does not de-dupe methods, including identity methods, such as x => x. This means each instance of x => x is compiled as a separate method; and each method must be JIT to asm separately, and must be done so for each type separately.

Adding an Identity method improves JIT significantly:

  • Fewer methods to JIT
  • Since these methods are generic, they have to be JITted separately for each type; Identity can share the JITted versions across multiple call sites

Until .net7.0 + tiered PGO, these methods will not get inlined because they are accessed via delegate. This means they also take additional space in memory due to the translated code.

@codecov
Copy link
codecov bot commented Nov 13, 2022

Codecov Report

Merging #880 (6ecbcbf) into master (b31c7fd) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master     #880   +/-   ##
=======================================
  Coverage   92.38%   92.39%           
=======================================
  Files         110      111    +1     
  Lines        3441     3443    +2     
  Branches     1020     1021    +1     
=======================================
+ Hits         3179     3181    +2     
  Misses        200      200           
  Partials       62       62           
Impacted Files Coverage Δ
MoreLinq/Batch.cs 94.33% <100.00%> (ø)
MoreLinq/GroupAdjacent.cs 98.52% <100.00%> (ø)
MoreLinq/IdFn.cs 100.00% <100.00%> (ø)
MoreLinq/OrderedMerge.cs 93.84% <100.00%> (ø)
MoreLinq/Rank.cs 96.15% <100.00%> (ø)
MoreLinq/Split.cs 89.70% <100.00%> (ø)
MoreLinq/Partition.cs 98.27% <0.00%> (+0.03%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Copy link
Member
@atifaziz atifaziz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see the value in adding this because it's not improving anything. It's less ergonomic/succinct (or more verbose) than just a lambda (x => x) and it's not removing duplication of any “methods” per the PR subject line. What's more, it doesn't work with anonymous types.

@viceroypenguin
Copy link
Contributor Author
viceroypenguin commented Nov 14, 2022

I don't see the value in adding this because it's not improving anything. It's less ergonomic/succinct (or more verbose) than just a lambda (x => x) and it's not removing duplication of any “methods” per the PR subject line. What's more, it doesn't work with anonymous types.

Selected Method:

public static IEnumerable<T> OrderedMerge<T, TKey>(
    this IEnumerable<T> first,
    IEnumerable<T> second,
    Func<T, TKey> keySelector)
{
    return OrderedMerge(first, second, keySelector, Identity<T>, Identity<T>, (a, _) => a, null);
}

Decompile of .OrderedMerge() with lambdas:

[Extension]
public static IEnumerable<T> OrderedMerge<[System.Runtime.CompilerServices.Nullable(2)] T, [System.Runtime.CompilerServices.Nullable(2)] TKey>(IEnumerable<T> first, IEnumerable<T> second, Func<T, TKey> keySelector)
{
	return OrderedMerge(first, second, keySelector, <>c__156<T, TKey>.<>9__156_0 ?? (<>c__156<T, TKey>.<>9__156_0 = new Func<T, T>(<>c__156<T, TKey>.<>9.<OrderedMerge>b__156_0)), <>c__156<T, TKey>.<>9__156_1 ?? (<>c__156<T, TKey>.<>9__156_1 = new Func<T, T>(<>c__156<T, TKey>.<>9.<OrderedMerge>b__156_1)), <>c__156<T, TKey>.<>9__156_2 ?? (<>c__156<T, TKey>.<>9__156_2 = new Func<T, T, T>(<>c__156<T, TKey>.<>9.<OrderedMerge>b__156_2)), null);
}

Decompile of .OrderedMerge() with Identity<T>:

[Extension]
public static IEnumerable<T> OrderedMerge<[System.Runtime.CompilerServices.Nullable(2)] T, [System.Runtime.CompilerServices.Nullable(2)] TKey>(IEnumerable<T> first, IEnumerable<T> second, Func<T, TKey> keySelector)
{
	return OrderedMerge(first, second, keySelector, <OrderedMerge>O__157_0<T, TKey>.<0>__Identity ?? (<OrderedMerge>O__157_0<T, TKey>.<0>__Identity = new Func<T, T>(Identity)), <OrderedMerge>O__157_0<T, TKey>.<0>__Identity ?? (<OrderedMerge>O__157_0<T, TKey>.<0>__Identity = new Func<T, T>(Identity)), <>c__157<T, TKey>.<>9__157_0 ?? (<>c__157<T, TKey>.<>9__157_0 = new Func<T, T, T>(<>c__157<T, TKey>.<>9.<OrderedMerge>b__157_0)), null);
}

In the original version, the two identity lambdas are compiled separately (<>c__156<T, TKey>.<>9.<OrderedMerge>b__156_0 and <>c__156<T, TKey>.<>9.<OrderedMerge>b__156_1). In the new version, only one method is compiled: Identity<T>. This duplication is increased for each method that also uses the same identity method.

This improves JIT significantly:

  • Fewer methods to JIT
  • Since these methods are generic, they have to be JITted separately for each type; Identity<T> can share the JITted versions across multiple call sites

Until .net7.0 + tiered PGO, these methods will not get inlined because they are accessed via delegate. This means they also take additional space in memory due to the translated code.

Also, it works fine with anonymous types - see LinqPAD attachment here:
image

@viceroypenguin viceroypenguin changed the title Add Identity method to reduce duplicate methods Add Identity method to reduce duplicate anonymous methods Nov 15, 2022
@atifaziz
Copy link
Member
atifaziz commented Nov 16, 2022

@viceroypenguin Thanks for the detailed explanation. I understand now the savings you're trying to make with this and it would have been great to have the justification in the initial description. I'll come back to you on this, hopefully before the week is over.

@atifaziz
Copy link
Member

Also, it works fine with anonymous types - see LinqPAD attachment here:

My main concern was as a method group within a query where you're expecting to use Identity (and not as an argument to the method as in your LINQPad example), but it works fine:

var map =
    Enumerable.Range(1, 10)
              .Select(x => new { X = x, Y = x * 2 })
              .ToDictionary(x => x.X, Identity);

I was somewhat misled during my initial review by the explicit type annotations like Identity<TSource> and Identity<IEnumerable<TSource>>, where you still specify the generic type parameter, but coming back to it, those are all redundant.

Copy link
Member
@atifaziz atifaziz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this. I think you missed another opportunity to use identity function in Split?

@viceroypenguin
Copy link
Contributor Author

I was somewhat misled during my initial review by the explicit type annotations like Identity<TSource> and Identity<IEnumerable<TSource>>, where you still specify the generic type parameter, but coming back to it, those are all redundant.

Ah, right. Totally missed that. Amazing how powerful the typing system is sometimes. :)

Copy link
Member
@atifaziz atifaziz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, and nearly there! 🏁

Co-authored-by: Atif Aziz <code@raboof.com>
@atifaziz atifaziz changed the title Add Identity method to reduce duplicate anonymous methods Add identity method to reduce JIT-ing Nov 19, 2022
@atifaziz atifaziz merged commit 4e09ca4 into morelinq:master Nov 30, 2022
@viceroypenguin viceroypenguin deleted the identity branch November 30, 2022 22:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0