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

Special case arrays in Enumerable.DefaultIfEmpty#98963

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:defaultifemptyarray
Feb 27, 2024

Conversation

stephentoub
Copy link
Member

Arrays are fixed-length, and DefaultIfEmpty only applies special behaviors on top of the input enumerable if it's empty. Thus, we can special-case arrays, such that if the input is a non-empty array, we just return the original input rather than wrapping it in a new enumerable.

usingBenchmarkDotNet.Attributes;usingBenchmarkDotNet.Running;BenchmarkSwitcher.FromAssembly(typeof(Tests).Assembly).Run(args);[MemoryDiagnoser(false)][HideColumns("Job","Error","StdDev","Median","RatioSD")]publicpartialclassTests{privateint[]_data=Enumerable.Range(0,1000).ToArray();[Benchmark]publicdoubleAverage()=>_data.DefaultIfEmpty().Average();}
MethodToolchainMeanRatioAllocatedAlloc Ratio
Average\main\corerun.exe3,098.3 ns1.0080 B1.00
Average\pr\corerun.exe145.0 ns0.05-0.00

Arrays are fixed-length, and DefaultIfEmpty only applies special behaviors on top of the input enumerable if it's empty. Thus, we can special-case arrays, such that if the input is a non-empty array, we just return the original input rather than wrapping it in a new enumerable.
@ghostghost added the area-System.Linq labelFeb 27, 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

Arrays are fixed-length, and DefaultIfEmpty only applies special behaviors on top of the input enumerable if it's empty. Thus, we can special-case arrays, such that if the input is a non-empty array, we just return the original input rather than wrapping it in a new enumerable.

usingBenchmarkDotNet.Attributes;usingBenchmarkDotNet.Running;BenchmarkSwitcher.FromAssembly(typeof(Tests).Assembly).Run(args);[MemoryDiagnoser(false)][HideColumns("Job","Error","StdDev","Median","RatioSD")]publicpartialclassTests{privateint[]_data=Enumerable.Range(0,1000).ToArray();[Benchmark]publicdoubleAverage()=>_data.DefaultIfEmpty().Average();}
MethodToolchainMeanRatioAllocatedAlloc Ratio
Average\main\corerun.exe3,098.3 ns1.0080 B1.00
Average\pr\corerun.exe145.0 ns0.05-0.00
Author:stephentoub
Assignees:stephentoub
Labels:

area-System.Linq

Milestone:-

@stephentoubstephentoub changed the titleSpecial-case arrays in Enumerable.DefaultIfEmptySpecial case arrays in Enumerable.DefaultIfEmptyFeb 27, 2024
@stephentoubstephentoub merged commit9d9b250 intodotnet:mainFeb 27, 2024
@stephentoubstephentoub deleted the defaultifemptyarray branchFebruary 27, 2024 12:07
@github-actionsgithub-actionsbot locked and limited conversation to collaboratorsMar 29, 2024
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.
Reviewers

@eiriktsarpaliseiriktsarpaliseiriktsarpalis approved these changes

Assignees

@stephentoubstephentoub

Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

2 participants
@stephentoub@eiriktsarpalis

[8]ページ先頭

©2009-2025 Movatter.jp