on 4b66779 help sign in
typerep-map/benchmark/OptimalVector.hs
59 lines 1.8 kB history raw
1{-# LANGUAGE DataKinds #-}
2{-# LANGUAGE ExplicitNamespaces #-}
3{-# LANGUAGE KindSignatures #-}
4{-# LANGUAGE PolyKinds #-}
5{-# LANGUAGE ScopedTypeVariables #-}
6{-# LANGUAGE TypeFamilies #-}
7{-# LANGUAGE TypeOperators #-}
8{-# LANGUAGE UndecidableInstances #-}
10{-# OPTIONS_GHC -fplugin GHC.TypeLits.KnownNat.Solver #-}
12module OptimalVector
13 ( benchVectorOpt
14 ) where
16import Criterion.Main (Benchmark, bench, bgroup, nf)
18import Prelude hiding (lookup)
20import Data.Maybe (fromJust)
21import Data.Proxy (Proxy (..))
22import Data.Typeable (Typeable)
23import GHC.TypeLits
25import Data.TypeRep.OptimalVector
27benchVectorOpt :: Benchmark
28benchVectorOpt = bgroup "vector optimal"
29 [ bench "lookup" $ nf tenLookups bigMap
30 -- , bench "insert new" $ whnf (\x -> rknf $ insert x bigMap) (Proxy :: Proxy 9999999999)
31 -- , bench "update old" $ whnf (\x -> rknf $ insert x bigMap) (Proxy :: Proxy 1)
32 ]
34tenLookups :: TypeRepVector (Proxy :: Nat -> *)
35 -> ( Proxy 10, Proxy 20, Proxy 30, Proxy 40
36 , Proxy 50, Proxy 60, Proxy 70, Proxy 80
37 )
38tenLookups tmap = (lp, lp, lp, lp, lp, lp, lp, lp)
39 where
40 lp :: forall (a::Nat). Typeable a => Proxy a
41 lp = fromJust $ lookup tmap
43-- TypeRepMap of 10000 elements
44bigMap :: TypeRepVector (Proxy :: Nat -> *)
45bigMap = fromList $ buildBigMap 10000 (Proxy :: Proxy 0) []
47data Z
48data S a
50buildBigMap :: forall a . (Typeable a, KnownNat a) => Int -> Proxy (a :: Nat) -> [TF (Proxy :: Nat -> *)] -> [TF (Proxy :: Nat -> *)]
51buildBigMap 1 x = (TF x :)
52buildBigMap n x = (TF x :) . buildBigMap (n - 1) (Proxy :: Proxy (a + 1))
54-- rknf :: TypeRepMap f -> ()
55-- rknf = rnf . keys
57type family BigProxy (n :: Nat) :: * where
58 BigProxy 0 = Z
59 BigProxy n = S (BigProxy (n - 1))