Skip to content

Commit

Permalink
Follow-up to Enumerable.Min/Max Comparer<T>.Default optimization (#48289
Browse files Browse the repository at this point in the history
)

I didn't notice the same pattern was employed in the `Func<TSource, TResult>` overloads, so changing them, too.
  • Loading branch information
stephentoub committed Feb 15, 2021
1 parent 16910d8 commit 6c8f9ff
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
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 @@ -991,11 +991,10 @@ public static decimal Max<TSource>(this IEnumerable<TSource> source, Func<TSourc
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector);
}

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

Comparer<TResult> comparer = Comparer<TResult>.Default;
while (e.MoveNext())
{
TResult x = selector(e.Current);
Expand All @@ -1017,10 +1017,7 @@ public static decimal Max<TSource>(this IEnumerable<TSource> source, Func<TSourc
}
}
}
}
else
{
using (IEnumerator<TSource> e = source.GetEnumerator())
else
{
if (!e.MoveNext())
{
Expand All @@ -1031,7 +1028,7 @@ public static decimal Max<TSource>(this IEnumerable<TSource> source, Func<TSourc
while (e.MoveNext())
{
TResult x = selector(e.Current);
if (comparer.Compare(x, value) > 0)
if (Comparer<TResult>.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 @@ -907,11 +907,10 @@ public static decimal Min<TSource>(this IEnumerable<TSource> source, Func<TSourc
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector);
}

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

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

0 comments on commit 6c8f9ff

Please sign in to comment.