on main help sign in
Fix the DMap benchmark (#41)
e7ac7acVladislav Zavialov
benchmark/DMap.hs+10−9
@@ -21,7 +21,6 @@ import Prelude hiding (lookup)
21
21
22import Control.DeepSeq (rnf)
22import Control.DeepSeq (rnf)
23import Control.Exception
23import Control.Exception
24−import Data.Functor.Identity (Identity (..))
25import Data.Maybe (fromJust)
24import Data.Maybe (fromJust)
26import Data.Proxy (Proxy (..))
25import Data.Proxy (Proxy (..))
27import Data.Type.Equality ((:~:) (..))
26import Data.Type.Equality ((:~:) (..))
@@ -34,6 +33,8 @@ import Data.Dependent.Map (DMap, empty, insert, keys, lookup)
34import Data.GADT.Compare (GCompare (..), GEq (..), GOrdering (..))
33import Data.GADT.Compare (GCompare (..), GEq (..), GOrdering (..))
35import Data.Some (Some (This))
34import Data.Some (Some (This))
36
35
36+type TypeRepMap = DMap TypeRep
37+
37benchDMap :: Benchmark
38benchDMap :: Benchmark
38benchDMap = bgroup "dependent map"
39benchDMap = bgroup "dependent map"
39 [ bench "lookup" $ nf tenLookups bigMap
40 [ bench "lookup" $ nf tenLookups bigMap
@@ -41,29 +42,29 @@ benchDMap = bgroup "dependent map"
41 -- , bench "update old" $ whnf (\x -> rknf $ insert x bigMap) (Proxy :: Proxy 1)
42 -- , bench "update old" $ whnf (\x -> rknf $ insert x bigMap) (Proxy :: Proxy 1)
42 ]
43 ]
43
44
44−tenLookups :: DMap TypeRep Identity
45+tenLookups :: TypeRepMap (Proxy :: Nat -> *)
45 -> ( Proxy 10, Proxy 20, Proxy 30, Proxy 40
46 -> ( Proxy 10, Proxy 20, Proxy 30, Proxy 40
46 , Proxy 50, Proxy 60, Proxy 70, Proxy 80
47 , Proxy 50, Proxy 60, Proxy 70, Proxy 80
47 )
48 )
48tenLookups tmap = (lp, lp, lp, lp, lp, lp, lp, lp)
49tenLookups tmap = (lp, lp, lp, lp, lp, lp, lp, lp)
49 where
50 where
50 lp :: forall (a :: Nat) . Typeable a => Proxy a
51 lp :: forall (a :: Nat) . Typeable a => Proxy a
51− lp = runIdentity $ fromJust $ lookup (typeRep @(Proxy a)) tmap
52+ lp = fromJust $ lookup (typeRep @a) tmap
52
53
53-- TypeRepMap of 10000 elements
54-- TypeRepMap of 10000 elements
54−bigMap :: DMap TypeRep Identity
55+bigMap :: TypeRepMap (Proxy :: Nat -> *)
55bigMap = buildBigMap 10000 (Proxy :: Proxy 0) empty
56bigMap = buildBigMap 10000 (Proxy :: Proxy 0) empty
56
57
57buildBigMap :: forall a . (KnownNat a)
58buildBigMap :: forall a . (KnownNat a)
58 => Int
59 => Int
59 -> Proxy (a :: Nat)
60 -> Proxy (a :: Nat)
60− -> DMap TypeRep Identity
61+ -> TypeRepMap (Proxy :: Nat -> *)
61− -> DMap TypeRep Identity
62+ -> TypeRepMap (Proxy :: Nat -> *)
62−buildBigMap 1 x = insert (typeRep @(Proxy a)) $ Identity x
63+buildBigMap 1 x = insert (typeRep @a) x
63−buildBigMap n x = insert (typeRep @(Proxy a)) (Identity x)
64+buildBigMap n x = insert (typeRep @a) x
64 . buildBigMap (n - 1) (Proxy @(a + 1))
65 . buildBigMap (n - 1) (Proxy @(a + 1))
65
66
66−rknf :: DMap TypeRep f -> ()
67+rknf :: TypeRepMap f -> ()
67rknf = rnf . map (\(This t) -> typeRepFingerprint t) . keys
68rknf = rnf . map (\(This t) -> typeRepFingerprint t) . keys
68
69
69prepareBenchDMap :: IO ()
70prepareBenchDMap :: IO ()