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