@@ -987,8 +987,12 @@ module internal Array =
987987
988988 open System
989989
990- let inline fastComparerForArraySort < 't when 't : comparison > () =
991- LanguagePrimitives.FastGenericComparerCanBeNull< 't>
990+ let inline getInternalComparer < 't when 't : comparison > () =
991+ // Previously a "comparer" was returned that could be null, which was for optimized Array.Sort
992+ // but we now mainly return Comparer.Default (and FastGenericComparerInternal more so)
993+ // which is also optimized in Array.Sort
994+ // ** this comment can be destroyed sometime in the future, it is just as a breadcrumb for review **
995+ LanguagePrimitives.FastGenericComparerInternal< 't>
992996
993997 // The input parameter should be checked by callers if necessary
994998 let inline zeroCreateUnchecked ( count : int ) =
@@ -1086,26 +1090,26 @@ module internal Array =
10861090 let keys = zeroCreateUnchecked array.Length
10871091 for i = 0 to array.Length - 1 do
10881092 keys.[ i] <- projection array.[ i]
1089- Array.Sort<_, _>( keys, array, fastComparerForArraySort ())
1093+ Array.Sort<_, _>( keys, array, getInternalComparer ())
10901094
10911095 let unstableSortInPlace ( array : array < 'T >) =
10921096 let len = array.Length
10931097 if len < 2 then ()
1094- else Array.Sort<_>( array, fastComparerForArraySort ())
1098+ else Array.Sort<_>( array, getInternalComparer ())
10951099
1096- let stableSortWithKeysAndComparer ( cFast : IComparer < 'Key >) ( c : IComparer < 'Key >) ( array : array < 'T >) ( keys : array < 'Key >) =
1100+ let stableSortWithKeysAndComparer ( c : IComparer < 'Key >) ( array : array < 'T >) ( keys : array < 'Key >) =
10971101 // 'places' is an array or integers storing the permutation performed by the sort
10981102 let places = zeroCreateUnchecked array.Length
10991103 for i = 0 to array.Length - 1 do
11001104 places.[ i] <- i
1101- System.Array.Sort<_, _>( keys, places, cFast )
1105+ System.Array.Sort<_, _>( keys, places, c )
11021106 // 'array2' is a copy of the original values
11031107 let array2 = ( array.Clone() :?> array< 'T>)
11041108
11051109 // Walk through any chunks where the keys are equal
11061110 let mutable i = 0
11071111 let len = array.Length
1108- let intCompare = fastComparerForArraySort < int>()
1112+ let intCompare = getInternalComparer < int>()
11091113
11101114 while i < len do
11111115 let mutable j = i
@@ -1120,9 +1124,8 @@ module internal Array =
11201124 i <- j
11211125
11221126 let stableSortWithKeys ( array : array < 'T >) ( keys : array < 'Key >) =
1123- let cFast = fastComparerForArraySort()
1124- let c = LanguagePrimitives.FastGenericComparer< 'Key>
1125- stableSortWithKeysAndComparer cFast c array keys
1127+ let c = getInternalComparer()
1128+ stableSortWithKeysAndComparer c array keys
11261129
11271130 let stableSortInPlaceBy ( projection : 'T -> 'U ) ( array : array < 'T >) =
11281131 let len = array.Length
@@ -1138,13 +1141,11 @@ module internal Array =
11381141 let len = array.Length
11391142 if len < 2 then ()
11401143 else
1141- let cFast = LanguagePrimitives.FastGenericComparerCanBeNull< 'T>
1142- match cFast with
1143- | null ->
1144+ if LanguagePrimitives.EquivalentForStableAndUnstableSort< 'T> then
11441145 // An optimization for the cases where the keys and values coincide and do not have identity, e.g. are integers
11451146 // In this case an unstable sort is just as good as a stable sort (and faster)
11461147 Array.Sort<_, _>( array, null )
1147- | _ ->
1148+ else
11481149 // 'keys' is an array storing the projected keys
11491150 let keys = ( array.Clone() :?> array< 'T>)
11501151 stableSortWithKeys array keys
@@ -1155,7 +1156,7 @@ module internal Array =
11551156 let keys = ( array.Clone() :?> array< 'T>)
11561157 let comparer = OptimizedClosures.FSharpFunc<_, _, _>. Adapt( comparer)
11571158 let c = { new IComparer< 'T> with member __.Compare ( x , y ) = comparer.Invoke( x, y) }
1158- stableSortWithKeysAndComparer c c array keys
1159+ stableSortWithKeysAndComparer c array keys
11591160
11601161 let inline subUnchecked startIndex count ( array : 'T []) =
11611162 let res = zeroCreateUnchecked count : 'T[]
@@ -1204,4 +1205,4 @@ module internal Seq =
12041205 while ( e.MoveNext()) do res <- e.Current
12051206 ValueSome( res)
12061207 else
1207- ValueNone
1208+ ValueNone
0 commit comments