on master help sign in
Move benchmarks from Fold to Array and Serial
911a6b6Anurag Hooda
benchmark/Streamly/Benchmark/Data/Array.hs+37
@@ -6,11 +6,15 @@
6-- Maintainer : streamly@composewell.com
6-- Maintainer : streamly@composewell.com
7
7
8{-# LANGUAGE CPP #-}
8{-# LANGUAGE CPP #-}
9+{-# LANGUAGE FlexibleContexts #-}
9
10
10import Control.DeepSeq (NFData(..))
11import Control.DeepSeq (NFData(..))
11import System.Random (randomRIO)
12import System.Random (randomRIO)
12
13
13import qualified Streamly.Benchmark.Data.ArrayOps as Ops
14import qualified Streamly.Benchmark.Data.ArrayOps as Ops
15+import qualified Streamly.Data.Array.Storable.Foreign as A
16+import qualified Streamly.Internal.Data.Array.Storable.Foreign as IA
17+import qualified Streamly.Prelude as S
14
18
15import Gauge
19import Gauge
16import Streamly.Benchmark.Common hiding (benchPureSrc)
20import Streamly.Benchmark.Common hiding (benchPureSrc)
@@ -66,6 +70,26 @@ benchIO' name src f = bench name $ nfIO $
66benchIOSink :: NFData b => Int -> String -> (Ops.Stream Int -> IO b) -> Benchmark
70benchIOSink :: NFData b => Int -> String -> (Ops.Stream Int -> IO b) -> Benchmark
67benchIOSink value name f = benchIO' name (Ops.sourceIntFromTo value) f
71benchIOSink value name f = benchIO' name (Ops.sourceIntFromTo value) f
68
72
73+{-# INLINE sourceUnfoldrM #-}
74+sourceUnfoldrM :: (S.IsStream t, S.MonadAsync m) => Int -> Int -> t m Int
75+sourceUnfoldrM value n = S.unfoldrM step n
76+ where
77+ step cnt =
78+ if cnt > n + value
79+ then return Nothing
80+ else return (Just (cnt, cnt + 1))
81+
82+{-# INLINE source #-}
83+source :: (S.MonadAsync m, S.IsStream t) => Int -> Int -> t m Int
84+source = sourceUnfoldrM
85+
86+ -- | Takes a fold method, and uses it with a default source.
87+{-# INLINE benchIOSinkF #-}
88+benchIOSinkF
89+ :: (S.IsStream t, NFData b)
90+ => Int -> String -> (t IO Int -> IO b) -> Benchmark
91+benchIOSinkF value name f = bench name $ nfIO $ randomRIO (1,1) >>= f . source value
92+
69o_1_space_generation :: Int -> [Benchmark]
93o_1_space_generation :: Int -> [Benchmark]
70o_1_space_generation value =
94o_1_space_generation value =
71 [ bgroup
95 [ bgroup
@@ -115,6 +139,8 @@ o_1_space_elimination value =
115 , benchIOSink value "foldl'" Ops.pureFoldl'
139 , benchIOSink value "foldl'" Ops.pureFoldl'
116 , benchIOSink value "read" Ops.unfoldReadDrain
140 , benchIOSink value "read" Ops.unfoldReadDrain
117 , benchIOSink value "toStreamRev" Ops.toStreamRevDrain
141 , benchIOSink value "toStreamRev" Ops.toStreamRevDrain
142+ , benchIOSinkF value "lastN.1" (S.fold (IA.lastN 1))
143+ , benchIOSinkF value "lastN.10" (S.fold (IA.lastN 10))
118#if !defined(DATA_ARRAY_PRIM) && !defined(DATA_ARRAY_PRIM_PINNED)
144#if !defined(DATA_ARRAY_PRIM) && !defined(DATA_ARRAY_PRIM_PINNED)
119#ifdef DEVBUILD
145#ifdef DEVBUILD
120 , benchPureSink value "foldable/foldl'" Ops.foldableFoldl'
146 , benchPureSink value "foldable/foldl'" Ops.foldableFoldl'
@@ -124,6 +150,16 @@ o_1_space_elimination value =
124 ]
150 ]
125 ]
151 ]
126
152
153+o_n_heap_serial :: Int -> [Benchmark]
154+o_n_heap_serial value =
155+ [ bgroup "elimination"
156+ [
157+ -- Converting the stream to an array
158+ benchIOSinkF value "lastN.Max" (S.fold (IA.lastN (value + 1)))
159+ , benchIOSinkF value "writeN" (S.fold (A.writeN value))
160+ ]
161+ ]
162+
127o_1_space_transformation :: Int -> [Benchmark]
163o_1_space_transformation :: Int -> [Benchmark]
128o_1_space_transformation value =
164o_1_space_transformation value =
129 [ bgroup "transformation"
165 [ bgroup "transformation"
@@ -176,5 +212,6 @@ main = do
176 , o_1_space_elimination size
212 , o_1_space_elimination size
177 , o_1_space_transformation size
213 , o_1_space_transformation size
178 , o_1_space_transformationX4 size
214 , o_1_space_transformationX4 size
215+ , o_n_heap_serial size
179 ]
216 ]
180 ]
217 ]
benchmark/Streamly/Benchmark/Data/Fold.hs+4−10
@@ -188,8 +188,6 @@ o_1_space_serial_elimination value =
188 (S.fold (FL.drainSepBy $ (>=) (value + 1)))
188 (S.fold (FL.drainSepBy $ (>=) (value + 1)))
189 , benchIOSink value "sink" (S.fold $ Sink.toFold Sink.drain)
189 , benchIOSink value "sink" (S.fold $ Sink.toFold Sink.drain)
190 , benchIOSink value "last" (S.fold FL.last)
190 , benchIOSink value "last" (S.fold FL.last)
191− , benchIOSink value "lastN.1" (S.fold (IA.lastN 1))
192− , benchIOSink value "lastN.10" (S.fold (IA.lastN 10))
193 , benchIOSink value "length" (S.fold FL.length)
191 , benchIOSink value "length" (S.fold FL.length)
194 , benchIOSink value "sum" (S.fold FL.sum)
192 , benchIOSink value "sum" (S.fold FL.sum)
195 , benchIOSink value "sum (foldMap)" (S.fold (FL.foldMap Sum))
193 , benchIOSink value "sum (foldMap)" (S.fold (FL.foldMap Sum))
@@ -328,18 +326,14 @@ o_1_space_serial_composition value =
328o_n_heap_serial :: Int -> [Benchmark]
326o_n_heap_serial :: Int -> [Benchmark]
329o_n_heap_serial value =
327o_n_heap_serial value =
330 [ bgroup "serially"
328 [ bgroup "serially"
331− [ bgroup "elimination"
329+ [ bgroup "elimination"
332 -- Left folds for building a structure are inherently non-streaming
330 -- Left folds for building a structure are inherently non-streaming
333 -- as the structure cannot be lazily consumed until fully built.
331 -- as the structure cannot be lazily consumed until fully built.
334− [ benchIOSink value "toStream" (S.fold IP.toStream)
332+ [
335− , benchIOSink value "toStreamRev" (S.fold IP.toStreamRev)
336− , benchIOSink value "toList" (S.fold FL.toList)
333+ benchIOSink value "toList" (S.fold FL.toList)
337 , benchIOSink value "toListRevF" (S.fold FL.toListRevF)
334 , benchIOSink value "toListRevF" (S.fold FL.toListRevF)
338− -- Converting the stream to an array
339− , benchIOSink value "lastN.Max" (S.fold (IA.lastN (value + 1)))
340− , benchIOSink value "writeN" (S.fold (A.writeN value))
341 ]
335 ]
342− ]
336+ ]
343 ]
337 ]
344
338
345-------------------------------------------------------------------------------
339-------------------------------------------------------------------------------
benchmark/Streamly/Benchmark/Prelude/Serial/Elimination.hs+3
@@ -41,6 +41,7 @@ import Gauge
41import Streamly.Prelude (SerialT, IsStream, serially)
41import Streamly.Prelude (SerialT, IsStream, serially)
42import Streamly.Benchmark.Common
42import Streamly.Benchmark.Common
43import Streamly.Benchmark.Prelude
43import Streamly.Benchmark.Prelude
44+import qualified Streamly.Internal.Data.Stream.IsStream as IP
44import Prelude hiding (length, sum, or, and, any, all, notElem, elem, (!!),
45import Prelude hiding (length, sum, or, and, any, all, notElem, elem, (!!),
45 lookup, repeat, minimum, maximum, product, last, mapM_, init)
46 lookup, repeat, minimum, maximum, product, last, mapM_, init)
46import qualified Prelude
47import qualified Prelude
@@ -539,6 +540,8 @@ o_n_heap_elimination_toList value =
539 -- Converting the stream to a list or pure stream in a strict monad
540 -- Converting the stream to a list or pure stream in a strict monad
540 [ benchIOSink value "toListRev" Internal.toListRev
541 [ benchIOSink value "toListRev" Internal.toListRev
541 , benchIOSink value "toPureRev" Internal.toPureRev
542 , benchIOSink value "toPureRev" Internal.toPureRev
543+ , benchIOSink value "toStream" (S.fold IP.toStream)
544+ , benchIOSink value "toStreamRev" (S.fold IP.toStreamRev)
542 ]
545 ]
543 ]
546 ]
544
547