Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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.

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

BenchmarkSwitcher.FromAssembly(typeof(Tests).Assembly).Run(args);

[MemoryDiagnoser(false)]
[HideColumns("Job", "Error", "StdDev", "Median", "RatioSD")]
public partial class Tests
{
    private int[] _data = Enumerable.Range(0, 1000).ToArray();

    [Benchmark]
    public double Average() => _data.DefaultIfEmpty().Average();
}
Method Toolchain Mean Ratio Allocated Alloc Ratio
Average \main\corerun.exe 3,098.3 ns 1.00 80 B 1.00
Average \pr\corerun.exe 145.0 ns 0.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.
@ghost ghost assigned stephentoub Feb 27, 2024
@ghost ghost added the area-System.Linq label Feb 27, 2024
@ghost
Copy link

ghost commented Feb 27, 2024

Tagging subscribers to this area: @dotnet/area-system-linq
See info in area-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.

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

BenchmarkSwitcher.FromAssembly(typeof(Tests).Assembly).Run(args);

[MemoryDiagnoser(false)]
[HideColumns("Job", "Error", "StdDev", "Median", "RatioSD")]
public partial class Tests
{
    private int[] _data = Enumerable.Range(0, 1000).ToArray();

    [Benchmark]
    public double Average() => _data.DefaultIfEmpty().Average();
}
Method Toolchain Mean Ratio Allocated Alloc Ratio
Average \main\corerun.exe 3,098.3 ns 1.00 80 B 1.00
Average \pr\corerun.exe 145.0 ns 0.05 - 0.00
Author: stephentoub
Assignees: stephentoub
Labels:

area-System.Linq

Milestone: -

@stephentoub stephentoub changed the title Special-case arrays in Enumerable.DefaultIfEmpty Special case arrays in Enumerable.DefaultIfEmpty Feb 27, 2024
@stephentoub stephentoub merged commit 9d9b250 into dotnet:main Feb 27, 2024
@stephentoub stephentoub deleted the defaultifemptyarray branch February 27, 2024 12:07
@github-actions github-actions bot locked and limited conversation to collaborators Mar 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants