This repository was archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Optimize Enumerable.Range(...).Select(...) #37410
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -125,8 +125,13 @@ public IPartition<TResult> Skip(int count) | |
| return new SelectListPartitionIterator<TSource, TResult>(_source, _selector, count, int.MaxValue); | ||
| } | ||
|
|
||
| public IPartition<TResult> Take(int count) => | ||
| count >= _source.Length ? (IPartition<TResult>)this : new SelectListPartitionIterator<TSource, TResult>(_source, _selector, 0, count - 1); | ||
| public IPartition<TResult> Take(int count) | ||
| { | ||
| Debug.Assert(count > 0); | ||
| return count >= _source.Length ? | ||
| (IPartition<TResult>)this : | ||
| new SelectListPartitionIterator<TSource, TResult>(_source, _selector, 0, count - 1); | ||
| } | ||
|
|
||
| public TResult TryGetElementAt(int index, out bool found) | ||
| { | ||
|
|
@@ -157,6 +162,132 @@ public TResult TryGetLast(out bool found) | |
| } | ||
| } | ||
|
|
||
| private sealed partial class SelectRangeIterator<TResult> : Iterator<TResult>, IPartition<TResult> | ||
| { | ||
| private readonly int _start; | ||
| private readonly int _end; | ||
| private readonly Func<int, TResult> _selector; | ||
|
|
||
| public SelectRangeIterator(int start, int end, Func<int, TResult> selector) | ||
| { | ||
| Debug.Assert(start < end); | ||
| Debug.Assert((end - start) <= int.MaxValue); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This expression is always true,even overflow. Does it should be?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup, it's missing the casts. We'd welcome a PR if you'd like to fix it. Thanks. |
||
| Debug.Assert(selector != null); | ||
|
|
||
| _start = start; | ||
| _end = end; | ||
| _selector = selector; | ||
| } | ||
|
|
||
| public override Iterator<TResult> Clone() => | ||
| new SelectRangeIterator<TResult>(_start, _end, _selector); | ||
|
|
||
| public override bool MoveNext() | ||
| { | ||
| if (_state < 1 || _state == (_end - _start + 1)) | ||
|
stephentoub marked this conversation as resolved.
|
||
| { | ||
| Dispose(); | ||
| return false; | ||
| } | ||
|
|
||
| int index = _state++ - 1; | ||
|
stephentoub marked this conversation as resolved.
|
||
| Debug.Assert(_start < _end - index); | ||
| _current = _selector(_start + index); | ||
|
stephentoub marked this conversation as resolved.
|
||
| return true; | ||
| } | ||
|
|
||
| public override IEnumerable<TResult2> Select<TResult2>(Func<TResult, TResult2> selector) => | ||
|
stephentoub marked this conversation as resolved.
|
||
| new SelectRangeIterator<TResult2>(_start, _end, CombineSelectors(_selector, selector)); | ||
|
|
||
| public TResult[] ToArray() | ||
| { | ||
| var results = new TResult[_end - _start]; | ||
| int srcIndex = _start; | ||
| for (int i = 0; i < results.Length; i++) | ||
| { | ||
| results[i] = _selector(srcIndex++); | ||
| } | ||
|
|
||
| return results; | ||
| } | ||
|
|
||
| public List<TResult> ToList() | ||
| { | ||
| var results = new List<TResult>(_end - _start); | ||
| for (int i = _start; i != _end; i++) | ||
|
stephentoub marked this conversation as resolved.
|
||
| { | ||
| results.Add(_selector(i)); | ||
|
stephentoub marked this conversation as resolved.
|
||
| } | ||
|
|
||
| return results; | ||
| } | ||
|
|
||
| public int GetCount(bool onlyIfCheap) | ||
| { | ||
| // In case someone uses Count() to force evaluation of the selector, | ||
| // run it provided `onlyIfCheap` is false. | ||
| if (!onlyIfCheap) | ||
| { | ||
| for (int i = _start; i != _end; i++) | ||
| { | ||
| _selector(i); | ||
| } | ||
| } | ||
|
|
||
| return _end - _start; | ||
| } | ||
|
|
||
| public IPartition<TResult> Skip(int count) | ||
| { | ||
| Debug.Assert(count > 0); | ||
|
|
||
| if (count >= (_end - _start)) | ||
| { | ||
| return EmptyPartition<TResult>.Instance; | ||
| } | ||
|
|
||
| return new SelectRangeIterator<TResult>(_start + count, _end, _selector); | ||
| } | ||
|
|
||
| public IPartition<TResult> Take(int count) | ||
|
stephentoub marked this conversation as resolved.
|
||
| { | ||
| Debug.Assert(count > 0); | ||
|
|
||
| if (count >= (_end - _start)) | ||
| { | ||
| return this; | ||
| } | ||
|
|
||
| return new SelectRangeIterator<TResult>(_start, _start + count, _selector); | ||
| } | ||
|
|
||
| public TResult TryGetElementAt(int index, out bool found) | ||
| { | ||
| if ((uint)index < (uint)(_end - _start)) | ||
| { | ||
| found = true; | ||
| return _selector(_start + index); | ||
| } | ||
|
|
||
| found = false; | ||
| return default; | ||
| } | ||
|
|
||
| public TResult TryGetFirst(out bool found) | ||
|
adamsitnik marked this conversation as resolved.
|
||
| { | ||
| Debug.Assert(_end > _start); | ||
| found = true; | ||
| return _selector(_start); | ||
| } | ||
|
|
||
| public TResult TryGetLast(out bool found) | ||
| { | ||
| Debug.Assert(_end > _start); | ||
| found = true; | ||
| return _selector(_end - 1); | ||
| } | ||
| } | ||
|
|
||
| private sealed partial class SelectListIterator<TSource, TResult> : IPartition<TResult> | ||
| { | ||
| public TResult[] ToArray() | ||
|
|
@@ -212,8 +343,11 @@ public IPartition<TResult> Skip(int count) | |
| return new SelectListPartitionIterator<TSource, TResult>(_source, _selector, count, int.MaxValue); | ||
| } | ||
|
|
||
| public IPartition<TResult> Take(int count) => | ||
| new SelectListPartitionIterator<TSource, TResult>(_source, _selector, 0, count - 1); | ||
| public IPartition<TResult> Take(int count) | ||
| { | ||
| Debug.Assert(count > 0); | ||
| return new SelectListPartitionIterator<TSource, TResult>(_source, _selector, 0, count - 1); | ||
| } | ||
|
|
||
| public TResult TryGetElementAt(int index, out bool found) | ||
| { | ||
|
|
@@ -308,8 +442,11 @@ public IPartition<TResult> Skip(int count) | |
| return new SelectListPartitionIterator<TSource, TResult>(_source, _selector, count, int.MaxValue); | ||
| } | ||
|
|
||
| public IPartition<TResult> Take(int count) => | ||
| new SelectListPartitionIterator<TSource, TResult>(_source, _selector, 0, count - 1); | ||
| public IPartition<TResult> Take(int count) | ||
| { | ||
| Debug.Assert(count > 0); | ||
| return new SelectListPartitionIterator<TSource, TResult>(_source, _selector, 0, count - 1); | ||
| } | ||
|
|
||
| public TResult TryGetElementAt(int index, out bool found) | ||
| { | ||
|
|
@@ -413,8 +550,11 @@ public IPartition<TResult> Skip(int count) | |
| return new SelectIPartitionIterator<TSource, TResult>(_source.Skip(count), _selector); | ||
| } | ||
|
|
||
| public IPartition<TResult> Take(int count) => | ||
| new SelectIPartitionIterator<TSource, TResult>(_source.Take(count), _selector); | ||
| public IPartition<TResult> Take(int count) | ||
| { | ||
| Debug.Assert(count > 0); | ||
| return new SelectIPartitionIterator<TSource, TResult>(_source.Take(count), _selector); | ||
| } | ||
|
|
||
| public TResult TryGetElementAt(int index, out bool found) | ||
| { | ||
|
|
@@ -579,6 +719,7 @@ public IPartition<TResult> Skip(int count) | |
|
|
||
| public IPartition<TResult> Take(int count) | ||
| { | ||
| Debug.Assert(count > 0); | ||
| int maxIndex = _minIndexInclusive + count - 1; | ||
| return (uint)maxIndex >= (uint)_maxIndexInclusive ? this : new SelectListPartitionIterator<TSource, TResult>(_source, _selector, _minIndexInclusive, maxIndex); | ||
| } | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.