Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Merged

Conversation

@Windows10CE
Copy link
Contributor

Enumerable.HashSetToList fills 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[]).

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();}}
BenchmarkDotNet v0.13.10, Arch LinuxAMD Ryzen 9 5900X, 1 CPU, 24 logical and 12 physical cores.NET SDK 8.0.100  [Host]     : .NET 8.0.0 (8.0.23.53103), X64 RyuJIT AVX2  Job-KQNFCO : .NET 8.0.1 (42.42.42.42424), X64 RyuJIT AVX2  Job-BSODDZ : .NET 8.0.0 (8.0.23.53103), X64 RyuJIT AVX2Runtime=.NET 8.0
MethodJobToolchainCountMeanErrorStdDevRatioRatioSD
DistinctToListJob-KQNFCOCoreRun10119.9 ns2.42 ns2.49 ns0.860.02
DistinctToListJob-BSODDZnet8.010138.2 ns1.33 ns1.11 ns1.000.00
DistinctToListJob-KQNFCOCoreRun10006,280.2 ns60.54 ns56.63 ns0.800.01
DistinctToListJob-BSODDZnet8.010007,831.8 ns47.20 ns44.15 ns1.000.00
DistinctToListJob-KQNFCOCoreRun100000017,175,312.7 ns341,877.48 ns406,980.82 ns0.940.02
DistinctToListJob-BSODDZnet8.0100000018,496,042.9 ns123,385.89 ns96,331.59 ns1.000.00

PaulusParssinen reacted with rocket emoji
@ghostghost added community-contributionIndicates that the PR has been added by a community member area-System.Linq labelsNov 25, 2023
@ghost
Copy link

Tagging subscribers to this area: @dotnet/area-system-linq
See info inarea-owners.md if you want to be subscribed.

Issue Details

Enumerable.HashSetToList fills 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[]).

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();}}
BenchmarkDotNet v0.13.10, Arch LinuxAMD Ryzen 9 5900X, 1 CPU, 24 logical and 12 physical cores.NET SDK 8.0.100  [Host]     : .NET 8.0.0 (8.0.23.53103), X64 RyuJIT AVX2  Job-KQNFCO : .NET 8.0.1 (42.42.42.42424), X64 RyuJIT AVX2  Job-BSODDZ : .NET 8.0.0 (8.0.23.53103), X64 RyuJIT AVX2Runtime=.NET 8.0
MethodJobToolchainCountMeanErrorStdDevRatioRatioSD
DistinctToListJob-KQNFCOCoreRun10119.9 ns2.42 ns2.49 ns0.860.02
DistinctToListJob-BSODDZnet8.010138.2 ns1.33 ns1.11 ns1.000.00
DistinctToListJob-KQNFCOCoreRun10006,280.2 ns60.54 ns56.63 ns0.800.01
DistinctToListJob-BSODDZnet8.010007,831.8 ns47.20 ns44.15 ns1.000.00
DistinctToListJob-KQNFCOCoreRun100000017,175,312.7 ns341,877.48 ns406,980.82 ns0.940.02
DistinctToListJob-BSODDZnet8.0100000018,496,042.9 ns123,385.89 ns96,331.59 ns1.000.00
Author:Windows10CE
Assignees:-
Labels:

area-System.Linq,community-contribution

Milestone:-

@Windows10CE
Copy link
ContributorAuthor

@dotnet-policy-service agree

@huoyaoyuan
Copy link
Member

Can you compare with latest main branch too, and include memory diagnoser?HashSet has a struct enumerator and I'd expect it to be faster with directforeach.

@Windows10CE
Copy link
ContributorAuthor

Windows10CE commentedNov 26, 2023
edited
Loading

Can you compare with latest main branch too, and include memory diagnoser?HashSet has a struct enumerator and I'd expect it to be faster with directforeach.

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:

MethodJobToolchainCountMeanErrorStdDevRatioRatioSDGen0Gen1Gen2AllocatedAlloc Ratio
DistinctToListJob-HVDLNJ.NET 8.010132.3 ns1.97 ns1.85 ns1.000.000.0291--488 B1.00
DistinctToListJob-GVVNDQmain10118.9 ns0.36 ns0.28 ns0.900.010.0291--488 B1.00
DistinctToListJob-NUYUKR#9522410112.1 ns1.75 ns1.64 ns0.850.020.0291--488 B1.00
DistinctToListJob-HVDLNJ.NET 8.010008,013.0 ns151.11 ns155.18 ns1.000.001.29700.0763-21920 B1.00
DistinctToListJob-GVVNDQmain10006,902.9 ns136.73 ns162.76 ns0.860.031.30460.0763-21920 B1.00
DistinctToListJob-NUYUKR#9522410006,623.9 ns98.54 ns92.17 ns0.830.021.30460.0763-21920 B1.00
DistinctToListJob-HVDLNJ.NET 8.0100000019,286,187.6 ns314,736.06 ns262,818.86 ns1.000.00406.2500406.2500406.250022603123 B1.00
DistinctToListJob-GVVNDQmain100000017,551,160.4 ns133,451.94 ns111,438.41 ns0.910.02406.2500406.2500406.250022603123 B1.00
DistinctToListJob-NUYUKR#95224100000017,392,611.3 ns108,270.07 ns101,275.89 ns0.900.01343.7500343.7500343.750022603082 B1.00

Still a very small improvement, but I apologize for the misleading benchmark in the original description 😅

Copy link
Member

@eiriktsarpaliseiriktsarpalis left a 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 :-)

@eiriktsarpaliseiriktsarpalis merged commitddd663d intodotnet:mainNov 27, 2023
@Windows10CEWindows10CE deleted the distinct-tolist-improvements branchNovember 27, 2023 11:49
@github-actionsgithub-actionsbot locked and limited conversation to collaboratorsDec 28, 2023
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.

Reviewers

@eiriktsarpaliseiriktsarpaliseiriktsarpalis approved these changes

Assignees

No one assigned

Labels

area-System.Linqcommunity-contributionIndicates that the PR has been added by a community member

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

3 participants

@Windows10CE@huoyaoyuan@eiriktsarpalis

[8]ページ先頭

©2009-2025 Movatter.jp