on master help sign in
Move benchmarks of Array.Foreign out of common array benchmark mess
2370901adithyaov
benchmark/Streamly/Benchmark/Data/Array.hs+2−28
@@ -21,8 +21,6 @@ import qualified Streamly.Data.Array.Foreign as A
21
21
22#ifdef DATA_ARRAY
22#ifdef DATA_ARRAY
23import qualified Streamly.Internal.Data.Array as IA
23import qualified Streamly.Internal.Data.Array as IA
24−#else
25−import qualified Streamly.Internal.Data.Array.Foreign as IA
26#endif
24#endif
27import qualified Streamly.Prelude as S
25import qualified Streamly.Prelude as S
28
26
@@ -30,10 +28,6 @@ import Gauge
30import Streamly.Benchmark.Common hiding (benchPureSrc)
28import Streamly.Benchmark.Common hiding (benchPureSrc)
31import qualified Streamly.Benchmark.Prelude as P
29import qualified Streamly.Benchmark.Prelude as P
32
30
33−#ifdef MEMORY_ARRAY
34−import qualified GHC.Exts as GHC
35−#endif
36−
37#if !defined(DATA_ARRAY_PRIM) && !defined(DATA_ARRAY_PRIM_PINNED)
31#if !defined(DATA_ARRAY_PRIM) && !defined(DATA_ARRAY_PRIM_PINNED)
38import Control.DeepSeq (deepseq)
32import Control.DeepSeq (deepseq)
39#endif
33#endif
@@ -42,13 +36,6 @@ import Control.DeepSeq (deepseq)
42--
36--
43-------------------------------------------------------------------------------
37-------------------------------------------------------------------------------
44
38
45−#ifdef MEMORY_ARRAY
46−-- Drain a source that generates a pure array
47−{-# INLINE benchPureSrc #-}
48−benchPureSrc :: String -> (Int -> Ops.Stream a) -> Benchmark
49−benchPureSrc name src = benchPure name src id
50−#endif
51−
52{-# INLINE benchIO #-}
39{-# INLINE benchIO #-}
53benchIO :: NFData b => String -> (Int -> IO a) -> (a -> b) -> Benchmark
40benchIO :: NFData b => String -> (Int -> IO a) -> (a -> b) -> Benchmark
54benchIO name src f = bench name $ nfIO $
41benchIO name src f = bench name $ nfIO $
@@ -58,8 +45,7 @@ benchIO name src f = bench name $ nfIO $
58{-# INLINE benchIOSrc #-}
45{-# INLINE benchIOSrc #-}
59benchIOSrc ::
46benchIOSrc ::
60#if !defined(DATA_ARRAY_PRIM) \
47#if !defined(DATA_ARRAY_PRIM) \
61− && !defined(DATA_ARRAY_PRIM_PINNED) \
48+ && !defined(DATA_ARRAY_PRIM_PINNED)
62− && !defined(MEMORY_ARRAY)
63 NFData a =>
49 NFData a =>
64#endif
50#endif
65 String -> (Int -> IO (Ops.Stream a)) -> Benchmark
51 String -> (Int -> IO (Ops.Stream a)) -> Benchmark
@@ -91,12 +77,6 @@ o_1_space_generation value =
91 (Ops.sourceIntFromToFromList value)
77 (Ops.sourceIntFromToFromList value)
92 , benchIOSrc "writeN . unfoldr" (Ops.sourceUnfoldr value)
78 , benchIOSrc "writeN . unfoldr" (Ops.sourceUnfoldr value)
93 , benchIOSrc "writeN . fromList" (Ops.sourceFromList value)
79 , benchIOSrc "writeN . fromList" (Ops.sourceFromList value)
94−#ifdef MEMORY_ARRAY
95− , benchPureSrc "writeN . IsList.fromList" (Ops.sourceIsList value)
96− , benchPureSrc
97− "writeN . IsString.fromString"
98− (Ops.sourceIsString value)
99−#endif
100#if !defined(DATA_ARRAY_PRIM) && !defined(DATA_ARRAY_PRIM_PINNED)
80#if !defined(DATA_ARRAY_PRIM) && !defined(DATA_ARRAY_PRIM_PINNED)
101#ifdef DATA_SMALLARRAY
81#ifdef DATA_SMALLARRAY
102 , let testStr =
82 , let testStr =
@@ -120,10 +100,6 @@ o_1_space_elimination value =
120 , benchPureSink value "/=" Ops.eqInstanceNotEq
100 , benchPureSink value "/=" Ops.eqInstanceNotEq
121 , benchPureSink value "<" Ops.ordInstance
101 , benchPureSink value "<" Ops.ordInstance
122 , benchPureSink value "min" Ops.ordInstanceMin
102 , benchPureSink value "min" Ops.ordInstanceMin
123−#ifdef MEMORY_ARRAY
124− -- length is used to check for foldr/build fusion
125− , benchPureSink value "length . IsList.toList" (length . GHC.toList)
126−#endif
127 , benchIOSink value "foldl'" Ops.pureFoldl'
103 , benchIOSink value "foldl'" Ops.pureFoldl'
128 , benchIOSink value "read" Ops.unfoldReadDrain
104 , benchIOSink value "read" Ops.unfoldReadDrain
129 , benchIOSink value "toStreamRev" Ops.toStreamRevDrain
105 , benchIOSink value "toStreamRev" Ops.toStreamRevDrain
@@ -173,9 +149,7 @@ o_1_space_transformationX4 value =
173 ]
149 ]
174
150
175moduleName :: String
151moduleName :: String
176−#if defined(MEMORY_ARRAY)
177−moduleName = "Data.Array.Foreign"
178−#elif defined(DATA_ARRAY_PRIM)
152+#if defined(DATA_ARRAY_PRIM)
179moduleName = "Data.Array.Prim"
153moduleName = "Data.Array.Prim"
180#elif defined(DATA_ARRAY_PRIM_PINNED)
154#elif defined(DATA_ARRAY_PRIM_PINNED)
181moduleName = "Data.Array.Prim.Pinned"
155moduleName = "Data.Array.Prim.Pinned"
benchmark/Streamly/Benchmark/Data/Array/Foreign.hsnew file+134
@@ -0,0 +1,134 @@
1+{-# LANGUAGE CPP #-}
2+
3+#include "Streamly/Benchmark/Data/Array/CommonImports.hs"
4+
5+import Control.DeepSeq (deepseq)
6+
7+import qualified Streamly.Internal.Data.Array.Foreign as IA
8+import qualified GHC.Exts as GHC
9+
10+import qualified Streamly.Data.Array.Foreign as A
11+import qualified Streamly.Internal.Data.Array.Foreign as A
12+type Stream = A.Array
13+
14+#include "Streamly/Benchmark/Data/Array/Common.hs"
15+
16+-------------------------------------------------------------------------------
17+-- Benchmark helpers
18+-------------------------------------------------------------------------------
19+
20+-- Drain a source that generates a pure array
21+{-# INLINE benchPureSrc #-}
22+benchPureSrc :: String -> (Int -> Stream a) -> Benchmark
23+benchPureSrc name src = benchPure name src id
24+
25+-- Drain a source that generates an array in the IO monad
26+{-# INLINE benchIOSrc #-}
27+benchIOSrc :: String -> (Int -> IO (Stream a)) -> Benchmark
28+benchIOSrc name src = benchIO name src id
29+
30+-------------------------------------------------------------------------------
31+-- Bench Ops
32+-------------------------------------------------------------------------------
33+
34+{-# INLINE sourceIntFromToFromList #-}
35+sourceIntFromToFromList :: MonadIO m => Int -> Int -> m (Stream Int)
36+sourceIntFromToFromList value n = P.return $ A.fromListN value $ [n..n + value]
37+
38+{-# INLINE readInstance #-}
39+readInstance :: P.String -> Stream Int
40+readInstance str =
41+ let r = P.reads str
42+ in case r of
43+ [(x,"")] -> x
44+ _ -> P.error "readInstance: no parse"
45+
46+#ifdef DEVBUILD
47+{-
48+{-# INLINE foldableFoldl' #-}
49+foldableFoldl' :: Stream Int -> Int
50+foldableFoldl' = F.foldl' (+) 0
51+
52+{-# INLINE foldableSum #-}
53+foldableSum :: Stream Int -> Int
54+foldableSum = P.sum
55+-}
56+#endif
57+
58+{-# INLINE sourceIsList #-}
59+sourceIsList :: Int -> Int -> Stream Int
60+sourceIsList value n = GHC.fromList [n..n+value]
61+
62+{-# INLINE sourceIsString #-}
63+sourceIsString :: Int -> Int -> Stream P.Char
64+sourceIsString value n = GHC.fromString (P.replicate (n + value) 'a')
65+
66+{-# INLINE sourceIntFromToFromStream #-}
67+sourceIntFromToFromStream :: MonadIO m => Int -> Int -> m (Stream Int)
68+sourceIntFromToFromStream value n = S.fold A.write $ S.enumerateFromTo n (n + value)
69+
70+-------------------------------------------------------------------------------
71+-- Bench groups
72+-------------------------------------------------------------------------------
73+
74+o_1_space_generation :: Int -> [Benchmark]
75+o_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)
85+
86+ ]
87+ ]
88+
89+o_1_space_elimination :: Int -> [Benchmark]
90+o_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+ ]
105+
106+o_n_heap_serial :: Int -> [Benchmark]
107+o_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+ ]
115+
116+moduleName :: String
117+moduleName = "Data.Array.Foreign"
118+
119+defStreamSize :: Int
120+defStreamSize = defaultStreamSize
121+
122+main :: IO ()
123+main = runWithCLIOpts defStreamSize allBenchmarks
124+
125+ where
126+
127+ allBenchmarks size =
128+ [ bgroup (o_1_space_prefix moduleName) $ concat $
129+ [ o_1_space_generation size
130+ , o_1_space_elimination size
131+ ]
132+ , bgroup (o_n_space_prefix moduleName) $
133+ o_n_heap_serial size
134+ ] ++ commonBenchmarks size
benchmark/Streamly/Benchmark/Data/ArrayOps.hs+1−27
@@ -31,12 +31,7 @@ import qualified Data.Foldable as F
31#endif
31#endif
32#endif
32#endif
33
33
34−#if defined(MEMORY_ARRAY)
35−import qualified GHC.Exts as GHC
36−import qualified Streamly.Data.Array.Foreign as A
37−import qualified Streamly.Internal.Data.Array.Foreign as A
38−type Stream = A.Array
39−#elif defined(DATA_ARRAY_PRIM)
34+#if defined(DATA_ARRAY_PRIM)
40import qualified Streamly.Internal.Data.Array.Prim as A
35import qualified Streamly.Internal.Data.Array.Prim as A
41type Stream = A.Array
36type Stream = A.Array
42#elif defined(DATA_ARRAY_PRIM_PINNED)
37#elif defined(DATA_ARRAY_PRIM_PINNED)
@@ -57,7 +52,6 @@ type Stream = A.Array
57
52
58-------------------------------------------------------------------------------
53-------------------------------------------------------------------------------
59-- CPP Common to:
54-- CPP Common to:
60−-- MEMORY_ARRAY
61-- DATA_ARRAY
55-- DATA_ARRAY
62-- DATA_ARRAY_PRIM
56-- DATA_ARRAY_PRIM
63-- DATA_ARRAY_PRIM_PINNED
57-- DATA_ARRAY_PRIM_PINNED
@@ -87,7 +81,6 @@ sourceIntFromToFromList value n = P.return $ A.fromList $ [n..n + value]
87
81
88-------------------------------------------------------------------------------
82-------------------------------------------------------------------------------
89-- CPP Common to:
83-- CPP Common to:
90−-- MEMORY_ARRAY
91-- DATA_ARRAY
84-- DATA_ARRAY
92-- DATA_ARRAY_PRIM
85-- DATA_ARRAY_PRIM
93-- DATA_ARRAY_PRIM_PINNED
86-- DATA_ARRAY_PRIM_PINNED
@@ -98,29 +91,12 @@ sourceIntFromToFromList value n = P.return $ A.fromList $ [n..n + value]
98sourceIntFromToFromStream :: MonadIO m => Int -> Int -> m (Stream Int)
91sourceIntFromToFromStream :: MonadIO m => Int -> Int -> m (Stream Int)
99sourceIntFromToFromStream value n = S.fold A.write $ S.enumerateFromTo n (n + value)
92sourceIntFromToFromStream value n = S.fold A.write $ S.enumerateFromTo n (n + value)
100
93
101−-------------------------------------------------------------------------------
102−-- CPP Common to:
103−-- MEMORY_ARRAY
104−-------------------------------------------------------------------------------
105−
106−-- CPP:
107−#ifdef MEMORY_ARRAY
108−{-# INLINE sourceIsList #-}
109−sourceIsList :: Int -> Int -> Stream Int
110−sourceIsList value n = GHC.fromList [n..n+value]
111−
112−{-# INLINE sourceIsString #-}
113−sourceIsString :: Int -> Int -> Stream P.Char
114−sourceIsString value n = GHC.fromString (P.replicate (n + value) 'a')
115−#endif
116−
117-------------------------------------------------------------------------------
94-------------------------------------------------------------------------------
118-- Transformation
95-- Transformation
119-------------------------------------------------------------------------------
96-------------------------------------------------------------------------------
120
97
121-------------------------------------------------------------------------------
98-------------------------------------------------------------------------------
122-- CPP Common to:
99-- CPP Common to:
123−-- MEMORY_ARRAY
124-- DATA_ARRAY
100-- DATA_ARRAY
125-- DATA_ARRAY_PRIM
101-- DATA_ARRAY_PRIM
126-- DATA_ARRAY_PRIM_PINNED
102-- DATA_ARRAY_PRIM_PINNED
@@ -183,7 +159,6 @@ pureFoldl' = S.foldl' (+) 0 . S.unfold A.read
183
159
184-------------------------------------------------------------------------------
160-------------------------------------------------------------------------------
185-- CPP Common to:
161-- CPP Common to:
186−-- MEMORY_ARRAY
187-- DATA_ARRAY
162-- DATA_ARRAY
188-------------------------------------------------------------------------------
163-------------------------------------------------------------------------------
189
164
@@ -216,7 +191,6 @@ foldableSum = P.sum
216
191
217-------------------------------------------------------------------------------
192-------------------------------------------------------------------------------
218-- CPP Common to:
193-- CPP Common to:
219−-- MEMORY_ARRAY
220-- DATA_ARRAY
194-- DATA_ARRAY
221-- DATA_ARRAY_PRIM
195-- DATA_ARRAY_PRIM
222-- DATA_ARRAY_PRIM_PINNED
196-- DATA_ARRAY_PRIM_PINNED
benchmark/streamly-benchmarks.cabal+1−4
@@ -487,10 +487,7 @@ benchmark Data.Array.Prim.Pinned
487benchmark Data.Array.Foreign
487benchmark Data.Array.Foreign
488 import: bench-options
488 import: bench-options
489 type: exitcode-stdio-1.0
489 type: exitcode-stdio-1.0
490− hs-source-dirs: .
491− main-is: Streamly/Benchmark/Data/Array.hs
490+ main-is: Streamly/Benchmark/Data/Array/Foreign.hs
492− other-modules: Streamly.Benchmark.Data.ArrayOps
493− cpp-options: -DMEMORY_ARRAY
494 if flag(limit-build-mem)
491 if flag(limit-build-mem)
495 ghc-options: +RTS -M1000M -RTS
492 ghc-options: +RTS -M1000M -RTS
496
493
streamly.cabal+1
@@ -59,6 +59,7 @@ extra-source-files:
59 benchmark/Streamly/Benchmark/Data/Array/Common.hs
59 benchmark/Streamly/Benchmark/Data/Array/Common.hs
60 benchmark/Streamly/Benchmark/Data/Array/CommonImports.hs
60 benchmark/Streamly/Benchmark/Data/Array/CommonImports.hs
61 benchmark/Streamly/Benchmark/Data/Array/SmallArray.hs
61 benchmark/Streamly/Benchmark/Data/Array/SmallArray.hs
62+ benchmark/Streamly/Benchmark/Data/Array/Foreign.hs
62 benchmark/Streamly/Benchmark/Data/Array/Stream/Foreign.hs
63 benchmark/Streamly/Benchmark/Data/Array/Stream/Foreign.hs
63 benchmark/Streamly/Benchmark/Data/Parser/*.hs
64 benchmark/Streamly/Benchmark/Data/Parser/*.hs
64 benchmark/Streamly/Benchmark/Data/Stream/*.hs
65 benchmark/Streamly/Benchmark/Data/Stream/*.hs