Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

[WIP] Specialize Where.First, Where.Last and the First and Last overloads accepting predicates #15637

Closed
wants to merge 3 commits into from

Conversation

jamesqo
Copy link
Contributor

@jamesqo jamesqo commented Jan 30, 2017

Changes:

  • source.Where(predicate).First() should have the same result as source.First(predicate) (barring exceptions), so if we optimize for one we can optimize for the other. This PR speeds things up in both cases if source is an array/List/IList, avoiding double interface dispatch and an enumerator allocation.

  • source.Where(predicate).Last() is now recognized in addition to source.Last(predicate), so we don't run the predicate on every item if source 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 for ilist.First(predicate) and ilist.Last(predicate), so we can implement ilist.Where(predicate).First() and ilist.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.

@jamesqo
Copy link
Contributor Author

jamesqo commented Feb 22, 2017

Closing temporarily. I haven't gotten around to making progress on this so far.

@jamesqo jamesqo closed this Feb 22, 2017
@jamesqo jamesqo deleted the more-where branch February 22, 2017 04:07
@karelz karelz modified the milestone: 2.0.0 Feb 22, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants