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

Use Iterator<T>.TryGetFirst in Enumerable.Any()#99218

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
stephentoub merged 1 commit intodotnet:mainfromstephentoub:anyiterator
Mar 4, 2024

Conversation

stephentoub
Copy link
Member

Enumerable.Any() currently uses TryGetNonEnumeratedCount, comparing the result to 0, and TryGetNonEnumeratedCount usesIterator<T>.GetCount(onlyIfCheap: true). But this leaves out iterators for which it's not cheap to compute the count; for these, however, we can now benefit from everyIterator<T> providing aTryGetFirst; itsfound result can be used as the result ofAny. This PR inlines TryGetNonEnumeratedCount into Any and then updates its use ofIterator<T> to first use GetCount and then fall back to using TryGetFirst if GetCount is unsuccessful.

MethodToolchainSourceMeanRatioAllocatedAlloc Ratio
Any\main\corerun.exe03.346 ns1.00-NA
Any\pr\corerun.exe03.277 ns0.98-NA
Any\main\corerun.exe12.625 ns1.00-NA
Any\pr\corerun.exe12.315 ns0.88-NA
Any\main\corerun.exe216.728 ns1.0056 B1.00
Any\pr\corerun.exe24.154 ns0.25-0.00
Any\main\corerun.exe313.110 ns1.00-NA
Any\pr\corerun.exe312.690 ns0.97-NA
Any\main\corerun.exe414.299 ns1.00-NA
Any\pr\corerun.exe412.912 ns0.90-NA
Any\main\corerun.exe53.741 ns1.00-NA
Any\pr\corerun.exe53.643 ns0.97-NA
Any\main\corerun.exe611.118 ns1.00-NA
Any\pr\corerun.exe68.741 ns0.79-NA
Any\main\corerun.exe747.425 ns1.00160 B1.00
Any\pr\corerun.exe748.630 ns1.03160 B1.00
Any\main\corerun.exe866.663 ns1.00328 B1.00
Any\pr\corerun.exe89.842 ns0.15-0.00
Any\main\corerun.exe912.304 ns1.00-NA
Any\pr\corerun.exe917.323 ns1.40-NA
Any\main\corerun.exe1012.027 ns1.0040 B1.00
Any\pr\corerun.exe1010.224 ns0.8440 B1.00
Any\main\corerun.exe1126.032 ns1.0096 B1.00
Any\pr\corerun.exe1114.288 ns0.5540 B0.42
Any\main\corerun.exe1230.752 ns1.00104 B1.00
Any\pr\corerun.exe1215.216 ns0.4940 B0.38
Any\main\corerun.exe13581.665 ns1.00520 B1.00
Any\pr\corerun.exe13195.656 ns0.3440 B0.08
Any\main\corerun.exe141,405.573 ns1.001576 B1.00
Any\pr\corerun.exe14285.060 ns0.2040 B0.03
Any\main\corerun.exe1554.888 ns1.0096 B1.00
Any\pr\corerun.exe1547.273 ns0.8640 B0.42
Any\main\corerun.exe16361.296 ns1.00512 B1.00
Any\pr\corerun.exe16173.690 ns0.4840 B0.08
Any\main\corerun.exe1746.332 ns1.00168 B1.00
Any\pr\corerun.exe1746.564 ns1.01168 B1.00
Any\main\corerun.exe1865.017 ns1.00336 B1.00
Any\pr\corerun.exe1814.699 ns0.2340 B0.12
Any\main\corerun.exe1937.565 ns1.0096 B1.00
Any\pr\corerun.exe1923.274 ns0.6240 B0.42
usingBenchmarkDotNet.Attributes;usingBenchmarkDotNet.Running;BenchmarkSwitcher.FromAssembly(typeof(Tests).Assembly).Run(args);[MemoryDiagnoser(false)][HideColumns("Job","Error","StdDev","Median","RatioSD")]publicpartialclassTests{privateIEnumerable<int>_data;[Params(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19)]publicintSource{get;set;}[GlobalSetup]publicvoidSetup(){IEnumerable<int>data=Source>=10?Iterations(100):Enumerable.Range(0,100).ToArray();_data=(Source%10)switch{0=>data,1=>data.Select(i=>i),2=>data.Where(i=>i%2==0).Select(i=>i),3=>data.Order(),4=>data.OrderBy(i=>i).Select(i=>i),5=>data.Skip(20).Take(10),6=>data.Reverse(),7=>data.SelectMany(i=>newint[]{i}),8=>data.Distinct(),9=>data.Concat(data),            _=>thrownewNotSupportedException()};}[Benchmark]publicboolAny()=>_data.Any();privatestaticIEnumerable<int>Iterations(intcount){for(inti=0;i<count;i++)yieldreturni;}}

Tornhoof and am11 reacted with thumbs up emojiShreyasJejurkar reacted with rocket emoji
Enumerable.Any() currently uses TryGetNonEnumeratedCount, comparing the result to 0, and TryGetNonEnumeratedCount uses `Iterator<T>.GetCount(onlyIfCheap: true)`. But this leaves out iterators for which it's not cheap to compute the count; for these, however, we can now benefit from every `Iterator<T>` providing a `TryGetFirst`; its `found` result can be used as the result of `Any`.  This PR inlines TryGetNonEnumeratedCount into Any and then updates its use of `Iterator<T>` to first use GetCount and then fall back to using TryGetFirst if GetCount is unsuccessful.
@stephentoubstephentoub added area-System.Linq tenet-performancePerformance related issue labelsMar 4, 2024
@stephentoubstephentoub added this to the9.0.0 milestoneMar 4, 2024
@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.Any() currently uses TryGetNonEnumeratedCount, comparing the result to 0, and TryGetNonEnumeratedCount usesIterator<T>.GetCount(onlyIfCheap: true). But this leaves out iterators for which it's not cheap to compute the count; for these, however, we can now benefit from everyIterator<T> providing aTryGetFirst; itsfound result can be used as the result ofAny. This PR inlines TryGetNonEnumeratedCount into Any and then updates its use ofIterator<T> to first use GetCount and then fall back to using TryGetFirst if GetCount is unsuccessful.

MethodToolchainSourceMeanRatioAllocatedAlloc Ratio
Any\main\corerun.exe03.346 ns1.00-NA
Any\pr\corerun.exe03.277 ns0.98-NA
Any\main\corerun.exe12.625 ns1.00-NA
Any\pr\corerun.exe12.315 ns0.88-NA
Any\main\corerun.exe216.728 ns1.0056 B1.00
Any\pr\corerun.exe24.154 ns0.25-0.00
Any\main\corerun.exe313.110 ns1.00-NA
Any\pr\corerun.exe312.690 ns0.97-NA
Any\main\corerun.exe414.299 ns1.00-NA
Any\pr\corerun.exe412.912 ns0.90-NA
Any\main\corerun.exe53.741 ns1.00-NA
Any\pr\corerun.exe53.643 ns0.97-NA
Any\main\corerun.exe611.118 ns1.00-NA
Any\pr\corerun.exe68.741 ns0.79-NA
Any\main\corerun.exe747.425 ns1.00160 B1.00
Any\pr\corerun.exe748.630 ns1.03160 B1.00
Any\main\corerun.exe866.663 ns1.00328 B1.00
Any\pr\corerun.exe89.842 ns0.15-0.00
Any\main\corerun.exe912.304 ns1.00-NA
Any\pr\corerun.exe917.323 ns1.40-NA
Any\main\corerun.exe1012.027 ns1.0040 B1.00
Any\pr\corerun.exe1010.224 ns0.8440 B1.00
Any\main\corerun.exe1126.032 ns1.0096 B1.00
Any\pr\corerun.exe1114.288 ns0.5540 B0.42
Any\main\corerun.exe1230.752 ns1.00104 B1.00
Any\pr\corerun.exe1215.216 ns0.4940 B0.38
Any\main\corerun.exe13581.665 ns1.00520 B1.00
Any\pr\corerun.exe13195.656 ns0.3440 B0.08
Any\main\corerun.exe141,405.573 ns1.001576 B1.00
Any\pr\corerun.exe14285.060 ns0.2040 B0.03
Any\main\corerun.exe1554.888 ns1.0096 B1.00
Any\pr\corerun.exe1547.273 ns0.8640 B0.42
Any\main\corerun.exe16361.296 ns1.00512 B1.00
Any\pr\corerun.exe16173.690 ns0.4840 B0.08
Any\main\corerun.exe1746.332 ns1.00168 B1.00
Any\pr\corerun.exe1746.564 ns1.01168 B1.00
Any\main\corerun.exe1865.017 ns1.00336 B1.00
Any\pr\corerun.exe1814.699 ns0.2340 B0.12
Any\main\corerun.exe1937.565 ns1.0096 B1.00
Any\pr\corerun.exe1923.274 ns0.6240 B0.42
usingBenchmarkDotNet.Attributes;usingBenchmarkDotNet.Running;BenchmarkSwitcher.FromAssembly(typeof(Tests).Assembly).Run(args);[MemoryDiagnoser(false)][HideColumns("Job","Error","StdDev","Median","RatioSD")]publicpartialclassTests{privateIEnumerable<int>_data;[Params(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19)]publicintSource{get;set;}[GlobalSetup]publicvoidSetup(){IEnumerable<int>data=Source>=10?Iterations(100):Enumerable.Range(0,100).ToArray();_data=(Source%10)switch{0=>data,1=>data.Select(i=>i),2=>data.Where(i=>i%2==0).Select(i=>i),3=>data.Order(),4=>data.OrderBy(i=>i).Select(i=>i),5=>data.Skip(20).Take(10),6=>data.Reverse(),7=>data.SelectMany(i=>newint[]{i}),8=>data.Distinct(),9=>data.Concat(data),            _=>thrownewNotSupportedException()};}[Benchmark]publicboolAny()=>_data.Any();privatestaticIEnumerable<int>Iterations(intcount){for(inti=0;i<count;i++)yieldreturni;}}
Author:stephentoub
Assignees:-
Labels:

area-System.Linq,tenet-performance

Milestone:9.0.0

@stephentoubstephentoub merged commit0d74d1d intodotnet:mainMar 4, 2024
@stephentoubstephentoub deleted the anyiterator branchMarch 4, 2024 15:45
@github-actionsgithub-actionsbot locked and limited conversation to collaboratorsApr 4, 2024
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.
Reviewers

@eiriktsarpaliseiriktsarpaliseiriktsarpalis approved these changes

Assignees

@stephentoubstephentoub

Labels
area-System.Linqtenet-performancePerformance related issue
Projects
None yet
Milestone
9.0.0
Development

Successfully merging this pull request may close these issues.

2 participants
@stephentoub@eiriktsarpalis

[8]ページ先頭

©2009-2025 Movatter.jp