|
| 1 | +{-# LANGUAGE CPP #-} |
| 2 | +{-# LANGUAGE BangPatterns #-} |
| 3 | +{-# LANGUAGE RankNTypes #-} |
| 4 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 5 | + |
| 6 | +module Utils.Fold |
| 7 | + ( foldBenchmarks |
| 8 | + , foldWithKeyBenchmarks |
| 9 | + ) where |
| 10 | + |
| 11 | +import Control.Monad.Trans.State.Strict |
| 12 | +#if !(MIN_VERSION_base(4,11,0)) |
| 13 | +import Data.Semigroup (Semigroup((<>))) |
| 14 | +#endif |
| 15 | +import Data.Monoid (Any(..)) |
| 16 | +import Prelude hiding (Foldable(..)) |
| 17 | +import Test.Tasty.Bench (Benchmark, bench, defaultMain, whnf, nf) |
| 18 | +import qualified GHC.Exts |
| 19 | + |
| 20 | +-- | Benchmarks for folds on a structure of @Int@s. |
| 21 | + |
| 22 | +-- See Note [Choice of benchmarks] |
| 23 | +foldBenchmarks |
| 24 | + :: forall f. |
| 25 | + (forall b. (Int -> b -> b) -> b -> f -> b) |
| 26 | + -> (forall b. (b -> Int -> b) -> b -> f -> b) |
| 27 | + -> (forall b. (Int -> b -> b) -> b -> f -> b) |
| 28 | + -> (forall b. (b -> Int -> b) -> b -> f -> b) |
| 29 | + -> (forall m. Monoid m => (Int -> m) -> f -> m) |
| 30 | + -> f |
| 31 | + -> [Benchmark] |
| 32 | +foldBenchmarks foldr foldl foldr' foldl' foldMap xs = |
| 33 | + [-- foldr |
| 34 | + bench "foldr_elem" $ whnf foldr_elem xs |
| 35 | + , bench "foldr_cpsSum" $ whnf foldr_cpsSum xs |
| 36 | + , bench "foldr_cpsOneShotSum" $ whnf foldr_cpsOneShotSum xs |
| 37 | + , bench "foldr_traverseSum" $ whnf foldr_traverseSum xs |
| 38 | + |
| 39 | + -- foldl |
| 40 | + , bench "foldl_skip" $ whnf foldl_elem xs |
| 41 | + , bench "foldl_cpsSum" $ whnf foldl_cpsSum xs |
| 42 | + , bench "foldl_cpsOneShotSum" $ whnf foldl_cpsOneShotSum xs |
| 43 | + , bench "foldl_traverseSum" $ whnf foldl_traverseSum xs |
| 44 | + |
| 45 | + -- foldr' |
| 46 | + , bench "foldr'_sum" $ whnf (foldr' (+) 0) xs |
| 47 | + |
| 48 | + -- foldl' |
| 49 | + , bench "foldl'_sum" $ whnf (foldl' (+) 0) xs |
| 50 | + |
| 51 | + -- foldMap |
| 52 | + , bench "foldMap_elem" $ whnf foldMap_elem xs |
| 53 | + , bench "foldMap_traverseSum" $ whnf foldMap_traverseSum xs |
| 54 | + ] |
| 55 | + where |
| 56 | + foldr_elem :: f -> Bool |
| 57 | + foldr_elem = foldr (\x z -> x == minBound || z) False |
| 58 | + |
| 59 | + foldr_cpsSum :: f -> Int |
| 60 | + foldr_cpsSum xs = foldr (\x k !acc -> k (x + acc)) id xs 0 |
| 61 | + |
| 62 | + foldr_cpsOneShotSum :: f -> Int |
| 63 | + foldr_cpsOneShotSum xs = |
| 64 | + foldr (\x k -> GHC.Exts.oneShot (\ !acc -> k (x + acc))) id xs 0 |
| 65 | + |
| 66 | + foldr_traverseSum :: f -> Int |
| 67 | + foldr_traverseSum xs = |
| 68 | + execState (foldr (\x z -> modify' (+x) *> z) (pure ()) xs) 0 |
| 69 | + |
| 70 | + foldl_elem :: f -> Bool |
| 71 | + foldl_elem = foldl (\z x -> x == minBound || z) False |
| 72 | + |
| 73 | + foldl_cpsSum :: f -> Int |
| 74 | + foldl_cpsSum xs = foldl (\k x !acc -> k (x + acc)) id xs 0 |
| 75 | + |
| 76 | + foldl_cpsOneShotSum :: f -> Int |
| 77 | + foldl_cpsOneShotSum xs = |
| 78 | + foldl (\k x -> GHC.Exts.oneShot (\ !acc -> k (x + acc))) id xs 0 |
| 79 | + |
| 80 | + foldl_traverseSum :: f -> Int |
| 81 | + foldl_traverseSum xs = |
| 82 | + execState (foldl (\z x -> modify' (+x) *> z) (pure ()) xs) 0 |
| 83 | + |
| 84 | + foldMap_elem :: f -> Any |
| 85 | + foldMap_elem = foldMap (\x -> Any (x == minBound)) |
| 86 | + |
| 87 | + foldMap_traverseSum :: f -> Int |
| 88 | + foldMap_traverseSum xs = |
| 89 | + execState (runEffect (foldMap (\x -> Effect (modify' (+x))) xs)) 0 |
| 90 | +{-# INLINE foldBenchmarks #-} |
| 91 | + |
| 92 | +-- | Benchmarks for folds on a structure of @Int@ keys and @Int@ values. |
| 93 | +foldWithKeyBenchmarks |
| 94 | + :: (forall b. (Int -> Int -> b -> b) -> b -> f -> b) |
| 95 | + -> (forall b. (b -> Int -> Int -> b) -> b -> f -> b) |
| 96 | + -> (forall b. (Int -> Int -> b -> b) -> b -> f -> b) |
| 97 | + -> (forall b. (b -> Int -> Int -> b) -> b -> f -> b) |
| 98 | + -> (forall m. Monoid m => (Int -> Int -> m) -> f -> m) |
| 99 | + -> f |
| 100 | + -> [Benchmark] |
| 101 | +foldWithKeyBenchmarks |
| 102 | + foldrWithKey foldlWithKey foldrWithKey' foldlWithKey' foldMapWithKey = |
| 103 | + foldBenchmarks |
| 104 | + (\f -> foldrWithKey (\k x z -> f (k + x) z)) |
| 105 | + (\f -> foldlWithKey (\z k x -> f z (k + x))) |
| 106 | + (\f -> foldrWithKey' (\k x z -> f (k + x) z)) |
| 107 | + (\f -> foldlWithKey' (\z k x -> f z (k + x))) |
| 108 | + (\f -> foldMapWithKey (\k x -> f (k + x))) |
| 109 | +{-# INLINE foldWithKeyBenchmarks #-} |
| 110 | + |
| 111 | +newtype Effect f = Effect { runEffect :: f () } |
| 112 | + |
| 113 | +instance Applicative f => Semigroup (Effect f) where |
| 114 | + Effect f1 <> Effect f2 = Effect (f1 *> f2) |
| 115 | + |
| 116 | +instance Applicative f => Monoid (Effect f) where |
| 117 | + mempty = Effect (pure ()) |
| 118 | +#if !(MIN_VERSION_base(4,11,0)) |
| 119 | + mappend = (<>) |
| 120 | +#endif |
| 121 | + |
| 122 | + |
| 123 | +-- Note [Choice of benchmarks] |
| 124 | +-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 125 | +-- |
| 126 | +-- foldr_elem, foldl_elem |
| 127 | +-- Simple lazy fold that visits every element. In practice: |
| 128 | +-- * Worst case for short-circuiting folds |
| 129 | +-- * Data.Foldable.toList |
| 130 | +-- |
| 131 | +-- foldr_cpsSum, foldr_cpsOneShotSum, foldl_cpsSum, foldl_cpsOneShotSum |
| 132 | +-- The well-known foldl'-via-foldr pattern. GHC.Exts.oneShot is used to help |
| 133 | +-- GHC with optimizations. In practice: |
| 134 | +-- * Used for early-return with an accumulator |
| 135 | +-- * Used by the foldl library |
| 136 | +-- |
| 137 | +-- foldr_traverseSum, foldr_traverseSum |
| 138 | +-- Folding with an effect. In practice: |
| 139 | +-- * Folds defined using foldr, such as Data.Foldable.traverse_ and friends |
| 140 | +-- |
| 141 | +-- foldl', foldr' |
| 142 | +-- Strict folds. |
| 143 | +-- |
| 144 | +-- foldMap_elem |
| 145 | +-- Simple lazy fold that visits every element. In practice: |
| 146 | +-- * Worst case for lazy folds defined using foldMap, such as |
| 147 | +-- Data.Foldable.any, Data.Foldable.find, etc. |
| 148 | +-- |
| 149 | +-- foldMap_traverseSum |
| 150 | +-- Folding with an effect. In practice: |
| 151 | +-- * With the lens library, using traverseOf_ on a foldMap based fold. |
0 commit comments