-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Improve .Distinct().ToList() and .Union(e).ToList()
#95224
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
Improve .Distinct().ToList() and .Union(e).ToList()
#95224
Conversation
|
Tagging subscribers to this area: @dotnet/area-system-linq Issue Details
public class Bench
{
[Params(10, 1000, 1_000_000)]
public int Count { get; set; }
private int[] _arr;
[GlobalSetup]
public void Init()
{
_arr = new int[Count];
new Random(3).NextBytes(MemoryMarshal.AsBytes(_arr.AsSpan()));
}
[Benchmark]
public List<int> DistinctToList()
{
return _arr.Distinct().ToList();
}
}
|
|
@dotnet-policy-service agree |
|
Can you compare with latest main branch too, and include memory diagnoser? |
Ah, yes, thank you for saying this, it is indeed less of an improvement than that first bench shows, most of the improvements gained here were also gained with #86796 (when I had checked earlier, I had thought this PR was included in net8, which was why I just benched against that, but looking again it was not). Here are the new results:
Still a very small improvement, but I apologize for the misleading benchmark in the original description 😅 |
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.
Still a very small improvement, but I apologize for the misleading benchmark in the original description 😅
No worries at all. This simplifies the implementation while still doing better than main. Thank you for the contribution :-)
Enumerable.HashSetToListfills the result list by copying from the set to the list itself, but this can be done faster (and more simply) by using theList<T>(IEnumerable<T>)constructor to callHashSet<T>.CopyTo(T[]).