@@ -1,79 +1,81 @@
1+{-# OPTIONS_GHC -fplugin GHC.TypeLits.KnownNat.Solver #-}
1{-# LANGUAGE DataKinds #-}
3{-# LANGUAGE DataKinds #-}
2−{-# LANGUAGE ExplicitNamespaces #-}
3{-# LANGUAGE InstanceSigs #-}
4{-# LANGUAGE InstanceSigs #-}
4−{-# LANGUAGE KindSignatures #-}
5{-# LANGUAGE PolyKinds #-}
5{-# LANGUAGE PolyKinds #-}
6{-# LANGUAGE TypeFamilies #-}
6{-# LANGUAGE TypeFamilies #-}
7{-# LANGUAGE TypeOperators #-}
7{-# LANGUAGE TypeOperators #-}
8{-# LANGUAGE UndecidableInstances #-}
8{-# LANGUAGE UndecidableInstances #-}
10−{-# OPTIONS_GHC -fplugin GHC.TypeLits.KnownNat.Solver #-}
12module DMap
10module DMap
16−import Criterion.Main (bench, env, nf, whnf)
18import Prelude hiding (lookup)
14import Prelude hiding (lookup)
20import Control.DeepSeq (NFData (..))
16import Control.DeepSeq (NFData (..))
17+import Criterion.Main (bench, env, nf, whnf)
18+import Data.Kind (Type)
21import Data.Maybe (fromJust)
19import Data.Maybe (fromJust)
22import Data.Proxy (Proxy (..))
20import Data.Proxy (Proxy (..))
23−import GHC.TypeLits
21+import GHC.TypeLits (type (+), KnownNat, Nat)
25import Type.Reflection (TypeRep, Typeable, typeRep)
22import Type.Reflection (TypeRep, Typeable, typeRep)
26import Type.Reflection.Unsafe (typeRepFingerprint)
23import Type.Reflection.Unsafe (typeRepFingerprint)
28import Data.Dependent.Map (DMap, empty, insert, keys, lookup)
25import Data.Dependent.Map (DMap, empty, insert, keys, lookup)
29import Data.Some (Some (Some))
26import Data.Some (Some (Some))
31−type TypeRepMap = DMap TypeRep
28+import Spec (BenchSpec (..))
31+type TypeRepMap = DMap TypeRep
34spec :: BenchSpec
33spec :: BenchSpec
35spec = BenchSpec
34spec = BenchSpec
36− { benchLookup = Just $ \name ->
35+ { benchLookup = Just $ \name ->
37− env mkBigMap $ \ ~(DMapNF bigMap) ->
36+ env mkBigMap $ \ ~(DMapNF bigMap) ->
38− bench name $ nf tenLookups bigMap
37+ bench name $ nf tenLookups bigMap
39− , benchInsertSmall = Just $ \name ->
38+ , benchInsertSmall = Just $ \name ->
40− bench name $ whnf (inserts empty 10) (Proxy @ 99999)
39+ bench name $ whnf (inserts empty 10) (Proxy @ 99999)
41− , benchInsertBig = Just $ \name ->
40+ , benchInsertBig = Just $ \name ->
42− env mkBigMap $ \ ~(DMapNF bigMap) ->
41+ env mkBigMap $ \ ~(DMapNF bigMap) ->
43− bench name $ whnf (inserts bigMap 1) (Proxy @ 99999)
42+ bench name $ whnf (inserts bigMap 1) (Proxy @ 99999)
44− , benchUpdateSmall = Nothing -- Not implemented
43+ , benchUpdateSmall = Nothing -- Not implemented
45− , benchUpdateBig = Nothing -- Not implemented
44+ , benchUpdateBig = Nothing -- Not implemented
48−tenLookups :: TypeRepMap (Proxy :: Nat -> *)
48+ :: TypeRepMap (Proxy :: Nat -> Type)
49− -> ( Proxy 10, Proxy 20, Proxy 30, Proxy 40
49+ -> ( Proxy 10, Proxy 20, Proxy 30, Proxy 40
50− , Proxy 50, Proxy 60, Proxy 70, Proxy 80
50+ , Proxy 50, Proxy 60, Proxy 70, Proxy 80
52tenLookups tmap = (lp, lp, lp, lp, lp, lp, lp, lp)
52tenLookups tmap = (lp, lp, lp, lp, lp, lp, lp, lp)
54 lp :: forall (a :: Nat) . Typeable a => Proxy a
54 lp :: forall (a :: Nat) . Typeable a => Proxy a
55 lp = fromJust $ lookup (typeRep @a) tmap
55 lp = fromJust $ lookup (typeRep @a) tmap
57−inserts :: forall a . (KnownNat a)
58+ :: forall a . (KnownNat a)
58− => TypeRepMap (Proxy :: Nat -> *)
59+ => TypeRepMap (Proxy :: Nat -> Type)
60− -> Proxy (a :: Nat)
61+ -> Proxy (a :: Nat)
61− -> TypeRepMap (Proxy :: Nat -> *)
62+ -> TypeRepMap (Proxy :: Nat -> Type)
62inserts !c 0 _ = c
63inserts !c 0 _ = c
63inserts !c n x = inserts
64inserts !c n x = inserts
64− (insert (typeRep @ a) x c)
65+ (insert (typeRep @ a) x c)
66− (Proxy :: Proxy (a+1))
67+ (Proxy :: Proxy (a+1))
68-- TypeRepMap of 10000 elements
69-- TypeRepMap of 10000 elements
69−mkBigMap :: IO (DMapNF (Proxy :: Nat -> *))
70+mkBigMap :: IO (DMapNF (Proxy :: Nat -> Type))
70mkBigMap = pure . DMapNF $ buildBigMap 10000 (Proxy :: Proxy 0) empty
71mkBigMap = pure . DMapNF $ buildBigMap 10000 (Proxy :: Proxy 0) empty
72−buildBigMap :: forall a . (KnownNat a)
74+ :: forall a . (KnownNat a)
74− -> Proxy (a :: Nat)
76+ -> Proxy (a :: Nat)
75− -> TypeRepMap (Proxy :: Nat -> *)
77+ -> TypeRepMap (Proxy :: Nat -> Type)
76− -> TypeRepMap (Proxy :: Nat -> *)
78+ -> TypeRepMap (Proxy :: Nat -> Type)
77buildBigMap 1 x = insert (typeRep @a) x
79buildBigMap 1 x = insert (typeRep @a) x
78buildBigMap n x = insert (typeRep @a) x
80buildBigMap n x = insert (typeRep @a) x
79 . buildBigMap (n - 1) (Proxy @(a + 1))
81 . buildBigMap (n - 1) (Proxy @(a + 1))
@@ -82,5 +84,6 @@ buildBigMap n x = insert (typeRep @a) x
82newtype DMapNF f = DMapNF (TypeRepMap f)
84newtype DMapNF f = DMapNF (TypeRepMap f)
84instance NFData (DMapNF f) where
86instance NFData (DMapNF f) where
87+ rnf :: DMapNF f -> ()
85− rnf (DMapNF x) =
88+ rnf (DMapNF x) =
86− rnf . map (\(Some t) -> typeRepFingerprint t) $ keys x
89+ rnf . map (\(Some t) -> typeRepFingerprint t) $ keys x