on fe28607 help sign in
134 lines 4.1 kB history raw
1{-# OPTIONS_GHC -Wno-deprecations #-}
3{-# LANGUAGE CPP #-}
5#include "Streamly/Benchmark/Data/Array/CommonImports.hs"
7import Control.DeepSeq (deepseq)
9import qualified Streamly.Internal.Data.Array as IA
10import qualified GHC.Exts as GHC
12-- import qualified Streamly.Data.Array as A
13import qualified Streamly.Internal.Data.Array as A
14type Stream = A.Array
16#include "Streamly/Benchmark/Data/Array/Common.hs"
18-------------------------------------------------------------------------------
19-- Benchmark helpers
20-------------------------------------------------------------------------------
22-- Drain a source that generates a pure array
23{-# INLINE benchPureSrc #-}
24benchPureSrc :: String -> (Int -> Stream a) -> Benchmark
25benchPureSrc name src = benchPure name src id
27-- Drain a source that generates an array in the IO monad
28{-# INLINE benchIOSrc #-}
29benchIOSrc :: String -> (Int -> IO (Stream a)) -> Benchmark
30benchIOSrc name src = benchIO name src id
32-------------------------------------------------------------------------------
33-- Bench Ops
34-------------------------------------------------------------------------------
36{-# INLINE sourceIntFromToFromList #-}
37sourceIntFromToFromList :: MonadIO m => Int -> Int -> m (Stream Int)
38sourceIntFromToFromList value n = P.return $ A.fromListN value [n..n + value]
40{-# INLINE readInstance #-}
41readInstance :: P.String -> Stream Int
42readInstance str =
43 let r = P.reads str
44 in case r of
45 [(x,"")] -> x
46 _ -> P.error "readInstance: no parse"
48#ifdef DEVBUILD
49{-
50{-# INLINE foldableFoldl' #-}
51foldableFoldl' :: Stream Int -> Int
52foldableFoldl' = F.foldl' (+) 0
54{-# INLINE foldableSum #-}
55foldableSum :: Stream Int -> Int
56foldableSum = P.sum
57-}
58#endif
60{-# INLINE sourceIsList #-}
61sourceIsList :: Int -> Int -> Stream Int
62sourceIsList value n = GHC.fromList [n..n+value]
64{-# INLINE sourceIsString #-}
65sourceIsString :: Int -> Int -> Stream P.Char
66sourceIsString value n = GHC.fromString (P.replicate (n + value) 'a')
68{-# INLINE sourceIntFromToFromStream #-}
69sourceIntFromToFromStream :: MonadIO m => Int -> Int -> m (Stream Int)
70sourceIntFromToFromStream value n = S.fold A.write $ S.enumerateFromTo n (n + value)
72-------------------------------------------------------------------------------
73-- Bench groups
74-------------------------------------------------------------------------------
76o_1_space_generation :: Int -> [Benchmark]
77o_1_space_generation value =
78 [ bgroup
79 "generation"
80 [ benchIOSrc "write . intFromTo" (sourceIntFromToFromStream value)
81 , let testStr = "fromList " ++ mkListString value
82 in testStr `deepseq` bench "read" (nf readInstance testStr)
83 , benchPureSrc "writeN . IsList.fromList" (sourceIsList value)
84 , benchPureSrc
85 "writeN . IsString.fromString"
86 (sourceIsString value)
88 ]
89 ]
91o_1_space_elimination :: Int -> [Benchmark]
92o_1_space_elimination value =
93 [ bgroup "elimination"
94 [ benchPureSink value "length . IsList.toList" (length . GHC.toList)
95 , benchFold "writeLastN.1"
96 (S.fold (IA.writeLastN 1)) (P.sourceUnfoldrM value)
97 , benchFold "writeLastN.10"
98 (S.fold (IA.writeLastN 10)) (P.sourceUnfoldrM value)
99#ifdef DEVBUILD
100{-
101 benchPureSink value "foldable/foldl'" foldableFoldl'
102 , benchPureSink value "foldable/sum" foldableSum
103-}
104#endif
105 ]
106 ]
108o_n_heap_serial :: Int -> [Benchmark]
109o_n_heap_serial value =
110 [ bgroup "elimination"
111 [
112 -- Converting the stream to an array
113 benchFold "writeLastN.Max" (S.fold (IA.writeLastN (value + 1)))
114 (P.sourceUnfoldrM value)
115 ]
116 ]
118moduleName :: String
119moduleName = "Data.Array"
121defStreamSize :: Int
122defStreamSize = defaultStreamSize
124main :: IO ()
125main = runWithCLIOpts defStreamSize allBenchmarks
127 where
129 allBenchmarks size =
130 [ bgroup (o_1_space_prefix moduleName) $
131 o_1_space_generation size ++ o_1_space_elimination size
132 , bgroup (o_n_space_prefix moduleName) $
133 o_n_heap_serial size
134 ] ++ commonBenchmarks size