Skip to content

Commit 45caaf8

Browse files
authored
Improve Enumerable.Skip and Enumerable.Take performance (#112401)
* improve Enumerable.Skip and Enumerable.Take performance * use TryGetSpan * use TryGetSpan at IndexOf in IListSkipTakeIterator
1 parent 66b39df commit 45caaf8

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/libraries/System.Linq/src/System/Linq/SkipTake.SpeedOpt.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,12 @@ public void CopyTo(TSource[] array, int arrayIndex) =>
148148

149149
private static void Fill(IList<TSource> source, Span<TSource> destination, int sourceIndex)
150150
{
151+
if (source.TryGetSpan(out ReadOnlySpan<TSource> sourceSpan))
152+
{
153+
sourceSpan.Slice(sourceIndex, destination.Length).CopyTo(destination);
154+
return;
155+
}
156+
151157
for (int i = 0; i < destination.Length; i++, sourceIndex++)
152158
{
153159
destination[i] = source[sourceIndex];
@@ -160,6 +166,11 @@ public int IndexOf(TSource item)
160166
{
161167
IList<TSource> source = _source;
162168

169+
if (source.TryGetSpan(out ReadOnlySpan<TSource> span))
170+
{
171+
return span.Slice(_minIndexInclusive, Count).IndexOf(item);
172+
}
173+
163174
int end = _minIndexInclusive + Count;
164175
for (int i = _minIndexInclusive; i < end; i++)
165176
{

0 commit comments

Comments
 (0)