on master help sign in
Cleanup bench:Data.Array by ridding of unecessary CPP pragmas
9f1144fadithyaov
benchmark/Streamly/Benchmark/Data/Array.hs+51−91
@@ -1,100 +1,82 @@
1−-- |
2−-- Module : Main
3−-- Copyright : (c) 2020 Composewell Technologies
4−--
5−-- License : BSD-3-Clause
6−-- Maintainer : streamly@composewell.com
7−
8{-# LANGUAGE CPP #-}
1{-# LANGUAGE CPP #-}
9−{-# LANGUAGE FlexibleContexts #-}
10−
11−import Control.DeepSeq (NFData(..))
12−import System.Random (randomRIO)
13
2
14−import qualified Streamly.Benchmark.Data.ArrayOps as Ops
3+#include "Streamly/Benchmark/Data/Array/CommonImports.hs"
15
4
16−#ifdef DATA_ARRAY
5+import Control.DeepSeq (deepseq)
17−import qualified Streamly.Internal.Data.Array as A
18−#else
19−import qualified Streamly.Data.Array.Foreign as A
20−#endif
21
6
22−#ifdef DATA_ARRAY
23import qualified Streamly.Internal.Data.Array as IA
7import qualified Streamly.Internal.Data.Array as IA
24−#endif
25−import qualified Streamly.Prelude as S
8+import qualified Streamly.Internal.Data.Array as A
9+type Stream = A.Array
26
10
27−import Gauge
28−import Streamly.Benchmark.Common hiding (benchPureSrc)
11+#include "Streamly/Benchmark/Data/Array/Common.hs"
29−import qualified Streamly.Benchmark.Prelude as P
30−
31−import Control.DeepSeq (deepseq)
32
12
33-------------------------------------------------------------------------------
13-------------------------------------------------------------------------------
34−--
14+-- Benchmark helpers
35-------------------------------------------------------------------------------
15-------------------------------------------------------------------------------
36
16
37−{-# INLINE benchIO #-}
38−benchIO :: NFData b => String -> (Int -> IO a) -> (a -> b) -> Benchmark
39−benchIO name src f = bench name $ nfIO $
40− randomRIO (1,1) >>= src >>= return . f
41−
42-- Drain a source that generates an array in the IO monad
17-- Drain a source that generates an array in the IO monad
43{-# INLINE benchIOSrc #-}
18{-# INLINE benchIOSrc #-}
44−benchIOSrc :: NFData a => String -> (Int -> IO (Ops.Stream a)) -> Benchmark
19+benchIOSrc :: NFData a => String -> (Int -> IO (Stream a)) -> Benchmark
45benchIOSrc name src = benchIO name src id
20benchIOSrc name src = benchIO name src id
46
21
47−{-# INLINE benchPureSink #-}
22+-------------------------------------------------------------------------------
48−benchPureSink :: NFData b => Int -> String -> (Ops.Stream Int -> b) -> Benchmark
23+-- Bench Ops
49−benchPureSink value name f = benchIO name (Ops.sourceIntFromTo value) f
24+-------------------------------------------------------------------------------
25+
26+{-# INLINE sourceIntFromToFromList #-}
27+sourceIntFromToFromList :: MonadIO m => Int -> Int -> m (Stream Int)
28+sourceIntFromToFromList value n = P.return $ A.fromListN value $ [n..n + value]
29+
30+
31+{-# INLINE readInstance #-}
32+readInstance :: P.String -> Stream Int
33+readInstance str =
34+ let r = P.reads str
35+ in case r of
36+ [(x,"")] -> x
37+ _ -> P.error "readInstance: no parse"
50
38
51−{-# INLINE benchIO' #-}
39+#ifdef DEVBUILD
52−benchIO' :: NFData b => String -> (Int -> IO a) -> (a -> IO b) -> Benchmark
40+{-
53−benchIO' name src f = bench name $ nfIO $
41+{-# INLINE foldableFoldl' #-}
54− randomRIO (1,1) >>= src >>= f
42+foldableFoldl' :: Stream Int -> Int
43+foldableFoldl' = F.foldl' (+) 0
44+
45+{-# INLINE foldableSum #-}
46+foldableSum :: Stream Int -> Int
47+foldableSum = P.sum
48+-}
49+#endif
50+
51+{-# INLINE sourceIntFromToFromStream #-}
52+sourceIntFromToFromStream :: MonadIO m => Int -> Int -> m (Stream Int)
53+sourceIntFromToFromStream value n = S.fold A.write $ S.enumerateFromTo n (n + value)
55
54
56−{-# INLINE benchIOSink #-}
55+-------------------------------------------------------------------------------
57−benchIOSink :: NFData b => Int -> String -> (Ops.Stream Int -> IO b) -> Benchmark
56+-- Bench groups
58−benchIOSink value name f = benchIO' name (Ops.sourceIntFromTo value) f
57+-------------------------------------------------------------------------------
59
58
60o_1_space_generation :: Int -> [Benchmark]
59o_1_space_generation :: Int -> [Benchmark]
61o_1_space_generation value =
60o_1_space_generation value =
62 [ bgroup
61 [ bgroup
63 "generation"
62 "generation"
64− [ benchIOSrc "writeN . intFromTo" (Ops.sourceIntFromTo value)
65−#ifndef DATA_SMALLARRAY
66− , benchIOSrc "write . intFromTo" (Ops.sourceIntFromToFromStream value)
63+ [ benchIOSrc "write . intFromTo" (sourceIntFromToFromStream value)
67−#endif
68− , benchIOSrc
69− "fromList . intFromTo"
70− (Ops.sourceIntFromToFromList value)
71− , benchIOSrc "writeN . unfoldr" (Ops.sourceUnfoldr value)
72− , benchIOSrc "writeN . fromList" (Ops.sourceFromList value)
73 , let testStr = mkListString value
64 , let testStr = mkListString value
74− in testStr `deepseq` (bench "read" $ nf Ops.readInstance testStr)
65+ in testStr `deepseq` (bench "read" $ nf readInstance testStr)
75− , benchPureSink value "show" Ops.showInstance
76 ]
66 ]
77 ]
67 ]
78
68
79o_1_space_elimination :: Int -> [Benchmark]
69o_1_space_elimination :: Int -> [Benchmark]
80o_1_space_elimination value =
70o_1_space_elimination value =
81 [ bgroup "elimination"
71 [ bgroup "elimination"
82− [ benchPureSink value "id" id
83− , benchPureSink value "==" Ops.eqInstance
84− , benchPureSink value "/=" Ops.eqInstanceNotEq
85− , benchPureSink value "<" Ops.ordInstance
86− , benchPureSink value "min" Ops.ordInstanceMin
87− , benchIOSink value "foldl'" Ops.pureFoldl'
88− , benchIOSink value "read" Ops.unfoldReadDrain
89− , benchIOSink value "toStreamRev" Ops.toStreamRevDrain
90− , benchFold "writeLastN.1"
72+ [ benchFold "writeLastN.1"
91 (S.fold (IA.writeLastN 1)) (P.sourceUnfoldrM value)
73 (S.fold (IA.writeLastN 1)) (P.sourceUnfoldrM value)
92 , benchFold "writeLastN.10"
74 , benchFold "writeLastN.10"
93 (S.fold (IA.writeLastN 10)) (P.sourceUnfoldrM value)
75 (S.fold (IA.writeLastN 10)) (P.sourceUnfoldrM value)
94#ifdef DEVBUILD
76#ifdef DEVBUILD
95{-
77{-
96− , benchPureSink value "foldable/foldl'" Ops.foldableFoldl'
78+ benchPureSink value "foldable/foldl'" foldableFoldl'
97− , benchPureSink value "foldable/sum" Ops.foldableSum
79+ , benchPureSink value "foldable/sum" foldableSum
98-}
80-}
99#endif
81#endif
100 ]
82 ]
@@ -107,29 +89,9 @@ o_n_heap_serial value =
107 -- Converting the stream to an array
89 -- Converting the stream to an array
108 benchFold "writeLastN.Max" (S.fold (IA.writeLastN (value + 1)))
90 benchFold "writeLastN.Max" (S.fold (IA.writeLastN (value + 1)))
109 (P.sourceUnfoldrM value)
91 (P.sourceUnfoldrM value)
110− , benchFold "writeN" (S.fold (A.writeN value))
111− (P.sourceUnfoldrM value)
112 ]
92 ]
113 ]
93 ]
114
94
115−o_1_space_transformation :: Int -> [Benchmark]
116−o_1_space_transformation value =
117− [ bgroup "transformation"
118− [ benchIOSink value "scanl'" (Ops.scanl' value 1)
119− , benchIOSink value "scanl1'" (Ops.scanl1' value 1)
120− , benchIOSink value "map" (Ops.map value 1)
121− ]
122− ]
123−
124−o_1_space_transformationX4 :: Int -> [Benchmark]
125−o_1_space_transformationX4 value =
126− [ bgroup "transformationX4"
127− [ benchIOSink value "scanl'" (Ops.scanl' value 4)
128− , benchIOSink value "scanl1'" (Ops.scanl1' value 4)
129− , benchIOSink value "map" (Ops.map value 4)
130− ]
131− ]
132−
133moduleName :: String
95moduleName :: String
134moduleName = "Data.Array"
96moduleName = "Data.Array"
135
97
@@ -142,12 +104,10 @@ main = runWithCLIOpts defStreamSize allBenchmarks
142 where
104 where
143
105
144 allBenchmarks size =
106 allBenchmarks size =
145− [ bgroup (o_1_space_prefix moduleName) $ concat
107+ [ bgroup (o_1_space_prefix moduleName) $ concat $
146− [ o_1_space_generation size
108+ [ o_1_space_generation size
147− , o_1_space_elimination size
109+ , o_1_space_elimination size
148− , o_1_space_transformation size
149− , o_1_space_transformationX4 size
150− ]
110+ ]
151 , bgroup (o_n_space_prefix moduleName) $
111 , bgroup (o_n_space_prefix moduleName) $
152 o_n_heap_serial size
112 o_n_heap_serial size
153− ]
113+ ] ++ commonBenchmarks size
benchmark/streamly-benchmarks.cabal−2
@@ -459,8 +459,6 @@ benchmark Data.Array
459 type: exitcode-stdio-1.0
459 type: exitcode-stdio-1.0
460 hs-source-dirs: .
460 hs-source-dirs: .
461 main-is: Streamly/Benchmark/Data/Array.hs
461 main-is: Streamly/Benchmark/Data/Array.hs
462− other-modules: Streamly.Benchmark.Data.ArrayOps
463− cpp-options: -DDATA_ARRAY
464
462
465benchmark Data.Array.Prim
463benchmark Data.Array.Prim
466 import: bench-options
464 import: bench-options