-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Reduce OrderBy overheads for default comparer #76418
Conversation
If OrderBy is used with a value type key and and a default or no comparer is specified, we can use `Comparer<TKey>.Default.Compare` to avoid the interface dispatch. This optimization can be taken further, but for now it handles a common set of cases.
Tagging subscribers to this area: @dotnet/area-system-linq Issue DetailsIf OrderBy is used with a value type key and and a default or no comparer is specified, we can use [Params(10, 100, 1_000, 1_000_000)]
public int Length { get; set; }
private int[] _values;
[GlobalSetup]
public void Setup() => _values = Enumerable.Range(0, Length).Reverse().ToArray();
[Benchmark]
public int[] OrderByToArray() => _values.OrderBy(i => i).ToArray();
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks
If OrderBy is used with a value type key and and a default or no comparer is specified, we can use
Comparer<TKey>.Default.Compare
to avoid the interface dispatch. This optimization can be taken further, but for now it handles a common set of cases. (We can subsequently look into a variety of improvements, such as a cheaper way to implement a stable sort, doing the sort in-place in the buffered output when conditions are ammenable, extending these checks for using the default comparer into chained OrderBy/ThenBy cases, etc.)