This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Repository navigation
Improve throughput of Enumerable.Contains for default comparer - #25097
Merged
Merged
Conversation
When no comparer or a null comparer is specified to Enumerable.Contains with a non-collection enumerable, `EqualityComparer<TSource>.Default` is used. However, with recent improvements to the JIT around devirtualization and inlining of `EqualityComparer<TSource>.Default`, we can do much better for this case by having a loop dedicated to the comparer==null case. For an enumerable of ints, this speeds up Contains in my measurements by up to 33% (which logically makes sense, as we're effectively eliminating one of three interface calls invoked per element).
JonHanna
approved these changes
Nov 7, 2017
| } | ||
|
|
||
| foreach (TSource element in source) | ||
| if (comparer == null) |
Contributor
There was a problem hiding this comment.
Would it be worth catching explicitly passed EqualityComparer<TSource>.Default too?
Contributor
There was a problem hiding this comment.
That's pretty much what I'm wondering. I would guess it would be more common with direct collection operations than with linq to always pass in a comparer and have Default as the default, but that's only a guess.
Member
Author
There was a problem hiding this comment.
I'm not going to right now. Other than size increase, this change doesn't penalize any cases (e.g. it's using a branch that's already there). Adding such a check in theory could negatively impact other cases, even if it might be negligible.
Member
|
LGTM too. |
danmoseley
approved these changes
Nov 8, 2017
Dotnet-GitSync-Bot
pushed a commit
to Dotnet-GitSync-Bot/corefx
that referenced
this pull request
Jun 12, 2019
…otnet#25097) This was removed accidentally as part of overhauling the async infrastructure in 2.1. VS depends on it for some task tracking logic. Signed-off-by: dotnet-bot <[email protected]>
stephentoub
added a commit
that referenced
this pull request
Jun 12, 2019
…25097) This was removed accidentally as part of overhauling the async infrastructure in 2.1. VS depends on it for some task tracking logic. Signed-off-by: dotnet-bot <[email protected]>
picenka21
pushed a commit
to picenka21/runtime
that referenced
this pull request
Feb 18, 2022
…t/corefx#25097) When no comparer or a null comparer is specified to Enumerable.Contains with a non-collection enumerable, `EqualityComparer<TSource>.Default` is used. However, with recent improvements to the JIT around devirtualization and inlining of `EqualityComparer<TSource>.Default`, we can do much better for this case by having a loop dedicated to the comparer==null case. For an enumerable of ints, this speeds up Contains in my measurements by up to 33% (which logically makes sense, as we're effectively eliminating one of three interface calls invoked per element). Commit migrated from dotnet/corefx@4cc7cd6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When no comparer or a null comparer is specified to Enumerable.Contains with a non-collection enumerable,
EqualityComparer<TSource>.Defaultis used. However, with recent improvements to the JIT around devirtualization and inlining ofEqualityComparer<TSource>.Default, we can do much better for this case by having a loop dedicated to the comparer==null case. For an enumerable of ints, this speeds up Contains in my measurements by up to 33% (which logically makes sense, as we're effectively eliminating one of three interface calls invoked per element).cc: @VSadov, @OmarTawfik, @AndyAyersMS