on dc58c59 help sign in
119 lines 3.5 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.Generic as IA
11import qualified Streamly.Internal.Data.Array.Generic as A
13type Stream = A.Array
15#include "Streamly/Benchmark/Data/Array/Common.hs"
17instance NFData a => NFData (A.Array a) where
18 {-# INLINE rnf #-}
19 rnf = A.foldl' (\_ x -> rnf x) ()
21-------------------------------------------------------------------------------
22-- Benchmark helpers
23-------------------------------------------------------------------------------
25-- Drain a source that generates an array in the IO monad
26{-# INLINE benchIOSrc #-}
27benchIOSrc :: NFData a => 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 sourceIntFromToFromStream #-}
59sourceIntFromToFromStream :: MonadIO m => Int -> Int -> m (Stream Int)
60sourceIntFromToFromStream value n =
61 S.fold A.write $ S.enumerateFromTo n (n + value)
63-------------------------------------------------------------------------------
64-- Bench groups
65-------------------------------------------------------------------------------
67o_1_space_generation :: Int -> [Benchmark]
68o_1_space_generation value =
69 [ bgroup
70 "generation"
71 [ benchIOSrc "write . intFromTo" (sourceIntFromToFromStream value)
72 , let testStr = "fromList " ++ mkListString value
73 in testStr `deepseq` bench "read" (nf readInstance testStr)
74 ]
75 ]
77o_1_space_elimination :: Int -> [Benchmark]
78o_1_space_elimination value =
79 [ bgroup "elimination"
80 [ benchFold "writeLastN.1"
81 (S.fold (IA.writeLastN 1)) (P.sourceUnfoldrM value)
82 , benchFold "writeLastN.10"
83 (S.fold (IA.writeLastN 10)) (P.sourceUnfoldrM value)
84#ifdef DEVBUILD
85{-
86 benchPureSink value "foldable/foldl'" foldableFoldl'
87 , benchPureSink value "foldable/sum" foldableSum
88-}
89#endif
90 ]
91 ]
93o_n_heap_serial :: Int -> [Benchmark]
94o_n_heap_serial value =
95 [ bgroup "elimination"
96 [
97 -- Converting the stream to an array
98 benchFold "writeLastN.Max" (S.fold (IA.writeLastN (value + 1)))
99 (P.sourceUnfoldrM value)
100 ]
101 ]
103moduleName :: String
104moduleName = "Data.Array.Generic"
106defStreamSize :: Int
107defStreamSize = defaultStreamSize
109main :: IO ()
110main = runWithCLIOpts defStreamSize allBenchmarks
112 where
114 allBenchmarks size =
115 [ bgroup (o_1_space_prefix moduleName) $
116 o_1_space_generation size ++ o_1_space_elimination size
117 , bgroup (o_n_space_prefix moduleName) $
118 o_n_heap_serial size
119 ] ++ commonBenchmarks size