@@ -6,11 +6,15 @@
6-- Maintainer : streamly@composewell.com
6-- Maintainer : streamly@composewell.com
8{-# LANGUAGE CPP #-}
8{-# LANGUAGE CPP #-}
9+{-# LANGUAGE FlexibleContexts #-}
10import Control.DeepSeq (NFData(..))
11import Control.DeepSeq (NFData(..))
11import System.Random (randomRIO)
12import System.Random (randomRIO)
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
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
73+{-# INLINE sourceUnfoldrM #-}
74+sourceUnfoldrM :: (S.IsStream t, S.MonadAsync m) => Int -> Int -> t m Int
75+sourceUnfoldrM value n = S.unfoldrM step n
80+ else return (Just (cnt, cnt + 1))
83+source :: (S.MonadAsync m, S.IsStream t) => Int -> Int -> t m Int
84+source = sourceUnfoldrM
86+ -- | Takes a fold method, and uses it with a default source.
87+{-# INLINE 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
69o_1_space_generation :: Int -> [Benchmark]
93o_1_space_generation :: Int -> [Benchmark]
70o_1_space_generation value =
94o_1_space_generation value =
@@ -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 =
153+o_n_heap_serial :: Int -> [Benchmark]
154+o_n_heap_serial value =
155+ [ bgroup "elimination"
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))
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