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