@@ -1,4 +1,61 @@
1−import Criterion.Main
1+{-# LANGUAGE DataKinds #-}
2+{-# LANGUAGE PolyKinds #-}
3+{-# LANGUAGE ExplicitForAll #-}
4+{-# LANGUAGE ExplicitNamespaces #-}
5+{-# LANGUAGE KindSignatures #-}
6+{-# LANGUAGE ScopedTypeVariables #-}
7+{-# LANGUAGE TypeFamilies #-}
8+{-# LANGUAGE TypeOperators #-}
9+{-# LANGUAGE UndecidableInstances #-}
10+
11+{-# OPTIONS_GHC -fplugin GHC.TypeLits.KnownNat.Solver #-}
12+
13+import Criterion.Main (bench, defaultMain, nf, whnf)
14+
15+import Prelude hiding (lookup)
16+
17+import Control.DeepSeq (rnf)
18+import Control.Exception.Base (evaluate)
19+import Data.Maybe (fromJust)
20+import Data.Proxy (Proxy (..))
21+import Data.Typeable (Typeable)
22+import GHC.TypeLits
23+
24+import Data.TypeRep.Map (TypeRepMap (..), empty, insert, keys, lookup, size)
225
326main :: IO ()
4−main = defaultMain [bench "const" (whnf const ())]
27+main = do
28+ putStrLn $ "size: " ++ (show $ size bigMap)
29+ evaluate $ rknf bigMap
30+ defaultMain
31+ [ bench "lookup" $ nf tenLookups bigMap
32+ , bench "insert new" $ whnf (\x -> rknf $ insert x bigMap) (Proxy :: Proxy 9999999999)
33+ , bench "update old" $ whnf (\x -> rknf $ insert x bigMap) (Proxy :: Proxy 1)
34+ ]
35+
36+tenLookups :: TypeRepMap (Proxy :: Nat -> *)
37+ -> ( Proxy 10, Proxy 20, Proxy 30, Proxy 40
38+ , Proxy 50, Proxy 60, Proxy 70, Proxy 80
39+ )
40+tenLookups tmap = (lp, lp, lp, lp, lp, lp, lp, lp)
41+ where
42+ lp :: forall (a::Nat). Typeable a => Proxy a
43+ lp = fromJust $ lookup tmap
44+
45+-- TypeRepMap of 10000 elements
46+bigMap :: TypeRepMap (Proxy :: Nat -> *)
47+bigMap = buildBigMap 10000 (Proxy :: Proxy 0) empty
48+
49+data Z
50+data S a
51+
52+buildBigMap :: forall a . (Typeable a, KnownNat a) => Int -> Proxy (a :: Nat) -> TypeRepMap (Proxy :: Nat -> *) -> TypeRepMap (Proxy :: Nat -> *)
53+buildBigMap 1 x = insert x
54+buildBigMap n x = insert x . buildBigMap (n - 1) (Proxy :: Proxy (a + 1))
55+
56+rknf :: TypeRepMap f -> ()
57+rknf = rnf . keys
58+
59+type family BigProxy (n :: Nat) :: * where
60+ BigProxy 0 = Z
61+ BigProxy n = S (BigProxy (n - 1))