-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Add early return in TryGetLast for empty results. #123306
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
Conversation
When source is an Iterator and GetCount(onlyIfCheap: true) returns a valid count (not -1), check if count <= _minIndexInclusive. In this case, immediately return with found=false.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds a performance optimization to IEnumerableSkipTakeIterator.TryGetLast by introducing an early return path when the source iterator can cheaply determine that there are no elements after skipping.
Changes:
- Modified the condition from
count > _minIndexInclusivetocount != -1to enter the fast path whenever a cheap count is available - Added an early return when
count <= _minIndexInclusive, avoiding enumeration when there are no elements to return - Performance improvement demonstrated via benchmarks: ~2x faster for scenarios like
Prepend().Skip(),Append().Skip(), andConcat().Skip()when skipping beyond the available elements
|
Tagging subscribers to this area: @dotnet/area-system-linq |
stephentoub
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks reasonable. Thanks!
Co-authored-by: Stephen Toub <stoub@microsoft.com>
This PR adds a fast path in `IEnumerableSkipTakeIterator.TryGetLast`. When source is an Iterator and `GetCount(onlyIfCheap: true)` returns a valid count (not -1), check if count <= _minIndexInclusive. In this case, immediately return with found=false. --------- Co-authored-by: Stephen Toub <stoub@microsoft.com>
Summary
This PR adds a fast path in
IEnumerableSkipTakeIterator.TryGetLast. When source is an Iterator andGetCount(onlyIfCheap: true)returns a valid count (not -1), check if count <= _minIndexInclusive. In this case, immediately return with found=false.Benchmark