Skip to content

Avoid OrderBy.ToArray/ToList overheads for length 1 #99639

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 15, 2024

Conversation

stephentoub
Copy link
Member

We already special-case length 0. That can trivially be extended to length 1.

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

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

[MemoryDiagnoser(false)]
[HideColumns("Job", "Error", "StdDev", "Median", "RatioSD")]
public class Tests
{
    private IEnumerable<string> _data;

    [Params(1)]
    public int Count { get; set; }

    [GlobalSetup]
    public void Setup() => _data = Enumerable.Range(0, Count).Select(i => i.ToString());

    [Benchmark]
    public string[] OrderArray() => _data.Order().ToArray();

    [Benchmark]
    public List<string> OrderList() => _data.Order().ToList();
}
Method Toolchain Count Mean Ratio Allocated Alloc Ratio
OrderArray \main\corerun.exe 1 85.30 ns 1.00 336 B 1.00
OrderArray \pr\corerun.exe 1 33.53 ns 0.39 120 B 0.36
OrderList \main\corerun.exe 1 91.46 ns 1.00 392 B 1.00
OrderList \pr\corerun.exe 1 42.60 ns 0.47 184 B 0.47
We already special-case length 0. That can trivially be extended to length 1.
@stephentoub stephentoub added this to the 9.0.0 milestone Mar 12, 2024
Copy link
Contributor

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

@stephentoub stephentoub merged commit 084c9db into dotnet:main Mar 15, 2024
111 checks passed
@stephentoub stephentoub deleted the orderby1 branch March 15, 2024 16:03
@github-actions github-actions bot locked and limited conversation to collaborators Apr 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
3 participants