Skip to content

Optimize Enumerable.Min/Max<T> for value types with Comparer<T>.Default intrinsic #48273

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
Feb 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions src/libraries/System.Linq/src/System/Linq/Max.cs
Original file line number Diff line number Diff line change
Expand Up @@ -448,11 +448,10 @@ public static decimal Max(this IEnumerable<decimal> source)
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source);
}

Comparer<TSource> comparer = Comparer<TSource>.Default;
TSource? value = default;
if (value == null)
using (IEnumerator<TSource> e = source.GetEnumerator())
{
using (IEnumerator<TSource> e = source.GetEnumerator())
if (value == null)
{
do
{
Expand All @@ -465,6 +464,7 @@ public static decimal Max(this IEnumerable<decimal> source)
}
while (value == null);

Comparer<TSource> comparer = Comparer<TSource>.Default;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, what is the motivation for not optimizing reference types?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See issue #10050 for more details

while (e.MoveNext())
{
TSource x = e.Current;
Expand All @@ -474,10 +474,7 @@ public static decimal Max(this IEnumerable<decimal> source)
}
}
}
}
else
{
using (IEnumerator<TSource> e = source.GetEnumerator())
else
{
if (!e.MoveNext())
{
Expand All @@ -488,7 +485,7 @@ public static decimal Max(this IEnumerable<decimal> source)
while (e.MoveNext())
{
TSource x = e.Current;
if (comparer.Compare(x, value) > 0)
if (Comparer<TSource>.Default.Compare(x, value) > 0)
{
value = x;
}
Expand Down
13 changes: 5 additions & 8 deletions src/libraries/System.Linq/src/System/Linq/Min.cs
Original file line number Diff line number Diff line change
Expand Up @@ -406,11 +406,10 @@ public static decimal Min(this IEnumerable<decimal> source)
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source);
}

Comparer<TSource> comparer = Comparer<TSource>.Default;
TSource? value = default;
if (value == null)
using (IEnumerator<TSource> e = source.GetEnumerator())
{
using (IEnumerator<TSource> e = source.GetEnumerator())
if (value == null)
{
do
{
Expand All @@ -423,6 +422,7 @@ public static decimal Min(this IEnumerable<decimal> source)
}
while (value == null);

Comparer<TSource> comparer = Comparer<TSource>.Default;
while (e.MoveNext())
{
TSource x = e.Current;
Expand All @@ -432,10 +432,7 @@ public static decimal Min(this IEnumerable<decimal> source)
}
}
}
}
else
{
using (IEnumerator<TSource> e = source.GetEnumerator())
else
{
if (!e.MoveNext())
{
Expand All @@ -446,7 +443,7 @@ public static decimal Min(this IEnumerable<decimal> source)
while (e.MoveNext())
{
TSource x = e.Current;
if (comparer.Compare(x, value) < 0)
if (Comparer<TSource>.Default.Compare(x, value) < 0)
{
value = x;
}
Expand Down