on 73e7201 help sign in
77 lines 2.3 kB history raw
1{-# OPTIONS_GHC -fplugin GHC.TypeLits.KnownNat.Solver #-}
3{-# LANGUAGE DataKinds #-}
4{-# LANGUAGE PolyKinds #-}
5{-# LANGUAGE TypeFamilies #-}
6{-# LANGUAGE TypeOperators #-}
7{-# LANGUAGE UndecidableInstances #-}
9module CMap
10 ( spec
11 ) where
13import Prelude hiding (lookup)
15import Criterion.Main (bench, env, nf, whnf)
16import Data.Kind (Type)
17import Data.Maybe (fromJust)
18import Data.Proxy (Proxy (..))
19import Data.Typeable (Typeable)
20import GHC.TypeLits (type (+), KnownNat, Nat)
22import Data.TypeRep.CMap (TypeRepMap (..), empty, insert, lookup)
24import Spec (BenchSpec (..))
27spec :: BenchSpec
28spec = BenchSpec
29 { benchLookup = Just $ \name ->
30 env (mkMap 10000) $ \ ~bigMap ->
31 bench name $ nf tenLookups bigMap
32 , benchInsertSmall = Just $ \name ->
33 bench name $ whnf (inserts empty 10) (Proxy @ 99999)
34 , benchInsertBig = Just $ \name ->
35 env (mkMap 10000) $ \ ~bigMap ->
36 bench name $ whnf (inserts bigMap 1) (Proxy @ 99999)
37 , benchUpdateSmall = Just $ \name ->
38 env (mkMap 10) $ \ ~smallMap ->
39 bench name $ whnf (inserts smallMap 10) (Proxy @ 0)
40 , benchUpdateBig = Just $ \name ->
41 env (mkMap 10000) $ \ ~bigMap ->
42 bench name $ whnf (inserts bigMap 10) (Proxy @ 0)
43 }
45tenLookups
46 :: TypeRepMap (Proxy :: Nat -> Type)
47 -> ( Proxy 10, Proxy 20, Proxy 30, Proxy 40
48 , Proxy 50, Proxy 60, Proxy 70, Proxy 80
49 )
50tenLookups tmap = (lp, lp, lp, lp, lp, lp, lp, lp)
51 where
52 lp :: forall (a::Nat). Typeable a => Proxy a
53 lp = fromJust $ lookup tmap
55inserts
56 :: forall a . (KnownNat a)
57 => TypeRepMap (Proxy :: Nat -> Type)
58 -> Int
59 -> Proxy (a :: Nat)
60 -> TypeRepMap (Proxy :: Nat -> Type)
61inserts !c 0 _ = c
62inserts !c n x = inserts
63 (insert x c)
64 (n-1)
65 (Proxy :: Proxy (a+1))
67mkMap :: Int -> IO (TypeRepMap (Proxy :: Nat -> Type))
68mkMap n = pure $ buildBigMap n (Proxy :: Proxy 0) empty
70buildBigMap
71 :: forall a . (KnownNat a)
72 => Int
73 -> Proxy (a :: Nat)
74 -> TypeRepMap (Proxy :: Nat -> Type)
75 -> TypeRepMap (Proxy :: Nat -> Type)
76buildBigMap 1 x = insert x
77buildBigMap n x = insert x . buildBigMap (n - 1) (Proxy :: Proxy (a + 1))