Improve Enumerable.Skip and Enumerable.Take performance#112401
Merged
stephentoub merged 3 commits intodotnet:mainfrom Feb 13, 2025
Merged
Improve Enumerable.Skip and Enumerable.Take performance#112401stephentoub merged 3 commits intodotnet:mainfrom
stephentoub merged 3 commits intodotnet:mainfrom
Conversation
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-linq |
Member
|
What does the performance look like for |
Member
|
@EgorBot -amd -arm using System.Collections.Immutable;
using BenchmarkDotNet.Attributes;
[MemoryDiagnoser]
public class Benchmark
{
private readonly IList<string> _list = Enumerable.Range(0, 1000)
.Select(i => i.ToString()).ToImmutableArray();
[Benchmark]
public List<string> SkipAndTake() =>
_list.Skip(200).Take(200).ToList();
} |
Member
|
I think with help of PGO, these casts are cheap (at least, in monomorphic microbenchmarks). Presumably the overhead is more visible on smaller inputs and when the input is diverse (so PGO can't help). |
Member
Perhaps using one benchmark class to run three against three different input types could help here? |
Contributor
Author
|
Even with small inputs there was not much degradation in performance [MemoryDiagnoser]
public class Benchmark
{
[Params(2, 20, 200, 2000)]
public int SkipCount { get; set; }
[Params(5, 50, 500, 5000)]
public int TakeCount { get; set; }
private readonly List<string> _list = Enumerable.Range(0, 10000).Select(i => i.ToString()).ToList();
private readonly ReadOnlyCollection<string> _readOnlyCollection = Enumerable.Range(0, 10000).Select(i => i.ToString()).ToList().AsReadOnly();
[Benchmark]
public List<string> ReadOnlyCollection_SkipAndTake() =>
_readOnlyCollection.Skip(SkipCount).Take(TakeCount).ToList();
[Benchmark]
public List<string> ReadOnlyCollection_SkipAndTake_PR() =>
_readOnlyCollection.Skip_PR(SkipCount).Take_PR(TakeCount).ToList();
[Benchmark]
public List<string> List_SkipAndTake() =>
_list.Skip(SkipCount).Take(TakeCount).ToList();
[Benchmark]
public List<string> List_SkipAndTake_PR() =>
_list.Skip_PR(SkipCount).Take_PR(TakeCount).ToList();
[Benchmark]
public List<string> List_GetRange() =>
_list.GetRange(SkipCount, TakeCount);
} |
neon-sunset
reviewed
Feb 11, 2025
stephentoub
reviewed
Feb 11, 2025
This was referenced Feb 11, 2025
Open
Closed
eiriktsarpalis
approved these changes
Feb 13, 2025
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
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Summary
CopyTois now used inToArrayandToListafterSkipandTakewhen source isList<T>orT[].Benchmark