Skip to content
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

Performance modifying change to LINQ OrderBy(...).First{OrDefault}(...) #19683

Closed
danmoseley opened this issue Jul 24, 2020 · 0 comments · Fixed by #19949
Closed

Performance modifying change to LINQ OrderBy(...).First{OrDefault}(...) #19683

danmoseley opened this issue Jul 24, 2020 · 0 comments · Fixed by #19949
Assignees
Labels
breaking-change Indicates a .NET Core breaking change 🏁 Release: .NET 5 Work items for the .NET 5 release doc-idea Indicates issues that are suggestions for new topics [org][type][category]

Comments

@danmoseley
Copy link
Member

danmoseley commented Jul 24, 2020

Complexity of OrderBy(...).First{OrDefault}(...) increased

[Brief description of the change]

Version introduced

.NET 5.0

Old behavior

As of .NET Core 1.0, OrderBy(...).First{OrDefault}(...) operated with O(N) complexity: since only the first (or default) element is required, it can simply complete the enumeration once to find it. However, the supplied predicate is invoked exactly N times.

Original change was dotnet/corefx#2401

New behavior

As of .NET 5.0, we reverted to the .NET Framework behavior. OrderBy(...).First{OrDefault}(...) operates with O(N log N) complexity, but may invoke the supplied predicate fewer than N times.

See issue dotnet/runtime#31554 and change dotnet/runtime#36643

Reason for change

Community feedback that the cost of potentially invoking the predicate more times outweighed the benefit of a lower complexity operation overall.

Recommended action

No action required.

Category

  • Core .NET libraries

Affected APIs

The behavior change only occurs when using these API together.

Enumerable.FirstOrDefault
Enumerable.First
Enumerable.OrderBy

Issue metadata

  • Issue type: breaking-change
@dotnet-bot dotnet-bot added ⌚ Not Triaged Not triaged breaking-change Indicates a .NET Core breaking change labels Jul 24, 2020
@danmoseley danmoseley changed the title Revert performance optimization to LINQ OrderBy(...).First{OrDefault}(...) Performance modifying change to LINQ OrderBy(...).First{OrDefault}(...) Jul 24, 2020
@gewarren gewarren self-assigned this Jul 27, 2020
@gewarren gewarren added doc-idea Indicates issues that are suggestions for new topics [org][type][category] 🏁 Release: .NET 5 Work items for the .NET 5 release and removed ⌚ Not Triaged Not triaged labels Jul 27, 2020
gewarren added a commit to gewarren/docs that referenced this issue Aug 10, 2020
gewarren added a commit that referenced this issue Aug 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change Indicates a .NET Core breaking change 🏁 Release: .NET 5 Work items for the .NET 5 release doc-idea Indicates issues that are suggestions for new topics [org][type][category]
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants