on master help sign in
Move benchmarks from Fold to Array and Serial
commit911a6b62d8a8bb2d72fd08fd2d93aca3b1407af0 authorAnurag Hooda <anuraghooda@composewell.com> dateSat 5 Dec 2020, 17:59 (6 years ago) parent49b7fe7
3 files changed+44−10
benchmark/Streamly/Benchmark/Data/Array.hs+37 benchmark/Streamly/Benchmark/Data/Fold.hs+4−10 benchmark/Streamly/Benchmark/Prelude/Serial/Elimination.hs+3
benchmark/Streamly/Benchmark/Data/Array.hs+37
@@ -6,11 +6,15 @@
66-- Maintainer : streamly@composewell.com
77
88{-# LANGUAGE CPP #-}
9+{-# LANGUAGE FlexibleContexts #-}
910
1011import Control.DeepSeq (NFData(..))
1112import System.Random (randomRIO)
1213
1314import 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
1418
1519import Gauge
1620import Streamly.Benchmark.Common hiding (benchPureSrc)
@@ -66,6 +70,26 @@ benchIO' name src f = bench name $ nfIO $
6670benchIOSink :: NFData b => Int -> String -> (Ops.Stream Int -> IO b) -> Benchmark
6771benchIOSink value name f = benchIO' name (Ops.sourceIntFromTo value) f
6872
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+
6993o_1_space_generation :: Int -> [Benchmark]
7094o_1_space_generation value =
7195 [ bgroup
@@ -115,6 +139,8 @@ o_1_space_elimination value =
115139 , benchIOSink value "foldl'" Ops.pureFoldl'
116140 , benchIOSink value "read" Ops.unfoldReadDrain
117141 , 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))
118144#if !defined(DATA_ARRAY_PRIM) && !defined(DATA_ARRAY_PRIM_PINNED)
119145#ifdef DEVBUILD
120146 , benchPureSink value "foldable/foldl'" Ops.foldableFoldl'
@@ -124,6 +150,16 @@ o_1_space_elimination value =
124150 ]
125151 ]
126152
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+
127163o_1_space_transformation :: Int -> [Benchmark]
128164o_1_space_transformation value =
129165 [ bgroup "transformation"
@@ -176,5 +212,6 @@ main = do
176212 , o_1_space_elimination size
177213 , o_1_space_transformation size
178214 , o_1_space_transformationX4 size
215+ , o_n_heap_serial size
179216 ]
180217 ]
benchmark/Streamly/Benchmark/Data/Fold.hs+4−10
@@ -188,8 +188,6 @@ o_1_space_serial_elimination value =
188188 (S.fold (FL.drainSepBy $ (>=) (value + 1)))
189189 , benchIOSink value "sink" (S.fold $ Sink.toFold Sink.drain)
190190 , 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))
193191 , benchIOSink value "length" (S.fold FL.length)
194192 , benchIOSink value "sum" (S.fold FL.sum)
195193 , benchIOSink value "sum (foldMap)" (S.fold (FL.foldMap Sum))
@@ -328,18 +326,14 @@ o_1_space_serial_composition value =
328326o_n_heap_serial :: Int -> [Benchmark]
329327o_n_heap_serial value =
330328 [ bgroup "serially"
331− [ bgroup "elimination"
329+ [ bgroup "elimination"
332330 -- Left folds for building a structure are inherently non-streaming
333331 -- as the structure cannot be lazily consumed until fully built.
334− [ benchIOSink value "toStream" (S.fold IP.toStream)
335− , benchIOSink value "toStreamRev" (S.fold IP.toStreamRev)
336− , benchIOSink value "toList" (S.fold FL.toList)
332+ [
333+ benchIOSink value "toList" (S.fold FL.toList)
337334 , 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))
341335 ]
342− ]
336+ ]
343337 ]
344338
345339-------------------------------------------------------------------------------
benchmark/Streamly/Benchmark/Prelude/Serial/Elimination.hs+3
@@ -41,6 +41,7 @@ import Gauge
4141import Streamly.Prelude (SerialT, IsStream, serially)
4242import Streamly.Benchmark.Common
4343import Streamly.Benchmark.Prelude
44+import qualified Streamly.Internal.Data.Stream.IsStream as IP
4445import Prelude hiding (length, sum, or, and, any, all, notElem, elem, (!!),
4546 lookup, repeat, minimum, maximum, product, last, mapM_, init)
4647import qualified Prelude
@@ -539,6 +540,8 @@ o_n_heap_elimination_toList value =
539540 -- Converting the stream to a list or pure stream in a strict monad
540541 [ benchIOSink value "toListRev" Internal.toListRev
541542 , benchIOSink value "toPureRev" Internal.toPureRev
543+ , benchIOSink value "toStream" (S.fold IP.toStream)
544+ , benchIOSink value "toStreamRev" (S.fold IP.toStreamRev)
542545 ]
543546 ]
544547