This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
[WIP] Specialize Where.First, Where.Last and the First and Last overloads accepting predicates #15637
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.
Changes:
source.Where(predicate).First()
should have the same result assource.First(predicate)
(barring exceptions), so if we optimize for one we can optimize for the other. This PR speeds things up in both cases ifsource
is an array/List/IList, avoiding double interface dispatch and an enumerator allocation.source.Where(predicate).Last()
is now recognized in addition tosource.Last(predicate)
, so we don't run the predicate on every item ifsource
supports random access.I also added code to specialize
ilist.Where(predicate)
. It makes sense because we can check for IList at little cost to existing codepaths, and there is already logic in place forilist.First(predicate)
andilist.Last(predicate)
, so we can implementilist.Where(predicate).First()
andilist.Where(predicate).Last()
for free.Marking WIP temporarily because I need to finish adding XML docs and make sure all branches are being covered. Comments are welcome in the meantime though.
/cc @JonHanna @VSadov
edit: Forgot the most important thing-- performance results. Here they are. There seems to be an improvement across the board for basically every pattern. The leaked memory stats can be ignored, I added that as part of #15389.