@@ -1,79 +1,81 @@
1+{-# OPTIONS_GHC -fplugin GHC.TypeLits.KnownNat.Solver #-}
2+
13{-# LANGUAGE DataKinds #-}
2−{-# LANGUAGE ExplicitNamespaces #-}
34{-# LANGUAGE InstanceSigs #-}
4−{-# LANGUAGE KindSignatures #-}
55{-# LANGUAGE PolyKinds #-}
66{-# LANGUAGE TypeFamilies #-}
77{-# LANGUAGE TypeOperators #-}
88{-# LANGUAGE UndecidableInstances #-}
99
10−{-# OPTIONS_GHC -fplugin GHC.TypeLits.KnownNat.Solver #-}
11−
1210module DMap
13− ( spec
14− ) where
15−
16−import Criterion.Main (bench, env, nf, whnf)
11+ ( spec
12+ ) where
1713
1814import Prelude hiding (lookup)
1915
2016import Control.DeepSeq (NFData (..))
17+import Criterion.Main (bench, env, nf, whnf)
18+import Data.Kind (Type)
2119import Data.Maybe (fromJust)
2220import Data.Proxy (Proxy (..))
23−import GHC.TypeLits
24−import Spec
21+import GHC.TypeLits (type (+), KnownNat, Nat)
2522import Type.Reflection (TypeRep, Typeable, typeRep)
2623import Type.Reflection.Unsafe (typeRepFingerprint)
2724
2825import Data.Dependent.Map (DMap, empty, insert, keys, lookup)
2926import Data.Some (Some (Some))
3027
31−type TypeRepMap = DMap TypeRep
28+import Spec (BenchSpec (..))
3229
3330
31+type TypeRepMap = DMap TypeRep
32+
3433spec :: BenchSpec
3534spec = BenchSpec
36− { benchLookup = Just $ \name ->
37− env mkBigMap $ \ ~(DMapNF bigMap) ->
38− bench name $ nf tenLookups bigMap
39− , benchInsertSmall = Just $ \name ->
40− bench name $ whnf (inserts empty 10) (Proxy @ 99999)
41− , benchInsertBig = Just $ \name ->
42− env mkBigMap $ \ ~(DMapNF bigMap) ->
43− bench name $ whnf (inserts bigMap 1) (Proxy @ 99999)
44− , benchUpdateSmall = Nothing -- Not implemented
45− , benchUpdateBig = Nothing -- Not implemented
46− }
35+ { benchLookup = Just $ \name ->
36+ env mkBigMap $ \ ~(DMapNF bigMap) ->
37+ bench name $ nf tenLookups bigMap
38+ , benchInsertSmall = Just $ \name ->
39+ bench name $ whnf (inserts empty 10) (Proxy @ 99999)
40+ , benchInsertBig = Just $ \name ->
41+ env mkBigMap $ \ ~(DMapNF bigMap) ->
42+ bench name $ whnf (inserts bigMap 1) (Proxy @ 99999)
43+ , benchUpdateSmall = Nothing -- Not implemented
44+ , benchUpdateBig = Nothing -- Not implemented
45+ }
4746
48−tenLookups :: TypeRepMap (Proxy :: Nat -> *)
49− -> ( Proxy 10, Proxy 20, Proxy 30, Proxy 40
50− , Proxy 50, Proxy 60, Proxy 70, Proxy 80
51− )
47+tenLookups
48+ :: TypeRepMap (Proxy :: Nat -> Type)
49+ -> ( Proxy 10, Proxy 20, Proxy 30, Proxy 40
50+ , Proxy 50, Proxy 60, Proxy 70, Proxy 80
51+ )
5252tenLookups tmap = (lp, lp, lp, lp, lp, lp, lp, lp)
5353 where
5454 lp :: forall (a :: Nat) . Typeable a => Proxy a
5555 lp = fromJust $ lookup (typeRep @a) tmap
5656
57−inserts :: forall a . (KnownNat a)
58− => TypeRepMap (Proxy :: Nat -> *)
59− -> Int
60− -> Proxy (a :: Nat)
61− -> TypeRepMap (Proxy :: Nat -> *)
57+inserts
58+ :: forall a . (KnownNat a)
59+ => TypeRepMap (Proxy :: Nat -> Type)
60+ -> Int
61+ -> Proxy (a :: Nat)
62+ -> TypeRepMap (Proxy :: Nat -> Type)
6263inserts !c 0 _ = c
6364inserts !c n x = inserts
64− (insert (typeRep @ a) x c)
65− (n-1)
66− (Proxy :: Proxy (a+1))
65+ (insert (typeRep @ a) x c)
66+ (n-1)
67+ (Proxy :: Proxy (a+1))
6768
6869-- TypeRepMap of 10000 elements
69−mkBigMap :: IO (DMapNF (Proxy :: Nat -> *))
70+mkBigMap :: IO (DMapNF (Proxy :: Nat -> Type))
7071mkBigMap = pure . DMapNF $ buildBigMap 10000 (Proxy :: Proxy 0) empty
7172
72−buildBigMap :: forall a . (KnownNat a)
73− => Int
74− -> Proxy (a :: Nat)
75− -> TypeRepMap (Proxy :: Nat -> *)
76− -> TypeRepMap (Proxy :: Nat -> *)
73+buildBigMap
74+ :: forall a . (KnownNat a)
75+ => Int
76+ -> Proxy (a :: Nat)
77+ -> TypeRepMap (Proxy :: Nat -> Type)
78+ -> TypeRepMap (Proxy :: Nat -> Type)
7779buildBigMap 1 x = insert (typeRep @a) x
7880buildBigMap n x = insert (typeRep @a) x
7981 . buildBigMap (n - 1) (Proxy @(a + 1))
@@ -82,5 +84,6 @@ buildBigMap n x = insert (typeRep @a) x
8284newtype DMapNF f = DMapNF (TypeRepMap f)
8385
8486instance NFData (DMapNF f) where
85− rnf (DMapNF x) =
86− rnf . map (\(Some t) -> typeRepFingerprint t) $ keys x
87+ rnf :: DMapNF f -> ()
88+ rnf (DMapNF x) =
89+ rnf . map (\(Some t) -> typeRepFingerprint t) $ keys x