@@ -73,7 +73,8 @@ type SortDirection =
7373/// shows the order of the elements with equal keys.
7474/// <c >Sort</c > and <c >SortDescending</c > compare whole elements and pairs never compare equal, so they take
7575/// <see cref =" T:FSharp.Collections.Immutable.Tests.KeyedItem " /> elements instead, and the instance creates and reads the
76- /// collection itself, as <c >SeqOps.Unzip</c > does for its pairs.
76+ /// collection itself, as <c >SeqOps.Unzip</c > does for its pairs. The same holds for the two functions that sort integers:
77+ /// the tests of the allocation of the descending sorts need elements that a comparison boxes.
7778/// </remarks >
7879type StableSortOps < 'C > = {
7980 /// The name of the module, for the messages of the assertions.
@@ -91,6 +92,11 @@ type StableSortOps<'C> = {
9192 SortDescending : KeyedItem array -> KeyedItem array
9293 SortWith : ( struct ( int * char ) -> struct ( int * char ) -> int ) -> 'C -> 'C
9394 SortWithComparer : IComparer < struct ( int * char )> -> 'C -> 'C
95+
96+ /// Sorts a collection of the given integers with <c >sortDescending</c >.
97+ SortIntegersDescending : int array -> int array
98+ /// Sorts a collection of the given integers with <c >sortByDescending</c >, by the integers themselves.
99+ SortIntegersByDescending : int array -> int array
94100}
95101
96102/// <summary >
@@ -130,6 +136,23 @@ type StableSortContract<'C> (ops : StableSortOps<'C>) =
130136 /// A projection that throws.
131137 static let throwingProjection ( _ : struct ( int * char )) : int = raise ( NotSupportedException " The projection fails" )
132138
139+ /// 10 000 integers with many duplicates, in a fixed pseudo-random order.
140+ static let manyIntegers () =
141+ let random = Random 42
142+ Array.init 10_000 ( fun _ -> random.Next 1000 )
143+
144+ /// What a sort of 10 000 integers may allocate: the arrays of the sort and the tree of the result need about 1.2 MB,
145+ /// and the boxes of the integers, which a comparison allocates when it goes through the generic operator, 6.8 MB more.
146+ static let allocationLimit = 3_000_000 L
147+
148+ /// The bytes that an action allocates on the current thread when it runs a second time, after the first run has
149+ /// compiled the code.
150+ static let allocatedBy ( action : unit -> unit ) =
151+ action ()
152+ let before = GC.GetAllocatedBytesForCurrentThread ()
153+ action ()
154+ GC.GetAllocatedBytesForCurrentThread () - before
155+
133156 /// The collection of the input pairs and the elements that it enumerates.
134157 let pairs () =
135158 let collection = ops.OfSeq Inputs.ManyPairs
@@ -277,3 +300,51 @@ type StableSortContract<'C> (ops : StableSortOps<'C>) =
277300 ( fun () -> ops.SortWith throwingComparer collection |> ignore),
278301 $" %s {ops.Name}.sortWith must throw what List.sortWith throws when the comparer throws"
279302 )
303+
304+ // The projection of sortByDescending runs in the comparisons, twice for each of them, as it does in
305+ // List.sortByDescending. A sort that computed the keys first would call it once for each element.
306+
307+ [<TestMethod>]
308+ member _. ``sortByDescending calls the projection in the comparisons as List.sortByDescending does`` () =
309+ let struct ( collection , elements ) = pairs ()
310+
311+ let record ( calls : ResizeArray < struct ( int * char )>) ( pair : struct ( int * char )) =
312+ calls.Add pair
313+ key pair
314+
315+ let expected = ResizeArray ()
316+ List.sortByDescending ( record expected) ( List.ofArray elements)
317+ |> ignore
318+ let actual = ResizeArray ()
319+ ops.SortByDescending ( record actual) collection |> ignore
320+
321+ CollectionAssert.AreEqual (
322+ expected.ToArray (),
323+ actual.ToArray (),
324+ $" %s {ops.Name}.sortByDescending must call the projection in the comparisons, as List.sortByDescending does"
325+ )
326+
327+ // Seq.sortDescending and Seq.sortByDescending are inline: in a generic function they compare with the generic
328+ // operator, which boxes the integers in every comparison and allocates 7.5 MB for 10 000 of them
329+
330+ [<TestMethod>]
331+ member _. ``sortDescending allocates no box for an integer in a comparison`` () =
332+ let items = manyIntegers ()
333+ let allocated = allocatedBy ( fun () -> ops.SortIntegersDescending items |> ignore)
334+
335+ Assert.IsLessThan (
336+ allocationLimit,
337+ allocated,
338+ $" %s {ops.Name}.sortDescending of 10 000 integers must not box them in the comparisons"
339+ )
340+
341+ [<TestMethod>]
342+ member _. ``sortByDescending allocates no box for a key in a comparison`` () =
343+ let items = manyIntegers ()
344+ let allocated = allocatedBy ( fun () -> ops.SortIntegersByDescending items |> ignore)
345+
346+ Assert.IsLessThan (
347+ allocationLimit,
348+ allocated,
349+ $" %s {ops.Name}.sortByDescending of 10 000 integers must not box the keys in the comparisons"
350+ )
0 commit comments