- Notifications
You must be signed in to change notification settings - Fork5.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 ourterms of service andprivacy 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
Uh oh!
There was an error while loading.Please reload this page.
Conversation
ghost commentedNov 25, 2023
Tagging subscribers to this area: @dotnet/area-system-linq Issue Details
publicclassBench{[Params(10,1000,1_000_000)]publicintCount{get;set;}privateint[]_arr;[GlobalSetup]publicvoidInit(){_arr=newint[Count];newRandom(3).NextBytes(MemoryMarshal.AsBytes(_arr.AsSpan()));}[Benchmark]publicList<int>DistinctToList(){return_arr.Distinct().ToList();}}
|
Windows10CE commentedNov 25, 2023
@dotnet-policy-service agree |
huoyaoyuan commentedNov 26, 2023
Can you compare with latest main branch too, and include memory diagnoser? |
Windows10CE commentedNov 26, 2023 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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 hadthought 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 😅 |
eiriktsarpalis 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.
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[]).