[corlib] Array qsort without head allocation. Fixes #20922
authorMarek Safar <marek.safar@gmail.com>
Fri, 27 Jun 2014 10:43:03 +0000 (12:43 +0200)
committerMarek Safar <marek.safar@gmail.com>
Fri, 27 Jun 2014 10:44:59 +0000 (12:44 +0200)
mcs/class/corlib/System/Array.cs
mcs/class/corlib/System/Int32.cs

index b3272b16a96fd72a8feadcedef41ad267b0642df..519b8d1a09a4d649aeedf51e9275ef2bb8edcd91 100644 (file)
@@ -1480,9 +1480,9 @@ namespace System
                        return false;
                }
                
-               private static void qsort (Array keys, Array items, int low0, int high0, IComparer comparer)
+               unsafe static void qsort (Array keys, Array items, int low0, int high0, IComparer comparer)
                {
-                       QSortStack[] stack = new QSortStack[32];
+                       QSortStack* stack = stackalloc QSortStack [32];
                        const int QSORT_THRESHOLD = 7;
                        int high, low, mid, i, k;
                        object key, hi, lo;
@@ -1934,9 +1934,9 @@ namespace System
                        return false;
                }
                
-               private static void qsort<T, U> (T[] keys, U[] items, int low0, int high0) where T : IComparable<T>
+               unsafe static void qsort<T, U> (T[] keys, U[] items, int low0, int high0) where T : IComparable<T>
                {
-                       QSortStack[] stack = new QSortStack[32];
+                       QSortStack* stack = stackalloc QSortStack [32];
                        const int QSORT_THRESHOLD = 7;
                        int high, low, mid, i, k;
                        int sp = 1;
@@ -2043,9 +2043,9 @@ namespace System
                }               
 
                // Specialized version for items==null
-               private static void qsort<T> (T[] keys, int low0, int high0) where T : IComparable<T>
+               unsafe static void qsort<T> (T[] keys, int low0, int high0) where T : IComparable<T>
                {
-                       QSortStack[] stack = new QSortStack[32];
+                       QSortStack* stack = stackalloc QSortStack [32];
                        const int QSORT_THRESHOLD = 7;
                        int high, low, mid, i, k;
                        int sp = 1;
@@ -2232,9 +2232,9 @@ namespace System
                        return false;
                }
                
-               private static void qsort<K, V> (K [] keys, V [] items, int low0, int high0, IComparer<K> comparer)
+               unsafe static void qsort<K, V> (K [] keys, V [] items, int low0, int high0, IComparer<K> comparer)
                {
-                       QSortStack[] stack = new QSortStack[32];
+                       QSortStack* stack = stackalloc QSortStack [32];
                        const int QSORT_THRESHOLD = 7;
                        int high, low, mid, i, k;
                        IComparable<K> gcmp;
@@ -2378,9 +2378,9 @@ namespace System
                }
 
                // Specialized version for items==null
-               private static void qsort<K> (K [] keys, int low0, int high0, IComparer<K> comparer)
+               unsafe static void qsort<K> (K [] keys, int low0, int high0, IComparer<K> comparer)
                {
-                       QSortStack[] stack = new QSortStack[32];
+                       QSortStack* stack = stackalloc QSortStack [32];
                        const int QSORT_THRESHOLD = 7;
                        int high, low, mid, i, k;
                        IComparable<K> gcmp;
@@ -2535,9 +2535,9 @@ namespace System
                        return false;
                }
                
-               private static void qsort<T> (T [] array, int low0, int high0, Comparison<T> compare)
+               unsafe static void qsort<T> (T [] array, int low0, int high0, Comparison<T> compare)
                {
-                       QSortStack[] stack = new QSortStack[32];
+                       QSortStack* stack = stackalloc QSortStack [32];
                        const int QSORT_THRESHOLD = 7;
                        int high, low, mid, i, k;
                        int sp = 1;
index 981a2f01ffc0baed23d1aa5526f6344508f7896a..39ff76d15f8b21a1b7e374be0ea2fb3988cea0f3 100644 (file)
@@ -31,6 +31,7 @@
 
 using System.Globalization;
 using System.Threading;
+using System.Runtime.CompilerServices;
 
 namespace System {
        
@@ -77,12 +78,10 @@ namespace System {
 
                public int CompareTo (int value)
                {
-                       if (m_value == value)
-                               return 0;
-                       if (m_value > value)
-                               return 1;
-                       else
-                               return -1;
+                       return
+                               m_value == value ? 0 :
+                               m_value > value ? 1 :
+                               -1;
                }
 
                public bool Equals (int obj)