@@ -5,105 +5,23 @@
5-- License : BSD3
5-- License : BSD3
6-- Maintainer : streamly@composewell.com
6-- Maintainer : streamly@composewell.com
8−import Control.DeepSeq (NFData(..))
9−import Data.Functor.Identity (Identity)
10−import System.Random (randomRIO)
12−import qualified Streamly.Benchmark.Prelude as Ops
14−import qualified Streamly.Data.Fold as FL
15−import qualified Streamly.Memory.Array as A
16−import qualified Streamly.Prelude as S
18−import qualified Streamly.Internal.Data.Fold as IFL
19−import qualified Streamly.Internal.Memory.Array as IA
20−import qualified Streamly.Internal.Prelude as IP
24import Streamly.Benchmark.Common
8import Streamly.Benchmark.Common
9+import Streamly.Benchmark.Prelude
26−-------------------------------------------------------------------------------
11+import Gauge
28−-------------------------------------------------------------------------------
30−-- We need a monadic bind here to make sure that the function f does not get
31−-- completely optimized out by the compiler in some cases.
33−-- | Takes a fold method, and uses it with a default source.
34−{-# INLINE benchIOSink #-}
36− :: (IsStream t, NFData b)
37− => Int -> String -> (t IO Int -> IO b) -> Benchmark
38−benchIOSink value name f =
39− bench name $ nfIO $ randomRIO (1,1) >>= f . Ops.source value
41−-- XXX We should be using sourceUnfoldrM for fair comparison with IO monad, but
42−-- we can't use it as it requires MonadAsync constraint.
43−{-# INLINE benchIdentitySink #-}
45− :: (IsStream t, NFData b)
46− => Int -> String -> (t Identity Int -> Identity b) -> Benchmark
47−benchIdentitySink value name f = bench name $ nf (f . Ops.sourceUnfoldr value) 1
49−-------------------------------------------------------------------------------
51−-------------------------------------------------------------------------------
53-- In addition to gauge options, the number of elements in the stream can be
13-- In addition to gauge options, the number of elements in the stream can be
54-- passed using the --stream-size option.
14-- passed using the --stream-size option.
56main :: IO ()
16main :: IO ()
58− -- XXX Fix indentation
59 (value, cfg, benches) <- parseCLIOpts defaultStreamSize
18 (value, cfg, benches) <- parseCLIOpts defaultStreamSize
60 size <- limitStreamSize value
19 size <- limitStreamSize value
62− size `seq` runMode (mode cfg) cfg benches
21+ runMode
63 -- Operations using O(1) stack space and O(n) heap space.
25 -- Operations using O(1) stack space and O(n) heap space.
64 -- Tail recursive left folds
26 -- Tail recursive left folds
65− [ bgroup "foldl"
27+ (concat [o_n_heap_serial_foldl size, o_n_heap_serial_buffering size])
67− -- Left folds for building a structure are inherently non-streaming
68− -- as the structure cannot be lazily consumed until fully built.
69− benchIOSink size "foldl'/build/IO" Ops.foldl'Build
70− , benchIdentitySink size "foldl'/build/Identity" Ops.foldl'Build
71− , benchIOSink size "foldlM'/build/IO" Ops.foldlM'Build
72− , benchIdentitySink size "foldlM'/build/Identity" Ops.foldlM'Build
74− , benchIOSink size "toStream" (S.fold IP.toStream)
75− , benchIOSink size "toStreamRev" (S.fold IP.toStreamRev)
77− , benchIOSink size "toList" (S.fold FL.toList)
78− , benchIOSink size "toListRevF" (S.fold IFL.toListRevF)
80− -- Converting the stream to an array
81− , benchIOSink size "lastN.Max" (S.fold (IA.lastN (size + 1)))
82− , benchIOSink size "writeN" (S.fold (A.writeN size))
84− -- Reversing/sorting a stream
85− , benchIOSink size "reverse" (Ops.reverse 1)
86− , benchIOSink size "reverse'" (Ops.reverse' 1)
90− -- Buffers the output of show/read.
91− -- XXX can the outputs be streaming? Can we have special read/show
92− -- style type classes, readM/showM supporting streaming effects?
93− bench "readsPrec pure streams" $
94− nf Ops.readInstance (mkString size)
95− , bench "readsPrec Haskell lists" $
96− nf Ops.readInstanceList (mkListString size)
97− , bench "showPrec Haskell lists" $
98− nf Ops.showInstanceList (mkList size)
100− -- interleave x/4 streams of 4 elements each. Needs to buffer
101− -- proportional to x/4. This is different from WSerial because
102− -- WSerial expands slowly because of binary interleave behavior and
103− -- this expands immediately because of Nary interleave behavior.
104− , benchIOSrc1 "concatUnfoldInterleaveRepl (x/4,4)"
105− (Ops.concatUnfoldInterleaveRepl4xN size)
106− , benchIOSrc1 "concatUnfoldRoundrobinRepl (x/4,4)"
107− (Ops.concatUnfoldRoundrobinRepl4xN size)