1{-# LANGUAGE FlexibleContexts #-}
4-- Copyright : (c) 2018 Composewell Technologies
7-- Maintainer : streamly@composewell.com
10 (composeN, benchIO, benchIOSink, benchIOSrc, sourceUnfoldrM)
11import Streamly.Data.Stream (Stream)
12import Streamly.Internal.Data.Stream.Async (MonadAsync)
14import qualified Data.List as List
15import qualified Streamly.Data.Fold as Fold
16import qualified Streamly.Data.Stream as Stream
17import qualified Streamly.Internal.Data.Stream.Async as Async
20import Prelude hiding (mapM)
21import Streamly.Benchmark.Common
24moduleName = "Data.Stream.Async"
26-- XXX Write inspection tests to make sure no dictionaries are being passed
27-- around to find specialization issues. Could be really bad for perf.
29-------------------------------------------------------------------------------
31-------------------------------------------------------------------------------
39mapM n = composeN n $ Async.mapM return
41o_1_space_mapping :: Int -> [Benchmark]
42o_1_space_mapping value =
44 [ benchIOSink value "mapM" $ mapM 1
48-------------------------------------------------------------------------------
49-- Size conserving transformations (reordering, buffering, etc.)
50-------------------------------------------------------------------------------
52o_n_heap_buffering :: Int -> [Benchmark]
53o_n_heap_buffering value =
55 [benchIOSink value "mkAsync" (Stream.fold Fold.drain . Async.eval)]
58-------------------------------------------------------------------------------
60-------------------------------------------------------------------------------
63async2 :: Int -> Int -> IO ()
65 Stream.fold Fold.drain $
66 sourceUnfoldrM count n `Async.append` sourceUnfoldrM count (n + 1)
68{-# INLINE concatAsync2 #-}
69concatAsync2 :: Int -> Int -> IO ()
71 Stream.fold Fold.drain $
72 Async.concatList [sourceUnfoldrM count n, sourceUnfoldrM count (n + 1)]
74{-# INLINE interleave2 #-}
75interleave2 :: Int -> Int -> IO ()
77 Stream.fold Fold.drain $
78 sourceUnfoldrM count n `Async.interleave` sourceUnfoldrM count (n + 1)
80o_1_space_joining :: Int -> [Benchmark]
81o_1_space_joining value =
83 [ benchIOSrc1 "async (2 of n/2)" (async2 (value `div` 2))
84 , benchIOSrc1 "concat async (2 of n/2)" (concatAsync2 (value `div` 2))
85 , benchIOSrc1 "interleave (2 of n/2)" (interleave2 (value `div` 2))
89-------------------------------------------------------------------------------
91-------------------------------------------------------------------------------
93{-# INLINE sourceFoldMapWith #-}
94sourceFoldMapWith :: Int -> Int -> Stream IO Int
95sourceFoldMapWith value n =
96 Async.concatMap Stream.fromPure $ Stream.fromList [n..n+value]
98{-# INLINE sourceFoldMapWithStream #-}
99sourceFoldMapWithStream :: Int -> Int -> Stream IO Int
100sourceFoldMapWithStream value n =
101 Async.concatMap Stream.fromPure $ Stream.enumerateFromTo n (n + value)
103{-# INLINE concatFoldableWith #-}
104concatFoldableWith :: Int -> Int -> Stream IO Int
105concatFoldableWith value n =
108 then Just (Stream.fromPure x, x + 1)
110 list = List.unfoldr step n
111 in Async.concatList list
113o_1_space_concatFoldable :: Int -> [Benchmark]
114o_1_space_concatFoldable value =
115 [ bgroup "concat-foldable"
116 [ benchIOSrc "foldMapWith (<>) (List)"
117 (sourceFoldMapWith value)
118 , benchIOSrc "foldMapWith (<>) (Stream)"
119 (sourceFoldMapWithStream value)
120 , benchIOSrc "S.concatFoldableWith (<>) (List)"
121 (concatFoldableWith value)
125{-# INLINE concatMapStreamsWith #-}
131concatMapStreamsWith outer inner n =
132 Stream.fold Fold.drain
133 $ Async.concatMap (sourceUnfoldrM inner) (sourceUnfoldrM outer n)
135{-# INLINE concatFmapStreamsWith #-}
141concatFmapStreamsWith outer inner n =
142 Stream.fold Fold.drain
144 $ fmap (sourceUnfoldrM inner) (sourceUnfoldrM outer n)
146{-# INLINE concatMapInterleaveStreamsWith #-}
147concatMapInterleaveStreamsWith
152concatMapInterleaveStreamsWith outer inner n =
153 Stream.fold Fold.drain
154 $ Async.concatMapInterleave
155 (sourceUnfoldrM inner) (sourceUnfoldrM outer n)
157o_1_space_concatMap :: Int -> [Benchmark]
158o_1_space_concatMap value =
161 [ benchIO "concatMapWith (n of 1)"
162 (concatMapStreamsWith value 1)
163 , benchIO "concatMapWith (sqrt x of sqrt x)"
164 (concatMapStreamsWith value2 value2)
165 , benchIO "concatMapWith (1 of n)"
166 (concatMapStreamsWith 1 value)
167 , benchIO "concat . fmap (n of 1)"
168 (concatFmapStreamsWith value 1)
169 , benchIO "concatMapInterleaveWith (n of 1)"
170 (concatMapInterleaveStreamsWith value 1)
171 , benchIO "concatMapInterleaveWith (sqrt x of sqrt x)"
172 (concatMapInterleaveStreamsWith value2 value2)
173 , benchIO "concatMapInterleaveWith (1 of n)"
174 (concatMapInterleaveStreamsWith 1 value)
180 value2 = round $ sqrt (fromIntegral value :: Double)
182-------------------------------------------------------------------------------
183-- Monadic outer product
184-------------------------------------------------------------------------------
186{-# INLINE toNullAp #-}
187toNullAp :: Int -> Int -> IO ()
188toNullAp linearCount start =
189 Stream.fold Fold.drain
191 (fmap (+) (sourceUnfoldrM nestedCount2 start))
192 (sourceUnfoldrM nestedCount2 start)
196 nestedCount2 = round (fromIntegral linearCount**(1/2::Double))
198o_1_space_outerProduct :: Int -> [Benchmark]
199o_1_space_outerProduct value =
200 [ bgroup "monad-outer-product"
201 [ benchIO "toNullAp" $ toNullAp value
205-------------------------------------------------------------------------------
207-------------------------------------------------------------------------------
210main = runWithCLIOpts defaultStreamSize allBenchmarks
215 allBenchmarks value =
216 [ bgroup (o_1_space_prefix moduleName) $ concat
217 [ o_1_space_mapping value
218 , o_1_space_concatFoldable value
219 , o_1_space_concatMap value
220 , o_1_space_outerProduct value
221 , o_1_space_joining value
223 , bgroup (o_n_heap_prefix moduleName) (o_n_heap_buffering value)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list