Optimize Enumerable.Min/Max<T> with Comparer<T>.Default intrinsic (#48273)
authorStephen Toub <stoub@microsoft.com>
Sun, 14 Feb 2021 13:45:54 +0000 (08:45 -0500)
committerGitHub <noreply@github.com>
Sun, 14 Feb 2021 13:45:54 +0000 (08:45 -0500)
src/libraries/System.Linq/src/System/Linq/Max.cs
src/libraries/System.Linq/src/System/Linq/Min.cs

index 9dc4c92..0e9c4c0 100644 (file)
@@ -448,11 +448,10 @@ namespace System.Linq
                 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
                     {
@@ -465,6 +464,7 @@ namespace System.Linq
                     }
                     while (value == null);
 
+                    Comparer<TSource> comparer = Comparer<TSource>.Default;
                     while (e.MoveNext())
                     {
                         TSource x = e.Current;
@@ -474,10 +474,7 @@ namespace System.Linq
                         }
                     }
                 }
-            }
-            else
-            {
-                using (IEnumerator<TSource> e = source.GetEnumerator())
+                else
                 {
                     if (!e.MoveNext())
                     {
@@ -488,7 +485,7 @@ namespace System.Linq
                     while (e.MoveNext())
                     {
                         TSource x = e.Current;
-                        if (comparer.Compare(x, value) > 0)
+                        if (Comparer<TSource>.Default.Compare(x, value) > 0)
                         {
                             value = x;
                         }
index 9958c84..f8c7f3b 100644 (file)
@@ -406,11 +406,10 @@ namespace System.Linq
                 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
                     {
@@ -423,6 +422,7 @@ namespace System.Linq
                     }
                     while (value == null);
 
+                    Comparer<TSource> comparer = Comparer<TSource>.Default;
                     while (e.MoveNext())
                     {
                         TSource x = e.Current;
@@ -432,10 +432,7 @@ namespace System.Linq
                         }
                     }
                 }
-            }
-            else
-            {
-                using (IEnumerator<TSource> e = source.GetEnumerator())
+                else
                 {
                     if (!e.MoveNext())
                     {
@@ -446,7 +443,7 @@ namespace System.Linq
                     while (e.MoveNext())
                     {
                         TSource x = e.Current;
-                        if (comparer.Compare(x, value) < 0)
+                        if (Comparer<TSource>.Default.Compare(x, value) < 0)
                         {
                             value = x;
                         }