on main help sign in
Add benchmarks with DMap (#14)

* Add benchmarks with DMap * Fix CI for 8.0.2

commite64c000e57b20f4994322c0ea34fd903b11f62f4 authorVeronika Romashkina <vrom911@gmail.com> dateSat 7 Jul 2018, 22:20 (8 years ago) parent71aa479
4 files changed+101−4
benchmark/CacheMap.hs+1−2 benchmark/DMap.hs+85 benchmark/Main.hs+11 typerep-map.cabal+4−2
benchmark/CacheMap.hs+1−2
@@ -2,7 +2,6 @@
22{-# LANGUAGE ExplicitNamespaces #-}
33{-# LANGUAGE KindSignatures #-}
44{-# LANGUAGE PolyKinds #-}
5−{-# LANGUAGE ScopedTypeVariables #-}
65{-# LANGUAGE TypeFamilies #-}
76{-# LANGUAGE TypeOperators #-}
87{-# LANGUAGE UndecidableInstances #-}
@@ -25,7 +24,7 @@ import Data.Proxy (Proxy (..))
2524import Data.Typeable (Typeable)
2625import GHC.TypeLits
2726
28−import Data.TypeRep.Map (TF (..), TypeRepMap (..), fromList, lookup)
27+import Data.TypeRep.CacheMap (TF (..), TypeRepMap (..), fromList, lookup)
2928
3029benchCacheMap :: Benchmark
3130benchCacheMap = bgroup "vector optimal cache"
benchmark/DMap.hsnew file+85
@@ -0,0 +1,85 @@
1+{-# LANGUAGE DataKinds #-}
2+{-# LANGUAGE ExplicitNamespaces #-}
3+{-# LANGUAGE InstanceSigs #-}
4+{-# LANGUAGE KindSignatures #-}
5+{-# LANGUAGE PolyKinds #-}
6+{-# LANGUAGE TypeFamilies #-}
7+{-# LANGUAGE TypeOperators #-}
8+{-# LANGUAGE UndecidableInstances #-}
9+{-# LANGUAGE ViewPatterns #-}
10+
11+{-# OPTIONS_GHC -fplugin GHC.TypeLits.KnownNat.Solver -fno-warn-orphans #-}
12+
13+module DMap
14+ ( benchDMap
15+ , prepareBenchDMap
16+ ) where
17+
18+import Criterion.Main (Benchmark, bench, bgroup, nf)
19+
20+import Prelude hiding (lookup)
21+
22+import Control.DeepSeq (rnf)
23+import Control.Exception
24+import Data.Functor.Identity (Identity (..))
25+import Data.Maybe (fromJust)
26+import Data.Proxy (Proxy (..))
27+import Data.Type.Equality ((:~:) (..))
28+import GHC.TypeLits
29+import Type.Reflection (TypeRep, Typeable, typeRep)
30+import Type.Reflection.Unsafe (typeRepFingerprint)
31+import Unsafe.Coerce (unsafeCoerce)
32+
33+import Data.Dependent.Map (DMap, empty, insert, keys, lookup)
34+import Data.GADT.Compare (GCompare (..), GEq (..), GOrdering (..))
35+import Data.Some (Some (This))
36+
37+benchDMap :: Benchmark
38+benchDMap = bgroup "dependent map"
39+ [ bench "lookup" $ nf tenLookups bigMap
40+ -- , bench "insert new" $ whnf (\x -> rknf $ insert x bigMap) (Proxy :: Proxy 9999999999)
41+ -- , bench "update old" $ whnf (\x -> rknf $ insert x bigMap) (Proxy :: Proxy 1)
42+ ]
43+
44+tenLookups :: DMap TypeRep Identity
45+ -> ( Proxy 10, Proxy 20, Proxy 30, Proxy 40
46+ , Proxy 50, Proxy 60, Proxy 70, Proxy 80
47+ )
48+tenLookups tmap = (lp, lp, lp, lp, lp, lp, lp, lp)
49+ where
50+ lp :: forall (a :: Nat) . Typeable a => Proxy a
51+ lp = runIdentity $ fromJust $ lookup (typeRep @(Proxy a)) tmap
52+
53+-- TypeRepMap of 10000 elements
54+bigMap :: DMap TypeRep Identity
55+bigMap = buildBigMap 10000 (Proxy :: Proxy 0) empty
56+
57+buildBigMap :: forall a . (KnownNat a)
58+ => Int
59+ -> Proxy (a :: Nat)
60+ -> DMap TypeRep Identity
61+ -> DMap TypeRep Identity
62+buildBigMap 1 x = insert (typeRep @(Proxy a)) $ Identity x
63+buildBigMap n x = insert (typeRep @(Proxy a)) (Identity x)
64+ . buildBigMap (n - 1) (Proxy @(a + 1))
65+
66+rknf :: DMap TypeRep f -> ()
67+rknf = rnf . map (\(This t) -> typeRepFingerprint t) . keys
68+
69+prepareBenchDMap :: IO ()
70+prepareBenchDMap = evaluate (rknf bigMap)
71+
72+instance GEq TypeRep where
73+ geq :: TypeRep a -> TypeRep b -> Maybe (a :~: b)
74+ geq (typeRepFingerprint -> a) (typeRepFingerprint -> b) =
75+ if a == b
76+ then Just $ unsafeCoerce Refl
77+ else Nothing
78+
79+instance GCompare TypeRep where
80+ gcompare :: TypeRep a -> TypeRep b -> GOrdering a b
81+ gcompare (typeRepFingerprint -> a) (typeRepFingerprint -> b) =
82+ case compare a b of
83+ EQ -> unsafeCoerce GEQ
84+ LT -> GLT
85+ GT -> GGT
benchmark/Main.hs+11
@@ -1,9 +1,14 @@
1+{-# LANGUAGE CPP #-}
2+
13module Main where
24
35import Criterion.Main (defaultMain)
46
57import CacheMap (benchCacheMap, prepareBenchCacheMap)
68import CMap (benchMap, prepareBenchMap)
9+#if ( __GLASGOW_HASKELL__ >= 802 )
10+import DMap (benchDMap, prepareBenchDMap)
11+#endif
712import OptimalVector (benchVectorOpt, prepareBenchVectorOpt)
813--import Vector (benchVector, prepareBenchVector)
914
@@ -13,9 +18,15 @@ main = do
1318 prepareBenchCacheMap
1419 --prepareBenchVector
1520 prepareBenchVectorOpt
21+#if ( __GLASGOW_HASKELL__ >= 802 )
22+ prepareBenchDMap
23+#endif
1624 defaultMain
1725 [ benchMap
1826 -- , benchVector
1927 , benchCacheMap
2028 , benchVectorOpt
29+#if ( __GLASGOW_HASKELL__ >= 802 )
30+ , benchDMap
31+#endif
2132 ]
typerep-map.cabal+4−2
@@ -76,15 +76,17 @@ benchmark typerep-map-benchmark
7676 , OptimalVector
7777 build-depends: base
7878 , criterion
79− , typerep-map
8079 , typerep-map-internal
8180 , deepseq
81+ , dependent-map
82+ , dependent-sum
8283 , ghc-typelits-knownnat
8384 default-extensions: OverloadedStrings
8485 RecordWildCards
8586 ScopedTypeVariables
8687 TypeApplications
87−
88+ if impl(ghc >= 8.2.2)
89+ other-modules: DMap
8890
8991source-repository head
9092 type: git