Skip to content

Commit

Permalink
Avoid OrderBy.ToArray/ToList overheads for length 1 (dotnet#99639)
Browse files Browse the repository at this point in the history
We already special-case length 0. That can trivially be extended to length 1.
  • Loading branch information
stephentoub authored and pull[bot] committed Aug 20, 2024
1 parent 3a6b292 commit 2203250
Showing 1 changed file with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ private abstract partial class OrderedIterator<TElement>
public override TElement[] ToArray()
{
TElement[] buffer = _source.ToArray();
if (buffer.Length == 0)
if (buffer.Length <= 1)
{
return buffer;
}
Expand All @@ -29,11 +29,15 @@ public override List<TElement> ToList()
{
TElement[] buffer = _source.ToArray();

List<TElement> list = new();
if (buffer.Length > 0)
List<TElement> list = new(buffer.Length);
if (buffer.Length >= 2)
{
Fill(buffer, SetCountAndGetSpan(list, buffer.Length));
}
else if (buffer.Length == 1)
{
list.Add(buffer[0]);
}

return list;
}
Expand Down

0 comments on commit 2203250

Please sign in to comment.