Merge pull request #301 from treeowl/map-inline-map
[packages/containers.git] / benchmarks / Map.hs
1 {-# LANGUAGE CPP #-}
2 {-# LANGUAGE BangPatterns #-}
3 module Main where
4
5 import Control.Applicative (Const(Const, getConst), pure)
6 import Control.DeepSeq (rnf)
7 import Control.Exception (evaluate)
8 import Criterion.Main (bench, defaultMain, whnf, nf)
9 import Data.Functor.Identity (Identity(..))
10 import Data.List (foldl')
11 import qualified Data.Map as M
12 import Data.Map (alterF)
13 import Data.Maybe (fromMaybe)
14 import Data.Functor ((<$))
15 #if __GLASGOW_HASKELL__ >= 708
16 import Data.Coerce
17 #endif
18 import Prelude hiding (lookup)
19
20 main = do
21 let m = M.fromAscList elems :: M.Map Int Int
22 m_even = M.fromAscList elems_even :: M.Map Int Int
23 m_odd = M.fromAscList elems_odd :: M.Map Int Int
24 evaluate $ rnf [m, m_even, m_odd]
25 defaultMain
26 [ bench "lookup absent" $ whnf (lookup evens) m_odd
27 , bench "lookup present" $ whnf (lookup evens) m_even
28 , bench "map" $ whnf (M.map (+ 1)) m
29 , bench "map really" $ nf (M.map (+ 2)) m
30 , bench "<$" $ whnf ((1 :: Int) <$) m
31 , bench "<$ really" $ nf ((2 :: Int) <$) m
32 , bench "alterF lookup absent" $ whnf (atLookup evens) m_odd
33 , bench "alterF lookup present" $ whnf (atLookup evens) m_even
34 , bench "alterF no rules lookup absent" $ whnf (atLookupNoRules evens) m_odd
35 , bench "alterF no rules lookup present" $ whnf (atLookupNoRules evens) m_even
36 , bench "insert absent" $ whnf (ins elems_even) m_odd
37 , bench "insert present" $ whnf (ins elems_even) m_even
38 , bench "alterF insert absent" $ whnf (atIns elems_even) m_odd
39 , bench "alterF insert present" $ whnf (atIns elems_even) m_even
40 , bench "alterF no rules insert absent" $ whnf (atInsNoRules elems_even) m_odd
41 , bench "alterF no rules insert present" $ whnf (atInsNoRules elems_even) m_even
42 , bench "delete absent" $ whnf (del evens) m_odd
43 , bench "delete present" $ whnf (del evens) m
44 , bench "alterF delete absent" $ whnf (atDel evens) m_odd
45 , bench "alterF delete present" $ whnf (atDel evens) m
46 , bench "alterF no rules delete absent" $ whnf (atDelNoRules evens) m_odd
47 , bench "alterF no rules delete present" $ whnf (atDelNoRules evens) m
48 , bench "alter absent" $ whnf (alt id evens) m_odd
49 , bench "alter insert" $ whnf (alt (const (Just 1)) evens) m_odd
50 , bench "alter update" $ whnf (alt id evens) m_even
51 , bench "alter delete" $ whnf (alt (const Nothing) evens) m
52 , bench "alterF alter absent" $ whnf (atAlt id evens) m_odd
53 , bench "alterF alter insert" $ whnf (atAlt (const (Just 1)) evens) m_odd
54 , bench "alterF alter update" $ whnf (atAlt id evens) m_even
55 , bench "alterF alter delete" $ whnf (atAlt (const Nothing) evens) m
56 , bench "alterF no rules alter absent" $ whnf (atAltNoRules id evens) m_odd
57 , bench "alterF no rules alter insert" $ whnf (atAltNoRules (const (Just 1)) evens) m_odd
58 , bench "alterF no rules alter update" $ whnf (atAltNoRules id evens) m_even
59 , bench "alterF no rules alter delete" $ whnf (atAltNoRules (const Nothing) evens) m
60 , bench "insertWith absent" $ whnf (insWith elems_even) m_odd
61 , bench "insertWith present" $ whnf (insWith elems_even) m_even
62 , bench "insertWith' absent" $ whnf (insWith' elems_even) m_odd
63 , bench "insertWith' present" $ whnf (insWith' elems_even) m_even
64 , bench "insertWithKey absent" $ whnf (insWithKey elems_even) m_odd
65 , bench "insertWithKey present" $ whnf (insWithKey elems_even) m_even
66 , bench "insertWithKey' absent" $ whnf (insWithKey' elems_even) m_odd
67 , bench "insertWithKey' present" $ whnf (insWithKey' elems_even) m_even
68 , bench "insertLookupWithKey absent" $ whnf (insLookupWithKey elems_even) m_odd
69 , bench "insertLookupWithKey present" $ whnf (insLookupWithKey elems_even) m_even
70 , bench "insertLookupWithKey' absent" $ whnf (insLookupWithKey' elems_even) m_odd
71 , bench "insertLookupWithKey' present" $ whnf (insLookupWithKey' elems_even) m_even
72 , bench "mapWithKey" $ whnf (M.mapWithKey (+)) m
73 , bench "foldlWithKey" $ whnf (ins elems) m
74 -- , bench "foldlWithKey'" $ whnf (M.foldlWithKey' sum 0) m
75 , bench "foldrWithKey" $ whnf (M.foldrWithKey consPair []) m
76 , bench "update absent" $ whnf (upd Just evens) m_odd
77 , bench "update present" $ whnf (upd Just evens) m_even
78 , bench "update delete" $ whnf (upd (const Nothing) evens) m
79 , bench "updateLookupWithKey absent" $ whnf (upd' Just evens) m_odd
80 , bench "updateLookupWithKey present" $ whnf (upd' Just evens) m_even
81 , bench "updateLookupWithKey delete" $ whnf (upd' (const Nothing) evens) m
82 , bench "mapMaybe" $ whnf (M.mapMaybe maybeDel) m
83 , bench "mapMaybeWithKey" $ whnf (M.mapMaybeWithKey (const maybeDel)) m
84 , bench "lookupIndex" $ whnf (lookupIndex keys) m
85 , bench "union" $ whnf (M.union m_even) m_odd
86 , bench "difference" $ whnf (M.difference m) m_even
87 , bench "intersection" $ whnf (M.intersection m) m_even
88 , bench "split" $ whnf (M.split (bound `div` 2)) m
89 , bench "fromList" $ whnf M.fromList elems
90 , bench "fromList-desc" $ whnf M.fromList (reverse elems)
91 , bench "fromAscList" $ whnf M.fromAscList elems
92 , bench "fromDistinctAscList" $ whnf M.fromDistinctAscList elems
93 ]
94 where
95 bound = 2^12
96 elems = zip keys values
97 elems_even = zip evens evens
98 elems_odd = zip odds odds
99 keys = [1..bound]
100 evens = [2,4..bound]
101 odds = [1,3..bound]
102 values = [1..bound]
103 sum k v1 v2 = k + v1 + v2
104 consPair k v xs = (k, v) : xs
105
106 add3 :: Int -> Int -> Int -> Int
107 add3 x y z = x + y + z
108 {-# INLINE add3 #-}
109
110 lookup :: [Int] -> M.Map Int Int -> Int
111 lookup xs m = foldl' (\n k -> fromMaybe n (M.lookup k m)) 0 xs
112
113 atLookup :: [Int] -> M.Map Int Int -> Int
114 atLookup xs m = foldl' (\n k -> fromMaybe n (getConst (alterF Const k m))) 0 xs
115
116 newtype Consty a b = Consty { getConsty :: a }
117 instance Functor (Consty a) where
118 fmap _ (Consty a) = Consty a
119
120 atLookupNoRules :: [Int] -> M.Map Int Int -> Int
121 atLookupNoRules xs m = foldl' (\n k -> fromMaybe n (getConsty (alterF Consty k m))) 0 xs
122
123 lookupIndex :: [Int] -> M.Map Int Int -> Int
124 lookupIndex xs m = foldl' (\n k -> fromMaybe n (M.lookupIndex k m)) 0 xs
125
126 ins :: [(Int, Int)] -> M.Map Int Int -> M.Map Int Int
127 ins xs m = foldl' (\m (k, v) -> M.insert k v m) m xs
128
129 atIns :: [(Int, Int)] -> M.Map Int Int -> M.Map Int Int
130 atIns xs m = foldl' (\m (k, v) -> runIdentity (alterF (\_ -> Identity (Just v)) k m)) m xs
131
132 newtype Ident a = Ident { runIdent :: a }
133 instance Functor Ident where
134 #if __GLASGOW_HASKELL__ >= 708
135 fmap = coerce
136 #else
137 fmap f (Ident a) = Ident (f a)
138 #endif
139
140 atInsNoRules :: [(Int, Int)] -> M.Map Int Int -> M.Map Int Int
141 atInsNoRules xs m = foldl' (\m (k, v) -> runIdent (alterF (\_ -> Ident (Just v)) k m)) m xs
142
143 insWith :: [(Int, Int)] -> M.Map Int Int -> M.Map Int Int
144 insWith xs m = foldl' (\m (k, v) -> M.insertWith (+) k v m) m xs
145
146 insWithKey :: [(Int, Int)] -> M.Map Int Int -> M.Map Int Int
147 insWithKey xs m = foldl' (\m (k, v) -> M.insertWithKey add3 k v m) m xs
148
149 insWith' :: [(Int, Int)] -> M.Map Int Int -> M.Map Int Int
150 insWith' xs m = foldl' (\m (k, v) -> M.insertWith' (+) k v m) m xs
151
152 insWithKey' :: [(Int, Int)] -> M.Map Int Int -> M.Map Int Int
153 insWithKey' xs m = foldl' (\m (k, v) -> M.insertWithKey' add3 k v m) m xs
154
155 data PairS a b = PS !a !b
156
157 insLookupWithKey :: [(Int, Int)] -> M.Map Int Int -> (Int, M.Map Int Int)
158 insLookupWithKey xs m = let !(PS a b) = foldl' f (PS 0 m) xs in (a, b)
159 where
160 f (PS n m) (k, v) = let !(n', m') = M.insertLookupWithKey add3 k v m
161 in PS (fromMaybe 0 n' + n) m'
162
163 insLookupWithKey' :: [(Int, Int)] -> M.Map Int Int -> (Int, M.Map Int Int)
164 insLookupWithKey' xs m = let !(PS a b) = foldl' f (PS 0 m) xs in (a, b)
165 where
166 f (PS n m) (k, v) = let !(n', m') = M.insertLookupWithKey' add3 k v m
167 in PS (fromMaybe 0 n' + n) m'
168
169 del :: [Int] -> M.Map Int Int -> M.Map Int Int
170 del xs m = foldl' (\m k -> M.delete k m) m xs
171
172 atDel :: [Int] -> M.Map Int Int -> M.Map Int Int
173 atDel xs m = foldl' (\m k -> runIdentity (alterF (\_ -> Identity Nothing) k m)) m xs
174
175 atDelNoRules :: [Int] -> M.Map Int Int -> M.Map Int Int
176 atDelNoRules xs m = foldl' (\m k -> runIdent (alterF (\_ -> Ident Nothing) k m)) m xs
177
178 upd :: (Int -> Maybe Int) -> [Int] -> M.Map Int Int -> M.Map Int Int
179 upd f xs m = foldl' (\m k -> M.update f k m) m xs
180
181 upd' :: (Int -> Maybe Int) -> [Int] -> M.Map Int Int -> M.Map Int Int
182 upd' f xs m = foldl' (\m k -> snd $ M.updateLookupWithKey (\_ a -> f a) k m) m xs
183
184 alt :: (Maybe Int -> Maybe Int) -> [Int] -> M.Map Int Int -> M.Map Int Int
185 alt f xs m = foldl' (\m k -> M.alter f k m) m xs
186
187 atAlt :: (Maybe Int -> Maybe Int) -> [Int] -> M.Map Int Int -> M.Map Int Int
188 atAlt f xs m = foldl' (\m k -> runIdentity (alterF (Identity . f) k m)) m xs
189
190 atAltNoRules :: (Maybe Int -> Maybe Int) -> [Int] -> M.Map Int Int -> M.Map Int Int
191 atAltNoRules f xs m = foldl' (\m k -> runIdent (alterF (Ident . f) k m)) m xs
192
193 maybeDel :: Int -> Maybe Int
194 maybeDel n | n `mod` 3 == 0 = Nothing
195 | otherwise = Just n