@@ -11,16 +11,15 @@
11{-# OPTIONS_GHC -fplugin GHC.TypeLits.KnownNat.Solver -fno-warn-orphans #-}
11{-# OPTIONS_GHC -fplugin GHC.TypeLits.KnownNat.Solver -fno-warn-orphans #-}
13module DMap
13module DMap
14− ( benchDMap
14+ ( spec
18−import Criterion.Main (Benchmark, bench, bgroup, nf)
17+import Criterion.Main (bench, nf, whnf, env)
20import Prelude hiding (lookup)
19import Prelude hiding (lookup)
22−import Control.DeepSeq (rnf)
21+import Spec
23−import Control.Exception
22+import Control.DeepSeq (NFData(..))
24import Data.Maybe (fromJust)
23import Data.Maybe (fromJust)
25import Data.Proxy (Proxy (..))
24import Data.Proxy (Proxy (..))
26import Data.Type.Equality ((:~:) (..))
25import Data.Type.Equality ((:~:) (..))
@@ -35,12 +34,20 @@ import Data.Some (Some (This))
36type TypeRepMap = DMap TypeRep
35type TypeRepMap = DMap TypeRep
38−benchDMap :: Benchmark
37+
39−benchDMap = bgroup "dependent map"
38+spec :: BenchSpec
40+ { benchLookup = Just $ \name ->
41+ env mkBigMap $ \ ~(DMapNF bigMap) ->
40− [ bench "lookup" $ nf tenLookups bigMap
42+ bench name $ nf tenLookups bigMap
41− -- , bench "insert new" $ whnf (\x -> rknf $ insert x bigMap) (Proxy :: Proxy 9999999999)
43+ , benchInsertSmall = Just $ \name ->
42− -- , bench "update old" $ whnf (\x -> rknf $ insert x bigMap) (Proxy :: Proxy 1)
44+ bench name $ whnf (inserts empty 10) (Proxy @ 99999)
43− ]
45+ , benchInsertBig = Just $ \name ->
46+ env mkBigMap $ \ ~(DMapNF bigMap) ->
47+ bench name $ whnf (inserts bigMap 1) (Proxy @ 99999)
48+ , benchUpdateSmall = Nothing -- Not implemented
49+ , benchUpdateBig = Nothing -- Not implemented
45tenLookups :: TypeRepMap (Proxy :: Nat -> *)
52tenLookups :: TypeRepMap (Proxy :: Nat -> *)
46 -> ( Proxy 10, Proxy 20, Proxy 30, Proxy 40
53 -> ( Proxy 10, Proxy 20, Proxy 30, Proxy 40
@@ -51,9 +58,20 @@ tenLookups tmap = (lp, lp, lp, lp, lp, lp, lp, lp)
51 lp :: forall (a :: Nat) . Typeable a => Proxy a
58 lp :: forall (a :: Nat) . Typeable a => Proxy a
52 lp = fromJust $ lookup (typeRep @a) tmap
59 lp = fromJust $ lookup (typeRep @a) tmap
61+inserts :: forall a . (KnownNat a)
62+ => TypeRepMap (Proxy :: Nat -> *)
65+ -> TypeRepMap (Proxy :: Nat -> *)
67+inserts !c n x = inserts
68+ (insert (typeRep @ a) x c)
70+ (Proxy :: Proxy (a+1))
54-- TypeRepMap of 10000 elements
72-- TypeRepMap of 10000 elements
55−bigMap :: TypeRepMap (Proxy :: Nat -> *)
73+mkBigMap :: IO (DMapNF (Proxy :: Nat -> *))
56−bigMap = buildBigMap 10000 (Proxy :: Proxy 0) empty
74+mkBigMap = pure . DMapNF $ buildBigMap 10000 (Proxy :: Proxy 0) empty
58buildBigMap :: forall a . (KnownNat a)
76buildBigMap :: forall a . (KnownNat a)
@@ -64,11 +82,12 @@ buildBigMap 1 x = insert (typeRep @a) x
64buildBigMap n x = insert (typeRep @a) x
82buildBigMap n x = insert (typeRep @a) x
65 . buildBigMap (n - 1) (Proxy @(a + 1))
83 . buildBigMap (n - 1) (Proxy @(a + 1))
67−rknf :: TypeRepMap f -> ()
85+-- | Wrapper that provides NFData instance to the 'DMap' structure.
68−rknf = rnf . map (\(This t) -> typeRepFingerprint t) . keys
86+newtype DMapNF f = DMapNF (TypeRepMap f)
70−prepareBenchDMap :: IO ()
88+instance NFData (DMapNF f) where
71−prepareBenchDMap = evaluate (rknf bigMap)
89+ rnf (DMapNF x) =
90+ rnf . map (\(This t) -> typeRepFingerprint t) $ keys x
73instance GEq TypeRep where
92instance GEq TypeRep where
74 geq :: TypeRep a -> TypeRep b -> Maybe (a :~: b)
93 geq :: TypeRep a -> TypeRep b -> Maybe (a :~: b)