@@ -29,6 +29,7 @@ import Test.Tasty.QuickCheck (testProperty, (===))
2929import qualified KMerge.Heap as K.Heap
3030import qualified KMerge.LoserTree as K.Tree
3131
32+
3233-- tests and benchmarks for various k-way merge implementations.
3334-- in short: loser tree is optimal in comparison counts performed,
3435-- but mutable heap implementation has lower constant factors.
@@ -57,6 +58,7 @@ main = do
5758 _ <- evaluate $ force input7
5859 _ <- evaluate $ force input5
5960
61+
6062 defaultMainWithIngredients B. benchIngredients $ testGroup " kmerge"
6163 [ testGroup " tests"
6264 [ testGroup " merge"
@@ -75,7 +77,7 @@ main = do
7577 -- (because the input values are unformly random,
7678 -- there shouldn't be a lot of "cheap" leftovers elements,
7779 -- i.e. when other inputs are exhausted, but there are few)
78- [ testCount " sortConcat" 3190 (L. sort . concat ) input8
80+ [ testCount " sortConcat" comparisons8 (L. sort . concat ) input8
7981 , testCount " listMerge" 3479 listMerge input8
8082 , testCount " treeMerge" 2391 treeMerge input8
8183 , testCount " heapMerge" 3168 heapMerge input8
@@ -115,7 +117,7 @@ main = do
115117 -- but I'm too lazy to think how to do that)
116118 --
117119 , testGroup " seven"
118- [ testCount " sortConcat" 2691 (L. sort . concat ) input7
120+ [ testCount " sortConcat" comparisons7 (L. sort . concat ) input7
119121 , testCount " listMerge" 2682 listMerge input7
120122 , testCount " treeMerge" 1992 treeMerge input7
121123 , testCount " heapMerge" 2645 heapMerge input7
@@ -126,7 +128,7 @@ main = do
126128 -- and 2x100 with 3 comparisons.
127129 -- i.e. target is 1200 total comparisons.
128130 , testGroup " five"
129- [ testCount " sortConcat" 1790 (L. sort . concat ) input5
131+ [ testCount " sortConcat" comparisons5 (L. sort . concat ) input5
130132 , testCount " listMerge" 1389 listMerge input5
131133 , testCount " treeMerge" 1291 treeMerge input5
132134 , testCount " heapMerge" 1485 heapMerge input5
@@ -139,7 +141,7 @@ main = do
139141 -- 4x125 elements with 3 comparisons
140142 -- i.e. target is 2000 total comparisons.
141143 , testGroup " levelling-min"
142- [ testCount " sortConcat" 3729 (L. sort . concat ) inputLevellingMin
144+ [ testCount " sortConcat" comparisonsMin (L. sort . concat ) inputLevellingMin
143145 , testCount " listMerge" 2112 listMerge inputLevellingMin
144146 , testCount " treeMerge" 2730 treeMerge inputLevellingMin
145147 , testCount " heapMerge" 2655 heapMerge inputLevellingMin
@@ -153,7 +155,7 @@ main = do
153155 -- 4x 50 elements with 3 comparisons
154156 -- i.e. target is 1400 total comparisons.
155157 , testGroup " levelling-max"
156- [ testCount " sortConcat" 3872 (L. sort . concat ) inputLevellingMax
158+ [ testCount " sortConcat" comparisonsMax (L. sort . concat ) inputLevellingMax
157159 , testCount " listMerge" 1440 listMerge inputLevellingMax
158160 , testCount " treeMerge" 2873 treeMerge inputLevellingMax
159161 , testCount " heapMerge" 1784 heapMerge inputLevellingMax
@@ -440,3 +442,27 @@ mutHeapMerge xss = case [ Heap.Entry x xs | x : xs <- xss ] of
440442 go ! heap (Just (Heap. Entry x xs)) = fmap (x : ) $ case xs of
441443 [] -> K.Heap. extract heap >>= go heap
442444 x': xs' -> K.Heap. replaceRoot heap (Heap. Entry x' xs') >>= go heap . Just
445+
446+ {- ------------------------------------------------------------------------------
447+ Account for differing sort comparisons across base versions
448+ -------------------------------------------------------------------------------}
449+
450+ -- | The 'sort' and 'sortBy' implementations changed as of @base-4.21@.
451+ -- The new implementation performs fewer comparisons on longer lists.
452+ --
453+ -- Because of this, we fall back to the old sort method when the version of
454+ -- @base@ is @4.21@ or greater.
455+ comparisons5 , comparisons7 , comparisons8 , comparisonsMin , comparisonsMax :: Int
456+ #if MIN_VERSION_base(4,21,0)
457+ comparisons5 = 1692
458+ comparisons7 = 2691
459+ comparisons8 = 3389
460+ comparisonsMin = 3606
461+ comparisonsMax = 3820
462+ #else
463+ comparisons5 = 1790
464+ comparisons7 = 2691
465+ comparisons8 = 3190
466+ comparisonsMin = 3729
467+ comparisonsMax = 3872
468+ #endif
0 commit comments