on master help sign in
Cleanup bench:Data.Array by ridding of unecessary CPP pragmas
commit9f1144fd7c1670f66b941ebe9ee21d9fbc46a87e authoradithyaov <adithya@composewell.com> dateMon 29 Nov 2021, 00:53 (5 years ago) parentf8d80cc
2 files changed+51−93
benchmark/Streamly/Benchmark/Data/Array.hs+51−91 benchmark/streamly-benchmarks.cabal−2
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−
81{-# LANGUAGE CPP #-}
9−{-# LANGUAGE FlexibleContexts #-}
10−
11−import Control.DeepSeq (NFData(..))
12−import System.Random (randomRIO)
132
14−import qualified Streamly.Benchmark.Data.ArrayOps as Ops
3+#include "Streamly/Benchmark/Data/Array/CommonImports.hs"
154
16−#ifdef DATA_ARRAY
17−import qualified Streamly.Internal.Data.Array as A
18−#else
19−import qualified Streamly.Data.Array.Foreign as A
20−#endif
5+import Control.DeepSeq (deepseq)
216
22−#ifdef DATA_ARRAY
237import 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
2610
27−import Gauge
28−import Streamly.Benchmark.Common hiding (benchPureSrc)
29−import qualified Streamly.Benchmark.Prelude as P
30−
31−import Control.DeepSeq (deepseq)
11+#include "Streamly/Benchmark/Data/Array/Common.hs"
3212
3313-------------------------------------------------------------------------------
34−--
14+-- Benchmark helpers
3515-------------------------------------------------------------------------------
3616
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−
4217-- Drain a source that generates an array in the IO monad
4318{-# INLINE benchIOSrc #-}
44−benchIOSrc :: NFData a => String -> (Int -> IO (Ops.Stream a)) -> Benchmark
19+benchIOSrc :: NFData a => String -> (Int -> IO (Stream a)) -> Benchmark
4520benchIOSrc name src = benchIO name src id
4621
47−{-# INLINE benchPureSink #-}
48−benchPureSink :: NFData b => Int -> String -> (Ops.Stream Int -> b) -> Benchmark
49−benchPureSink value name f = benchIO name (Ops.sourceIntFromTo value) f
22+-------------------------------------------------------------------------------
23+-- Bench Ops
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"
5038
51−{-# INLINE benchIO' #-}
52−benchIO' :: NFData b => String -> (Int -> IO a) -> (a -> IO b) -> Benchmark
53−benchIO' name src f = bench name $ nfIO $
54− randomRIO (1,1) >>= src >>= f
39+#ifdef DEVBUILD
40+{-
41+{-# INLINE foldableFoldl' #-}
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)
5554
56−{-# INLINE benchIOSink #-}
57−benchIOSink :: NFData b => Int -> String -> (Ops.Stream Int -> IO b) -> Benchmark
58−benchIOSink value name f = benchIO' name (Ops.sourceIntFromTo value) f
55+-------------------------------------------------------------------------------
56+-- Bench groups
57+-------------------------------------------------------------------------------
5958
6059o_1_space_generation :: Int -> [Benchmark]
6160o_1_space_generation value =
6261 [ bgroup
6362 "generation"
64− [ benchIOSrc "writeN . intFromTo" (Ops.sourceIntFromTo value)
65−#ifndef DATA_SMALLARRAY
66− , benchIOSrc "write . intFromTo" (Ops.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)
63+ [ benchIOSrc "write . intFromTo" (sourceIntFromToFromStream value)
7364 , let testStr = mkListString value
74− in testStr `deepseq` (bench "read" $ nf Ops.readInstance testStr)
75− , benchPureSink value "show" Ops.showInstance
65+ in testStr `deepseq` (bench "read" $ nf readInstance testStr)
7666 ]
7767 ]
7868
7969o_1_space_elimination :: Int -> [Benchmark]
8070o_1_space_elimination value =
8171 [ 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"
9173 (S.fold (IA.writeLastN 1)) (P.sourceUnfoldrM value)
9274 , benchFold "writeLastN.10"
9375 (S.fold (IA.writeLastN 10)) (P.sourceUnfoldrM value)
9476#ifdef DEVBUILD
9577{-
96− , benchPureSink value "foldable/foldl'" Ops.foldableFoldl'
97− , benchPureSink value "foldable/sum" Ops.foldableSum
78+ benchPureSink value "foldable/foldl'" foldableFoldl'
79+ , benchPureSink value "foldable/sum" foldableSum
9880-}
9981#endif
10082 ]
@@ -107,29 +89,9 @@ o_n_heap_serial value =
10789 -- Converting the stream to an array
10890 benchFold "writeLastN.Max" (S.fold (IA.writeLastN (value + 1)))
10991 (P.sourceUnfoldrM value)
110− , benchFold "writeN" (S.fold (A.writeN value))
111− (P.sourceUnfoldrM value)
11292 ]
11393 ]
11494
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−
13395moduleName :: String
13496moduleName = "Data.Array"
13597
@@ -142,12 +104,10 @@ main = runWithCLIOpts defStreamSize allBenchmarks
142104 where
143105
144106 allBenchmarks size =
145− [ bgroup (o_1_space_prefix moduleName) $ concat
146− [ o_1_space_generation size
147− , o_1_space_elimination size
148− , o_1_space_transformation size
149− , o_1_space_transformationX4 size
150− ]
107+ [ bgroup (o_1_space_prefix moduleName) $ concat $
108+ [ o_1_space_generation size
109+ , o_1_space_elimination size
110+ ]
151111 , bgroup (o_n_space_prefix moduleName) $
152112 o_n_heap_serial size
153− ]
113+ ] ++ commonBenchmarks size
benchmark/streamly-benchmarks.cabal−2
@@ -459,8 +459,6 @@ benchmark Data.Array
459459 type: exitcode-stdio-1.0
460460 hs-source-dirs: .
461461 main-is: Streamly/Benchmark/Data/Array.hs
462− other-modules: Streamly.Benchmark.Data.ArrayOps
463− cpp-options: -DDATA_ARRAY
464462
465463benchmark Data.Array.Prim
466464 import: bench-options