on master help sign in
Merge O_n_Heap.hs into Prelude.hs

* Benchmark/Prelude/Serial/O_n_Heap -> lib/Streamly/Benchmark/Prelude.hs * Modify O_n_Heap.hs accordingly

commit69a81af68d62e022ca6e9f2a32c5ffe57889a331 authoradithyaov <adithya@composewell.com> dateWed 26 Feb 2020, 18:00 (7 years ago) parent8544b1f
2 files changed+68−90
benchmark/Streamly/Benchmark/Prelude/Serial/O_n_Heap.hs+8−90 benchmark/lib/Streamly/Benchmark/Prelude.hs+60
benchmark/Streamly/Benchmark/Prelude/Serial/O_n_Heap.hs+8−90
@@ -5,105 +5,23 @@
55-- License : BSD3
66-- Maintainer : streamly@composewell.com
77
8−import Control.DeepSeq (NFData(..))
9−import Data.Functor.Identity (Identity)
10−import System.Random (randomRIO)
11−
12−import qualified Streamly.Benchmark.Prelude as Ops
13−
14−import qualified Streamly.Data.Fold as FL
15−import qualified Streamly.Memory.Array as A
16−import qualified Streamly.Prelude as S
17−
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
21−
22−import Gauge
23−import Streamly
248import Streamly.Benchmark.Common
9+import Streamly.Benchmark.Prelude
2510
26−-------------------------------------------------------------------------------
27−--
28−-------------------------------------------------------------------------------
29−
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.
32−
33−-- | Takes a fold method, and uses it with a default source.
34−{-# INLINE benchIOSink #-}
35−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
40−
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 #-}
44−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
48−
49−-------------------------------------------------------------------------------
50−--
51−-------------------------------------------------------------------------------
11+import Gauge
5212
5313-- In addition to gauge options, the number of elements in the stream can be
5414-- passed using the --stream-size option.
5515--
5616main :: IO ()
5717main = do
58− -- XXX Fix indentation
5918 (value, cfg, benches) <- parseCLIOpts defaultStreamSize
6019 size <- limitStreamSize value
61−
62− size `seq` runMode (mode cfg) cfg benches
20+ size `seq`
21+ runMode
22+ (mode cfg)
23+ cfg
24+ benches
6325 -- Operations using O(1) stack space and O(n) heap space.
6426 -- Tail recursive left folds
65− [ bgroup "foldl"
66− [
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
73−
74− , benchIOSink size "toStream" (S.fold IP.toStream)
75− , benchIOSink size "toStreamRev" (S.fold IP.toStreamRev)
76−
77− , benchIOSink size "toList" (S.fold FL.toList)
78− , benchIOSink size "toListRevF" (S.fold IFL.toListRevF)
79−
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))
83−
84− -- Reversing/sorting a stream
85− , benchIOSink size "reverse" (Ops.reverse 1)
86− , benchIOSink size "reverse'" (Ops.reverse' 1)
87− ]
88− , bgroup "buffering"
89− [
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)
99−
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)
108− ]
109− ]
27+ (concat [o_n_heap_serial_foldl size, o_n_heap_serial_buffering size])
benchmark/lib/Streamly/Benchmark/Prelude.hs+60
@@ -60,6 +60,7 @@ import Streamly.Internal.Data.Time.Units
6060
6161import qualified Streamly.Internal.Data.Sink as Sink
6262
63+import qualified Streamly.Memory.Array as A
6364import qualified Streamly.Internal.Memory.Array as IA
6465import qualified Streamly.Internal.Data.Fold as IFL
6566import qualified Streamly.Internal.Prelude as IP
@@ -2105,3 +2106,62 @@ o_n_space_serial_foldr value =
21052106 ]
21062107 ]
21072108 ]
2109+
2110+
2111+o_n_heap_serial_foldl :: Int -> [Benchmark]
2112+o_n_heap_serial_foldl value =
2113+ [ bgroup
2114+ "serially"
2115+ [ bgroup
2116+ "foldl"
2117+ -- Left folds for building a structure are inherently non-streaming
2118+ -- as the structure cannot be lazily consumed until fully built.
2119+ [ benchIOSink value "foldl'/build/IO" foldl'Build
2120+ , benchIdentitySink value "foldl'/build/Identity" foldl'Build
2121+ , benchIOSink value "foldlM'/build/IO" foldlM'Build
2122+ , benchIdentitySink
2123+ value
2124+ "foldlM'/build/Identity"
2125+ foldlM'Build
2126+ , benchIOSink value "toStream" (S.fold IP.toStream)
2127+ , benchIOSink value "toStreamRev" (S.fold IP.toStreamRev)
2128+ , benchIOSink value "toList" (S.fold FL.toList)
2129+ , benchIOSink value "toListRevF" (S.fold IFL.toListRevF)
2130+ -- Converting the stream to an array
2131+ , benchIOSink value "lastN.Max" (S.fold (IA.lastN (value + 1)))
2132+ , benchIOSink value "writeN" (S.fold (A.writeN value))
2133+ -- Reversing/sorting a stream
2134+ , benchIOSink value "reverse" (reverse 1)
2135+ , benchIOSink value "reverse'" (reverse' 1)
2136+ ]
2137+ ]
2138+ ]
2139+
2140+o_n_heap_serial_buffering :: Int -> [Benchmark]
2141+o_n_heap_serial_buffering value =
2142+ [ bgroup
2143+ "serially"
2144+ [ bgroup
2145+ "buffering"
2146+ -- Buffers the output of show/read.
2147+ -- XXX can the outputs be streaming? Can we have special read/show
2148+ -- style type classes, readM/showM supporting streaming effects?
2149+ [ bench "readsPrec pure streams" $
2150+ nf readInstance (mkString value)
2151+ , bench "readsPrec Haskell lists" $
2152+ nf readInstanceList (mkListString value)
2153+ , bench "showPrec Haskell lists" $
2154+ nf showInstanceList (mkList value)
2155+ -- interleave x/4 streams of 4 elements each. Needs to buffer
2156+ -- proportional to x/4. This is different from WSerial because
2157+ -- WSerial expands slowly because of binary interleave behavior and
2158+ -- this expands immediately because of Nary interleave behavior.
2159+ , benchIOSrc1
2160+ "concatUnfoldInterleaveRepl (x/4,4)"
2161+ (concatUnfoldInterleaveRepl4xN value)
2162+ , benchIOSrc1
2163+ "concatUnfoldRoundrobinRepl (x/4,4)"
2164+ (concatUnfoldRoundrobinRepl4xN value)
2165+ ]
2166+ ]
2167+ ]