@@ -1,207 +1,140 @@
11-- |
22-- Module : Streamly.Benchmark.Data.Stream.StreamK
3−-- Copyright : (c) 2018 Composewell Technologies
3+-- Copyright : (c) 2018 Harendra Kumar
44--
55-- License : BSD3
66-- Maintainer : streamly@composewell.com
77
8−{-# LANGUAGE CPP #-}
98{-# LANGUAGE FlexibleContexts #-}
109{-# LANGUAGE ScopedTypeVariables #-}
11−{-# OPTIONS_GHC -Wno-orphans #-}
1210
13−#ifdef __HADDOCK_VERSION__
14−#undef INSPECTION
15−#endif
16−
17−#ifdef INSPECTION
18−{-# LANGUAGE TemplateHaskell #-}
19−{-# OPTIONS_GHC -fplugin Test.Inspection.Plugin #-}
20−#endif
21−
22−module Main (main) where
11+module Streamly.Benchmark.Data.Stream.StreamK
12+ (
13+ o_1_space
14+ , o_n_stack
15+ , o_n_heap
16+ , o_n_space
17+ , o_1_space_list
18+ )
19+where
2320
24−#if !MIN_VERSION_base(4,18,0)
25−import Control.Applicative (liftA2)
26−#endif
2721import Control.Monad (when)
2822import Data.Maybe (isJust)
23+import Prelude
24+ (Monad, Int, (+), ($), (.), return, even, (>), (<=), div,
25+ subtract, undefined, Maybe(..), not, (>>=),
26+ maxBound, flip, (<$>), (<*>), round, (/), (**), (<), foldr, fmap)
2927import System.Random (randomRIO)
30−import Prelude hiding
31− ( Foldable(..), tail, mapM_, last, map, mapM, concatMap, zipWith, init, iterate
32− , repeat, replicate
33− )
34−
3528import qualified Prelude as P
3629import qualified Data.List as List
3730
38−import qualified Streamly.Internal.Data.StreamK as S
39−
40−import Test.Tasty.Bench (bench, nfIO, bgroup, Benchmark, defaultMain)
41−
42−import Streamly.Benchmark.Common
43−
44−#ifdef INSPECTION
45−import Test.Inspection
46−#endif
47−
48−{-
49−
50−Benchmarks that need to be added
31+import qualified Streamly.Internal.Data.Stream.StreamK as S
32+import qualified Streamly.Internal.Data.Stream.Prelude as SP
33+import qualified Streamly.Internal.Data.SVar as S
5134
52−-- fromList
35+import Streamly.Benchmark.Common (benchFold)
36+import Gauge (bench, nfIO, bgroup, Benchmark)
5337
54−-- bindWith
55−-- concatPairsWith
56−-- apWith
57−-- apSerial
58−-- apSerialDiscardFst
59−-- apSerialDiscardSnd
38+value, value2, value3, value16, maxValue :: Int
39+value = 100000
40+value2 = round (P.fromIntegral value**(1/2::P.Double)) -- double nested loop
41+value3 = round (P.fromIntegral value**(1/3::P.Double)) -- triple nested loop
42+value16 = round (P.fromIntegral value**(1/16::P.Double)) -- triple nested loop
43+maxValue = value
6044
61−-- the
62−-- serial
63−-- consMStream
64−-- withLocal
65−-- mfix
66−
67−-- elem
68−-- notElem
69−-- all
70−-- any
71−-- minimum
72−-- minimumBy
73−-- maximum
74−-- maximumBy
75−-- findIndices
76−-- lookup
77−-- findM
78−-- find
79−-- (!!)
80−
81−-- foldlM'
82−-- foldlT
83−-- foldlx'
84−-- foldlMx'
85−-- fold
86−
87−-- sequence
88−
89−-- foldrSM
90−-- buildS
91−-- buildM
92−-- augmentS
93−-- augmentSM
94−-- foldr
95−-- foldr1
96−-- foldrM
97−-- foldrT
98−
99−-- intersperseM
100−-- insertBy
101−-- deleteBy
102−-- reverse
103−-- mapMaybe
104−
105−-- zipWithM
106−-- mergeBy
107−-- mergeByM
108−
109−-- toStreamK (Probably can be skipped)
110−-- hoist
45+-------------------------------------------------------------------------------
46+-- Benchmark ops
47+-------------------------------------------------------------------------------
11148
112−-- scanlx'
49+{-# INLINE toNull #-}
50+{-# INLINE uncons #-}
51+{-# INLINE nullTail #-}
52+{-# INLINE headTail #-}
53+{-# INLINE zip #-}
54+toNull, uncons, nullTail, headTail, zip
55+ :: Monad m
56+ => Stream m Int -> m ()
11357
114−-}
58+{-# INLINE toList #-}
59+toList :: Monad m => Stream m Int -> m [Int]
60+{-# INLINE foldl #-}
61+foldl :: Monad m => Stream m Int -> m Int
62+{-# INLINE last #-}
63+last :: Monad m => Stream m Int -> m (Maybe Int)
11564
11665-------------------------------------------------------------------------------
11766-- Stream generation and elimination
11867-------------------------------------------------------------------------------
11968
120−type Stream m a = S.StreamK m a
69+type Stream m a = S.Stream m a
12170
122−{-# INLINE unfoldr #-}
123−unfoldr :: Int -> Int -> Stream m Int
124−unfoldr streamLen n = S.unfoldr step n
71+{-# INLINE sourceUnfoldr #-}
72+sourceUnfoldr :: Int -> Stream m Int
73+sourceUnfoldr n = S.unfoldr step n
12574 where
12675 step cnt =
127− if cnt > n + streamLen
76+ if cnt > n + value
12877 then Nothing
12978 else Just (cnt, cnt + 1)
13079
131−{-# INLINE unfoldrM #-}
132−unfoldrM :: Monad m => Int -> Int -> Stream m Int
133−unfoldrM streamLen n = S.unfoldrMWith S.consM step n
80+{-# INLINE sourceUnfoldrN #-}
81+sourceUnfoldrN :: Int -> Int -> Stream m Int
82+sourceUnfoldrN m n = S.unfoldr step n
83+ where
84+ step cnt =
85+ if cnt > n + m
86+ then Nothing
87+ else Just (cnt, cnt + 1)
88+
89+{-# INLINE sourceUnfoldrM #-}
90+sourceUnfoldrM :: S.MonadAsync m => Int -> Stream m Int
91+sourceUnfoldrM n = S.unfoldrM step n
92+ where
93+ step cnt =
94+ if cnt > n + value
95+ then return Nothing
96+ else return (Just (cnt, cnt + 1))
97+
98+{-# INLINE sourceUnfoldrMN #-}
99+sourceUnfoldrMN :: S.MonadAsync m => Int -> Int -> Stream m Int
100+sourceUnfoldrMN m n = S.unfoldrM step n
134101 where
135102 step cnt =
136− if cnt > n + streamLen
103+ if cnt > n + m
137104 then return Nothing
138105 else return (Just (cnt, cnt + 1))
139106
140−{-# INLINE repeat #-}
141−repeat :: Int -> Int -> Stream m Int
142−repeat streamLen = S.take streamLen . S.repeat
143−
144−{-# INLINE repeatM #-}
145−repeatM :: Monad m => Int -> Int -> Stream m Int
146−repeatM streamLen = S.take streamLen . S.repeatM . return
147−
148−{-# INLINE replicate #-}
149−replicate :: Int -> Int -> Stream m Int
150−replicate = S.replicate
151−
152−{-# INLINE replicateM #-}
153−replicateM :: Monad m => Int -> Int -> Stream m Int
154−replicateM streamLen = S.replicateMWith S.consM streamLen . return
155−
156−{-# INLINE iterate #-}
157−iterate :: Int -> Int -> Stream m Int
158−iterate streamLen = S.take streamLen . S.iterate (+1)
159−
160−{-# INLINE iterateM #-}
161−iterateM :: Monad m => Int -> Int -> Stream m Int
162−iterateM streamLen = S.take streamLen . S.iterateM (return . (+1)) . return
163−
164−{-# INLINE fromFoldable #-}
165−fromFoldable :: Int -> Int -> Stream m Int
166−fromFoldable streamLen n = S.fromFoldable [n..n+streamLen]
167−
168−{-# INLINE fromFoldableM #-}
169−fromFoldableM :: Monad m => Int -> Int -> Stream m Int
170−fromFoldableM streamLen n =
171− List.foldr S.consM S.nil (Prelude.fmap return [n..n+streamLen])
172−
173−{-# INLINABLE concatMapFoldableWith #-}
174−concatMapFoldableWith :: P.Foldable f
175− => (Stream m b -> Stream m b -> Stream m b)
176− -> (a -> Stream m b)
177− -> f a
178− -> Stream m b
179−concatMapFoldableWith f g = P.foldr (f . g) S.nil
180−
181−{-# INLINE concatMapFoldableSerial #-}
182−concatMapFoldableSerial :: Int -> Int -> Stream m Int
183−concatMapFoldableSerial streamLen n =
184− concatMapFoldableWith S.append S.fromPure [n..n+streamLen]
185−
186−{-# INLINE concatMapFoldableSerialM #-}
187−concatMapFoldableSerialM :: Monad m => Int -> Int -> Stream m Int
188−concatMapFoldableSerialM streamLen n =
189− concatMapFoldableWith S.append (S.fromEffect . return) [n..n+streamLen]
107+{-# INLINE sourceFromFoldable #-}
108+sourceFromFoldable :: Int -> Stream m Int
109+sourceFromFoldable n = S.fromFoldable [n..n+value]
110+
111+{-# INLINE sourceFromFoldableM #-}
112+sourceFromFoldableM :: S.MonadAsync m => Int -> Stream m Int
113+sourceFromFoldableM n =
114+ Prelude.foldr S.consM S.nil (Prelude.fmap return [n..n+value])
115+
116+{-# INLINE sourceFoldMapWith #-}
117+sourceFoldMapWith :: Int -> Stream m Int
118+sourceFoldMapWith n = SP.foldMapWith S.serial S.yield [n..n+value]
119+
120+{-# INLINE sourceFoldMapWithM #-}
121+sourceFoldMapWithM :: Monad m => Int -> Stream m Int
122+sourceFoldMapWithM n = SP.foldMapWith S.serial (S.yieldM . return) [n..n+value]
190123
191124-------------------------------------------------------------------------------
192125-- Elimination
193126-------------------------------------------------------------------------------
194127
195−{-# INLINE drain #-}
196−drain :: Monad m => Stream m a -> m ()
197−drain = S.drain
128+{-# INLINE runStream #-}
129+runStream :: Monad m => Stream m a -> m ()
130+runStream = S.drain
131+-- runStream = S.mapM_ (\_ -> return ())
198132
199133{-# INLINE mapM_ #-}
200134mapM_ :: Monad m => Stream m a -> m ()
201135mapM_ = S.mapM_ (\_ -> return ())
202136
203−{-# INLINE uncons #-}
204−uncons :: Monad m => Stream m Int -> m ()
137+toNull = runStream
205138uncons s = do
206139 r <- S.uncons s
207140 case r of
@@ -209,350 +142,205 @@ uncons s = do
209142 Just (_, t) -> uncons t
210143
211144{-# INLINE init #-}
212−init :: Monad m => Stream m a -> m ()
145+init :: (Monad m, S.IsStream t) => t m a -> m ()
213146init s = do
214147 t <- S.init s
215148 P.mapM_ S.drain t
216149
217150{-# INLINE tail #-}
218−tail :: Monad m => Stream m a -> m ()
151+tail :: (Monad m, S.IsStream t) => t m a -> m ()
219152tail s = S.tail s >>= P.mapM_ tail
220153
221−{-# INLINE nullTail #-}
222−nullTail :: Monad m => Stream m Int -> m ()
223154nullTail s = do
224155 r <- S.null s
225156 when (not r) $ S.tail s >>= P.mapM_ nullTail
226157
227−{-# INLINE headTail #-}
228−headTail :: Monad m => Stream m Int -> m ()
229158headTail s = do
230159 h <- S.head s
231160 when (isJust h) $ S.tail s >>= P.mapM_ headTail
232161
233−{-# INLINE toList #-}
234−toList :: Monad m => Stream m Int -> m [Int]
235162toList = S.toList
236−
237−{-# INLINE foldl' #-}
238−foldl' :: Monad m => Stream m Int -> m Int
239−foldl' = S.foldl' (+) 0
240−
241−{-# INLINE last #-}
242−last :: Monad m => Stream m Int -> m (Maybe Int)
243−last = S.last
163+foldl = S.foldl' (+) 0
164+last = S.last
244165
245166-------------------------------------------------------------------------------
246167-- Transformation
247168-------------------------------------------------------------------------------
248169
170+{-# INLINE transform #-}
171+transform :: Monad m => Stream m a -> m ()
172+transform = runStream
173+
249174{-# INLINE composeN #-}
250175composeN
251176 :: Monad m
252177 => Int -> (Stream m Int -> Stream m Int) -> Stream m Int -> m ()
253178composeN n f =
254179 case n of
255− 1 -> drain . f
256− 2 -> drain . f . f
257− 3 -> drain . f . f . f
258− 4 -> drain . f . f . f . f
180+ 1 -> transform . f
181+ 2 -> transform . f . f
182+ 3 -> transform . f . f . f
183+ 4 -> transform . f . f . f . f
259184 _ -> undefined
260185
261−{-# INLINE scanl' #-}
262−scanl' :: Monad m => Int -> Stream m Int -> m ()
263−scanl' n = composeN n $ S.scanl' (+) 0
264−
186+{-# INLINE scan #-}
265187{-# INLINE map #-}
266−map :: Monad m => Int -> Stream m Int -> m ()
267−map n = composeN n $ S.map (+ 1)
268−
269188{-# INLINE fmapK #-}
270−fmapK :: Monad m => Int -> Stream m Int -> m ()
271−fmapK n = composeN n $ P.fmap (+ 1)
272−
273−{-# INLINE mapM #-}
274−mapM :: Monad m => Int -> Stream m Int -> m ()
275−mapM n = composeN n $ S.mapMWith S.consM return
276−
277−{-# INLINE mapMSerial #-}
278−mapMSerial :: Monad m => Int -> Stream m Int -> m ()
279−mapMSerial n = composeN n $ S.mapMSerial return
280−
281189{-# INLINE filterEven #-}
282−filterEven :: Monad m => Int -> Stream m Int -> m ()
283−filterEven n = composeN n $ S.filter even
284−
285190{-# INLINE filterAllOut #-}
286−filterAllOut :: Monad m => Int -> Int -> Stream m Int -> m ()
287−filterAllOut streamLen n = composeN n $ S.filter (> streamLen)
288−
289191{-# INLINE filterAllIn #-}
290−filterAllIn :: Monad m => Int -> Int -> Stream m Int -> m ()
291−filterAllIn streamLen n = composeN n $ S.filter (<= streamLen)
292−
293192{-# INLINE _takeOne #-}
294−_takeOne :: Monad m => Int -> Stream m Int -> m ()
295−_takeOne n = composeN n $ S.take 1
296−
297193{-# INLINE takeAll #-}
298−takeAll :: Monad m => Int -> Int -> Stream m Int -> m ()
299−takeAll streamLen n = composeN n $ S.take streamLen
300−
301194{-# INLINE takeWhileTrue #-}
302−takeWhileTrue :: Monad m => Int -> Int -> Stream m Int -> m ()
303−takeWhileTrue streamLen n = composeN n $ S.takeWhile (<= streamLen)
304−
305195{-# INLINE dropOne #-}
306−dropOne :: Monad m => Int -> Stream m Int -> m ()
307−dropOne n = composeN n $ S.drop 1
308−
309196{-# INLINE dropAll #-}
310−dropAll :: Monad m => Int -> Int -> Stream m Int -> m ()
311−dropAll streamLen n = composeN n $ S.drop streamLen
312−
313197{-# INLINE dropWhileTrue #-}
314−dropWhileTrue :: Monad m => Int -> Int -> Stream m Int -> m ()
315−dropWhileTrue streamLen n = composeN n $ S.dropWhile (<= streamLen)
316−
317198{-# INLINE dropWhileFalse #-}
318−dropWhileFalse :: Monad m => Int -> Stream m Int -> m ()
319−dropWhileFalse n = composeN n $ S.dropWhile (<= 1)
320−
321199{-# INLINE foldrS #-}
322−foldrS :: Monad m => Int -> Stream m Int -> m ()
323−foldrS n = composeN n $ S.foldrS S.cons S.nil
324−
325200{-# INLINE foldlS #-}
326−foldlS :: Monad m => Int -> Stream m Int -> m ()
327−foldlS n = composeN n $ S.foldlS (flip S.cons) S.nil
201+{-# INLINE concatMap #-}
202+scan, map, fmapK, filterEven, filterAllOut,
203+ filterAllIn, _takeOne, takeAll, takeWhileTrue, dropAll, dropOne,
204+ dropWhileTrue, dropWhileFalse, foldrS, foldlS, concatMap
205+ :: Monad m
206+ => Int -> Stream m Int -> m ()
328207
208+{-# INLINE mapM #-}
209+{-# INLINE mapMSerial #-}
329210{-# INLINE intersperse #-}
330−intersperse :: Monad m => Int -> Int -> Stream m Int -> m ()
331−intersperse streamLen n = composeN n $ S.intersperse streamLen
211+mapM, mapMSerial, intersperse
212+ :: S.MonadAsync m => Int -> Stream m Int -> m ()
213+
214+scan n = composeN n $ S.scanl' (+) 0
215+map n = composeN n $ P.fmap (+1)
216+fmapK n = composeN n $ P.fmap (+1)
217+mapM n = composeN n $ S.mapM return
218+mapMSerial n = composeN n $ S.mapMSerial return
219+filterEven n = composeN n $ S.filter even
220+filterAllOut n = composeN n $ S.filter (> maxValue)
221+filterAllIn n = composeN n $ S.filter (<= maxValue)
222+_takeOne n = composeN n $ S.take 1
223+takeAll n = composeN n $ S.take maxValue
224+takeWhileTrue n = composeN n $ S.takeWhile (<= maxValue)
225+dropOne n = composeN n $ S.drop 1
226+dropAll n = composeN n $ S.drop maxValue
227+dropWhileTrue n = composeN n $ S.dropWhile (<= maxValue)
228+dropWhileFalse n = composeN n $ S.dropWhile (<= 1)
229+foldrS n = composeN n $ S.foldrS S.cons S.nil
230+foldlS n = composeN n $ S.foldlS (flip S.cons) S.nil
231+-- We use a (sqrt n) element stream as source and then concat the same stream
232+-- for each element to produce an n element stream.
233+concatMap n = composeN n $ (\s -> S.concatMap (\_ -> s) s)
234+intersperse n = composeN n $ S.intersperse maxValue
332235
333236-------------------------------------------------------------------------------
334237-- Iteration
335238-------------------------------------------------------------------------------
336239
240+iterStreamLen, maxIters :: Int
241+iterStreamLen = 10
242+maxIters = 10000
243+
337244{-# INLINE iterateSource #-}
338245iterateSource
339− :: Monad m => Int -> (Stream m Int -> Stream m Int) -> Int -> Int -> Stream m Int
340−iterateSource iterStreamLen g i n = f i (unfoldrM iterStreamLen n)
246+ :: S.MonadAsync m
247+ => (Stream m Int -> Stream m Int) -> Int -> Int -> Stream m Int
248+iterateSource g i n = f i (sourceUnfoldrMN iterStreamLen n)
341249 where
342250 f (0 :: Int) m = g m
343251 f x m = g (f (x P.- 1) m)
344252
345−-- this is quadratic
346−{-# INLINE iterateScan #-}
347−iterateScan :: Monad m => Int -> Int -> Int -> Stream m Int
348−iterateScan iterStreamLen maxIters =
349− iterateSource iterStreamLen (S.scanl' (+) 0) (maxIters `div` 10)
350−
351−-- this is quadratic
352−{-# INLINE iterateDropWhileFalse #-}
353−iterateDropWhileFalse :: Monad m => Int -> Int -> Int -> Int -> Stream m Int
354−iterateDropWhileFalse streamLen iterStreamLen maxIters =
355− iterateSource iterStreamLen (S.dropWhile (> streamLen)) (maxIters `div` 10)
356−
357253{-# INLINE iterateMapM #-}
358−iterateMapM :: Monad m => Int -> Int -> Int -> Stream m Int
359−iterateMapM iterStreamLen =
360− iterateSource iterStreamLen (S.mapMWith S.consM return)
361−
254+{-# INLINE iterateScan #-}
362255{-# INLINE iterateFilterEven #-}
363−iterateFilterEven :: Monad m => Int -> Int -> Int -> Stream m Int
364−iterateFilterEven iterStreamLen = iterateSource iterStreamLen (S.filter even)
365−
366256{-# INLINE iterateTakeAll #-}
367−iterateTakeAll :: Monad m => Int -> Int -> Int -> Int -> Stream m Int
368−iterateTakeAll streamLen iterStreamLen =
369− iterateSource iterStreamLen (S.take streamLen)
370−
371257{-# INLINE iterateDropOne #-}
372−iterateDropOne :: Monad m => Int -> Int -> Int -> Stream m Int
373−iterateDropOne iterStreamLen = iterateSource iterStreamLen (S.drop 1)
374−
258+{-# INLINE iterateDropWhileFalse #-}
375259{-# INLINE iterateDropWhileTrue #-}
376−iterateDropWhileTrue ::
377− Monad m => Int -> Int -> Int -> Int -> Stream m Int
378−iterateDropWhileTrue streamLen iterStreamLen =
379− iterateSource iterStreamLen (S.dropWhile (<= streamLen))
260+iterateMapM, iterateScan, iterateFilterEven, iterateTakeAll, iterateDropOne,
261+ iterateDropWhileFalse, iterateDropWhileTrue
262+ :: S.MonadAsync m
263+ => Int -> Stream m Int
380264
381−-------------------------------------------------------------------------------
382−-- Zipping
383−-------------------------------------------------------------------------------
265+-- this is quadratic
266+iterateScan = iterateSource (S.scanl' (+) 0) (maxIters `div` 10)
267+iterateDropWhileFalse = iterateSource (S.dropWhile (> maxValue))
268+ (maxIters `div` 10)
384269
385−{-# INLINE zipWith #-}
386−zipWith :: Monad m => Stream m Int -> m ()
387−zipWith src = drain $ S.zipWith (,) src src
270+iterateMapM = iterateSource (S.mapM return) maxIters
271+iterateFilterEven = iterateSource (S.filter even) maxIters
272+iterateTakeAll = iterateSource (S.take maxValue) maxIters
273+iterateDropOne = iterateSource (S.drop 1) maxIters
274+iterateDropWhileTrue = iterateSource (S.dropWhile (<= maxValue)) maxIters
388275
389−{-# INLINE zipWithM #-}
390−zipWithM :: Monad m => Stream m Int -> m ()
391−zipWithM src = drain $ S.zipWithM (curry return) src src
276+-------------------------------------------------------------------------------
277+-- Zipping and concat
278+-------------------------------------------------------------------------------
392279
393−{-# INLINE sortByK #-}
394−sortByK :: (a -> a -> Ordering) -> Stream m a -> Stream m a
395−sortByK f = S.mergeMapWith (S.mergeBy f) S.fromPure
280+zip src = transform $ S.zipWith (,) src src
396281
397−{-# INLINE sortBy #-}
398−sortBy :: Monad m => Stream m Int -> m ()
399−sortBy = drain . sortByK compare
282+{-# INLINE concatMapRepl4xN #-}
283+concatMapRepl4xN :: Monad m => Stream m Int -> m ()
284+concatMapRepl4xN src = transform $ (S.concatMap (S.replicate 4) src)
400285
401286-------------------------------------------------------------------------------
402287-- Mixed Composition
403288-------------------------------------------------------------------------------
404289
405290{-# INLINE scanMap #-}
406−scanMap :: Monad m => Int -> Stream m Int -> m ()
407−scanMap n = composeN n $ S.map (subtract 1) . S.scanl' (+) 0
408−
409291{-# INLINE dropMap #-}
410−dropMap :: Monad m => Int -> Stream m Int -> m ()
411−dropMap n = composeN n $ S.map (subtract 1) . S.drop 1
412−
413292{-# INLINE dropScan #-}
414−dropScan :: Monad m => Int -> Stream m Int -> m ()
415−dropScan n = composeN n $ S.scanl' (+) 0 . S.drop 1
416−
417293{-# INLINE takeDrop #-}
418−takeDrop :: Monad m => Int -> Int -> Stream m Int -> m ()
419−takeDrop streamLen n = composeN n $ S.drop 1 . S.take streamLen
420−
421294{-# INLINE takeScan #-}
422−takeScan :: Monad m => Int -> Int -> Stream m Int -> m ()
423−takeScan streamLen n = composeN n $ S.scanl' (+) 0 . S.take streamLen
424−
425295{-# INLINE takeMap #-}
426−takeMap :: Monad m => Int -> Int -> Stream m Int -> m ()
427−takeMap streamLen n = composeN n $ S.map (subtract 1) . S.take streamLen
428−
429296{-# INLINE filterDrop #-}
430−filterDrop :: Monad m => Int -> Int -> Stream m Int -> m ()
431−filterDrop streamLen n = composeN n $ S.drop 1 . S.filter (<= streamLen)
432−
433297{-# INLINE filterTake #-}
434−filterTake :: Monad m => Int -> Int -> Stream m Int -> m ()
435−filterTake streamLen n = composeN n $ S.take streamLen . S.filter (<= streamLen)
436−
437298{-# INLINE filterScan #-}
438−filterScan :: Monad m => Int -> Stream m Int -> m ()
439−filterScan n = composeN n $ S.scanl' (+) 0 . S.filter (<= maxBound)
440−
441299{-# INLINE filterMap #-}
442−filterMap :: Monad m => Int -> Int -> Stream m Int -> m ()
443−filterMap streamLen n = composeN n $ S.map (subtract 1) . S.filter (<= streamLen)
444−
445−-------------------------------------------------------------------------------
446−-- ConcatMap
447−-------------------------------------------------------------------------------
448−
449−-- concatMap unfoldrM/unfoldrM
450−
451−{-# INLINE concatMap #-}
452−concatMap :: Int -> Int -> Int -> IO ()
453−concatMap outer inner n =
454− S.drain $ S.concatMap
455− (\_ -> unfoldrM inner n)
456− (unfoldrM outer n)
457−
458−#ifdef INSPECTION
459−inspect $ hasNoTypeClasses 'concatMap
460−#endif
461−
462−-- concatMap unfoldr/unfoldr
463−
464−{-# INLINE concatMapPure #-}
465−concatMapPure :: Int -> Int -> Int -> IO ()
466−concatMapPure outer inner n =
467− S.drain $ S.concatMap
468− (\_ -> unfoldr inner n)
469− (unfoldr outer n)
470−
471−#ifdef INSPECTION
472−inspect $ hasNoTypeClasses 'concatMapPure
473−#endif
474−
475−-- concatMap replicate/unfoldrM
476−
477−{-# INLINE concatMapRepl #-}
478−concatMapRepl :: Int -> Int -> Int -> IO ()
479−concatMapRepl outer inner n =
480− S.drain $ S.concatMap (S.replicate inner) (unfoldrM outer n)
481−
482−#ifdef INSPECTION
483−inspect $ hasNoTypeClasses 'concatMapRepl
484−#endif
485−
486−-- concatMapWith
487−
488−{-# INLINE sourceConcatMapId #-}
489−sourceConcatMapId :: Monad m
490− => Int -> Int -> Stream m (Stream m Int)
491−sourceConcatMapId val n =
492− S.fromFoldable $ fmap (S.fromEffect . return) [n..n+val]
493−
494−{-# INLINE concatMapBySerial #-}
495−concatMapBySerial :: Int -> Int -> Int -> IO ()
496−concatMapBySerial outer inner n =
497− S.drain $ S.concatMapWith S.append
498− (unfoldrM inner)
499− (unfoldrM outer n)
300+scanMap, dropMap, dropScan, takeDrop, takeScan, takeMap, filterDrop,
301+ filterTake, filterScan, filterMap
302+ :: Monad m => Int -> Stream m Int -> m ()
303+
304+scanMap n = composeN n $ S.map (subtract 1) . S.scanl' (+) 0
305+dropMap n = composeN n $ S.map (subtract 1) . S.drop 1
306+dropScan n = composeN n $ S.scanl' (+) 0 . S.drop 1
307+takeDrop n = composeN n $ S.drop 1 . S.take maxValue
308+takeScan n = composeN n $ S.scanl' (+) 0 . S.take maxValue
309+takeMap n = composeN n $ S.map (subtract 1) . S.take maxValue
310+filterDrop n = composeN n $ S.drop 1 . S.filter (<= maxValue)
311+filterTake n = composeN n $ S.take maxValue . S.filter (<= maxValue)
312+filterScan n = composeN n $ S.scanl' (+) 0 . S.filter (<= maxBound)
313+filterMap n = composeN n $ S.map (subtract 1) . S.filter (<= maxValue)
500314
501315-------------------------------------------------------------------------------
502316-- Nested Composition
503317-------------------------------------------------------------------------------
504318
505−instance Monad m => Applicative (S.StreamK m) where
506− {-# INLINE pure #-}
507− pure = S.fromPure
508−
509− {-# INLINE (<*>) #-}
510− (<*>) = S.crossApply
511−
512− {-# INLINE liftA2 #-}
513− liftA2 f x = (<*>) (fmap f x)
514−
515− {-# INLINE (*>) #-}
516− (*>) = S.crossApplySnd
517−
518− {-# INLINE (<*) #-}
519− (<*) = S.crossApplyFst
520−
521−-- NOTE: even though concatMap for StreamD is 3x faster compared to StreamK,
522−-- the monad instance of StreamD is slower than StreamK after foldr/build
523−-- fusion.
524−instance Monad m => Monad (S.StreamK m) where
525− {-# INLINE return #-}
526− return = pure
527−
528− {-# INLINE (>>=) #-}
529− (>>=) = flip S.concatMap
530−
531−{-# INLINE drainApplicative #-}
532−drainApplicative :: Monad m => Stream m Int -> m ()
533−drainApplicative s = drain $ do
319+{-# INLINE toNullApNested #-}
320+toNullApNested :: Monad m => Stream m Int -> m ()
321+toNullApNested s = runStream $ do
534322 (+) <$> s <*> s
535323
536−{-# INLINE drainMonad #-}
537−drainMonad :: Monad m => Stream m Int -> m ()
538−drainMonad s = drain $ do
324+{-# INLINE toNullNested #-}
325+toNullNested :: Monad m => Stream m Int -> m ()
326+toNullNested s = runStream $ do
539327 x <- s
540328 y <- s
541329 return $ x + y
542330
543−{-# INLINE drainMonad3 #-}
544−drainMonad3 :: Monad m => Stream m Int -> m ()
545−drainMonad3 s = drain $ do
331+{-# INLINE toNullNested3 #-}
332+toNullNested3 :: Monad m => Stream m Int -> m ()
333+toNullNested3 s = runStream $ do
546334 x <- s
547335 y <- s
548336 z <- s
549337 return $ x + y + z
550338
551−{-# INLINE filterAllOutMonad #-}
552−filterAllOutMonad
339+{-# INLINE filterAllOutNested #-}
340+filterAllOutNested
553341 :: Monad m
554342 => Stream m Int -> m ()
555−filterAllOutMonad str = drain $ do
343+filterAllOutNested str = runStream $ do
556344 x <- str
557345 y <- str
558346 let s = x + y
@@ -560,11 +348,11 @@ filterAllOutMonad str = drain $ do
560348 then return s
561349 else S.nil
562350
563−{-# INLINE filterAllInMonad #-}
564−filterAllInMonad
351+{-# INLINE filterAllInNested #-}
352+filterAllInNested
565353 :: Monad m
566354 => Stream m Int -> m ()
567−filterAllInMonad str = drain $ do
355+filterAllInNested str = runStream $ do
568356 x <- str
569357 y <- str
570358 let s = x + y
@@ -576,14 +364,9 @@ filterAllInMonad str = drain $ do
576364-- Nested Composition Pure lists
577365-------------------------------------------------------------------------------
578366
579−-- There are several list benchmarks here for comparison with lists. It is easy
580−-- and convenient to see the comparisons when they are here, otherwise we'll
581−-- have to add a separate module for list benchmarks with the same names and
582−-- then add a comparison in bench.sh.
583−
584−{-# INLINE unfoldrList #-}
585−unfoldrList :: Int -> Int -> [Int]
586−unfoldrList maxval n = List.unfoldr step n
367+{-# INLINE sourceUnfoldrList #-}
368+sourceUnfoldrList :: Int -> Int -> [Int]
369+sourceUnfoldrList maxval n = List.unfoldr step n
587370 where
588371 step cnt =
589372 if cnt > n + maxval
@@ -633,302 +416,194 @@ filterAllInNestedList str = do
633416-- Benchmarks
634417-------------------------------------------------------------------------------
635418
636−moduleName :: String
637−moduleName = "Data.StreamK"
419+o_1_space :: [Benchmark]
420+o_1_space =
421+ [ bgroup "streamK"
422+ [ bgroup "generation"
423+ [ benchFold "unfoldr" toNull sourceUnfoldr
424+ , benchFold "unfoldrM" toNull sourceUnfoldrM
638425
639−o_1_space_generation :: Int -> Benchmark
640−o_1_space_generation streamLen =
641− bgroup "generation"
642− [ benchFold "unfoldr" drain (unfoldr streamLen)
643− , benchFold "unfoldrM" drain (unfoldrM streamLen)
644− , benchFold "repeat" drain (repeat streamLen)
645− , benchFold "repeatM" drain (repeatM streamLen)
646− , benchFold "replicate" drain (replicate streamLen)
647− , benchFold "replicateM" drain (replicateM streamLen)
648− , benchFold "iterate" drain (iterate streamLen)
649− , benchFold "iterateM" drain (iterateM streamLen)
650−
651− , benchFold "fromFoldable" drain (fromFoldable streamLen)
652− , benchFold "fromFoldableM" drain (fromFoldableM streamLen)
426+ , benchFold "fromFoldable" toNull sourceFromFoldable
427+ , benchFold "fromFoldableM" toNull sourceFromFoldableM
653428
654429 -- appends
655− , benchFold "concatMapFoldableWith" drain (concatMapFoldableSerial streamLen)
656− , benchFold "concatMapFoldableWithM" drain (concatMapFoldableSerialM streamLen)
430+ , benchFold "foldMapWith" toNull sourceFoldMapWith
431+ , benchFold "foldMapWithM" toNull sourceFoldMapWithM
657432 ]
658−
659−o_1_space_elimination :: Int -> Benchmark
660−o_1_space_elimination streamLen =
661− bgroup "elimination"
662− [ benchFold "toNull" drain (unfoldrM streamLen)
663− , benchFold "mapM_" mapM_ (unfoldrM streamLen)
664− , benchFold "uncons" uncons (unfoldrM streamLen)
665− , benchFold "init" init (unfoldrM streamLen)
666− , benchFold "foldl'" foldl' (unfoldrM streamLen)
667− , benchFold "last" last (unfoldrM streamLen)
433+ , bgroup "elimination"
434+ [ benchFold "toNull" toNull sourceUnfoldrM
435+ , benchFold "mapM_" mapM_ sourceUnfoldrM
436+ , benchFold "uncons" uncons sourceUnfoldrM
437+ , benchFold "init" init sourceUnfoldrM
438+ , benchFold "foldl'" foldl sourceUnfoldrM
439+ , benchFold "last" last sourceUnfoldrM
668440 ]
669−
670−o_1_space_nested :: Int -> Benchmark
671−o_1_space_nested streamLen =
672− bgroup "nested"
673− [ benchFold "drainApplicative" drainApplicative (unfoldrM streamLen2)
674− , benchFold "drainMonad" drainMonad (unfoldrM streamLen2)
675− , benchFold "drainMonad3" drainMonad3 (unfoldrM streamLen3)
676− , benchFold "filterAllInMonad" filterAllInMonad (unfoldrM streamLen2)
677− , benchFold "filterAllOutMonad" filterAllOutMonad (unfoldrM streamLen2)
678− , benchFold "drainApplicative (pure)" drainApplicative (unfoldr streamLen2)
679− , benchFold "drainMonad (pure)" drainMonad (unfoldr streamLen2)
680− , benchFold "drainMonad3 (pure)" drainMonad3 (unfoldr streamLen3)
681− , benchFold "filterAllInMonad (pure)" filterAllInMonad (unfoldr streamLen2)
682− , benchFold "filterAllOutMonad (pure)" filterAllOutMonad (unfoldr streamLen2)
441+ , bgroup "nested"
442+ [ benchFold "toNullAp" toNullApNested (sourceUnfoldrMN value2)
443+ , benchFold "toNull" toNullNested (sourceUnfoldrMN value2)
444+ , benchFold "toNull3" toNullNested3 (sourceUnfoldrMN value3)
445+ , benchFold "filterAllIn" filterAllInNested (sourceUnfoldrMN value2)
446+ , benchFold "filterAllOut" filterAllOutNested (sourceUnfoldrMN value2)
447+ , benchFold "toNullApPure" toNullApNested (sourceUnfoldrN value2)
448+ , benchFold "toNullPure" toNullNested (sourceUnfoldrN value2)
449+ , benchFold "toNull3Pure" toNullNested3 (sourceUnfoldrN value3)
450+ , benchFold "filterAllInPure" filterAllInNested (sourceUnfoldrN value2)
451+ , benchFold "filterAllOutPure" filterAllOutNested (sourceUnfoldrN value2)
683452 ]
684− where
685− streamLen2 = round (P.fromIntegral streamLen**(1/2::P.Double)) -- double nested loop
686− streamLen3 = round (P.fromIntegral streamLen**(1/3::P.Double)) -- triple nested loop
687−
688−o_1_space_transformation :: Int -> Benchmark
689−o_1_space_transformation streamLen =
690− bgroup "transformation"
691− [ benchFold "foldrS" (foldrS 1) (unfoldrM streamLen)
692− , benchFold "scanl'" (scanl' 1) (unfoldrM streamLen)
693− , benchFold "map" (map 1) (unfoldrM streamLen)
694− , benchFold "fmap" (fmapK 1) (unfoldrM streamLen)
695− , benchFold "mapM" (mapM 1) (unfoldrM streamLen)
696− , benchFold "mapMSerial" (mapMSerial 1) (unfoldrM streamLen)
453+ , bgroup "transformation"
454+ [ benchFold "foldrS" (foldrS 1) sourceUnfoldrM
455+ , benchFold "scan" (scan 1) sourceUnfoldrM
456+ , benchFold "map" (map 1) sourceUnfoldrM
457+ , benchFold "fmap" (fmapK 1) sourceUnfoldrM
458+ , benchFold "mapM" (mapM 1) sourceUnfoldrM
459+ , benchFold "mapMSerial" (mapMSerial 1) sourceUnfoldrM
460+ -- , benchFoldSrcK "concatMap" concatMap
461+ , benchFold "concatMapNxN" (concatMap 1) (sourceUnfoldrMN value2)
462+ , benchFold "concatMapPureNxN" (concatMap 1) (sourceUnfoldrN value2)
463+ , benchFold "concatMapRepl4xN" concatMapRepl4xN
464+ (sourceUnfoldrMN (value `div` 4))
697465 ]
698−
699−o_1_space_transformationX4 :: Int -> Benchmark
700−o_1_space_transformationX4 streamLen =
701− bgroup "transformationX4"
702− [ benchFold "scanl'" (scanl' 4) (unfoldrM streamLen)
703− , benchFold "map" (map 4) (unfoldrM streamLen)
704− , benchFold "fmap" (fmapK 4) (unfoldrM streamLen)
705− , benchFold "mapM" (mapM 4) (unfoldrM streamLen)
706− , benchFold "mapMSerial" (mapMSerial 4) (unfoldrM streamLen)
466+ , bgroup "transformationX4"
467+ [ benchFold "scan" (scan 4) sourceUnfoldrM
468+ , benchFold "map" (map 4) sourceUnfoldrM
469+ , benchFold "fmap" (fmapK 4) sourceUnfoldrM
470+ , benchFold "mapM" (mapM 4) sourceUnfoldrM
471+ , benchFold "mapMSerial" (mapMSerial 4) sourceUnfoldrM
707472 -- XXX this is horribly slow
708− -- , benchFold "concatMap" (concatMap 4) (unfoldrM streamLen16)
473+ -- , benchFold "concatMap" (concatMap 4) (sourceUnfoldrMN value16)
709474 ]
710−
711−o_1_space_concat :: Int -> Benchmark
712−o_1_space_concat streamLen =
713− bgroup "concat"
714− [ benchIOSrc1 "concatMapPure (n of 1)"
715− (concatMapPure streamLen 1)
716− , benchIOSrc1 "concatMapPure (sqrt n of sqrt n)"
717− (concatMapPure streamLen2 streamLen2)
718− , benchIOSrc1 "concatMapPure (1 of n)"
719− (concatMapPure 1 streamLen)
720−
721− , benchIOSrc1 "concatMap (n of 1)"
722− (concatMap streamLen 1)
723− , benchIOSrc1 "concatMap (sqrt n of sqrt n)"
724− (concatMap streamLen2 streamLen2)
725− , benchIOSrc1 "concatMap (1 of n)"
726− (concatMap 1 streamLen)
727−
728− , benchIOSrc1 "concatMapRepl (sqrt n of sqrt n)"
729− (concatMapRepl streamLen2 streamLen2)
730−
731− -- This is for comparison with concatMapFoldableWith
732− , benchIOSrc1 "concatMapWithId (n of 1) (fromFoldable)"
733− (S.drain
734− . S.concatMapWith S.append id
735− . sourceConcatMapId streamLen)
736−
737− , benchIOSrc1 "concatMapBy serial (n of 1)"
738− (concatMapBySerial streamLen 1)
739− , benchIOSrc1 "concatMapBy serial (sqrt n of sqrt n)"
740− (concatMapBySerial streamLen2 streamLen2)
741− , benchIOSrc1 "concatMapBy serial (1 of n)"
742− (concatMapBySerial 1 streamLen)
743− , benchFold "sortBy" sortBy (unfoldrM streamLen)
475+ , bgroup "filtering"
476+ [ benchFold "filter-even" (filterEven 1) sourceUnfoldrM
477+ , benchFold "filter-all-out" (filterAllOut 1) sourceUnfoldrM
478+ , benchFold "filter-all-in" (filterAllIn 1) sourceUnfoldrM
479+ , benchFold "take-all" (takeAll 1) sourceUnfoldrM
480+ , benchFold "takeWhile-true" (takeWhileTrue 1) sourceUnfoldrM
481+ , benchFold "drop-one" (dropOne 1) sourceUnfoldrM
482+ , benchFold "drop-all" (dropAll 1) sourceUnfoldrM
483+ , benchFold "dropWhile-true" (dropWhileTrue 1) sourceUnfoldrM
484+ , benchFold "dropWhile-false" (dropWhileFalse 1) sourceUnfoldrM
744485 ]
745− where
746− streamLen2 = round (P.fromIntegral streamLen**(1/2::P.Double)) -- double nested loop
747−
748−o_1_space_filtering :: Int -> Benchmark
749−o_1_space_filtering streamLen =
750− bgroup "filtering"
751− [ benchFold "filter-even" (filterEven 1) (unfoldrM streamLen)
752− , benchFold "filter-all-out" (filterAllOut streamLen 1) (unfoldrM streamLen)
753− , benchFold "filter-all-in" (filterAllIn streamLen 1) (unfoldrM streamLen)
754− , benchFold "take-all" (takeAll streamLen 1) (unfoldrM streamLen)
755− , benchFold "takeWhile-true" (takeWhileTrue streamLen 1) (unfoldrM streamLen)
756− , benchFold "drop-one" (dropOne 1) (unfoldrM streamLen)
757− , benchFold "drop-all" (dropAll streamLen 1) (unfoldrM streamLen)
758− , benchFold "dropWhile-true" (dropWhileTrue streamLen 1) (unfoldrM streamLen)
759− , benchFold "dropWhile-false" (dropWhileFalse 1) (unfoldrM streamLen)
486+ , bgroup "filteringX4"
487+ [ benchFold "filter-even" (filterEven 4) sourceUnfoldrM
488+ , benchFold "filter-all-out" (filterAllOut 4) sourceUnfoldrM
489+ , benchFold "filter-all-in" (filterAllIn 4) sourceUnfoldrM
490+ , benchFold "take-all" (takeAll 4) sourceUnfoldrM
491+ , benchFold "takeWhile-true" (takeWhileTrue 4) sourceUnfoldrM
492+ , benchFold "drop-one" (dropOne 4) sourceUnfoldrM
493+ , benchFold "drop-all" (dropAll 4) sourceUnfoldrM
494+ , benchFold "dropWhile-true" (dropWhileTrue 4) sourceUnfoldrM
495+ , benchFold "dropWhile-false" (dropWhileFalse 4) sourceUnfoldrM
760496 ]
761−
762−o_1_space_filteringX4 :: Int -> Benchmark
763−o_1_space_filteringX4 streamLen =
764− bgroup "filteringX4"
765− [ benchFold "filter-even" (filterEven 4) (unfoldrM streamLen)
766− , benchFold "filter-all-out" (filterAllOut streamLen 4) (unfoldrM streamLen)
767− , benchFold "filter-all-in" (filterAllIn streamLen 4) (unfoldrM streamLen)
768− , benchFold "take-all" (takeAll streamLen 4) (unfoldrM streamLen)
769− , benchFold "takeWhile-true" (takeWhileTrue streamLen 4) (unfoldrM streamLen)
770− , benchFold "drop-one" (dropOne 4) (unfoldrM streamLen)
771− , benchFold "drop-all" (dropAll streamLen 4) (unfoldrM streamLen)
772− , benchFold "dropWhile-true" (dropWhileTrue streamLen 4) (unfoldrM streamLen)
773− , benchFold "dropWhile-false" (dropWhileFalse 4) (unfoldrM streamLen)
497+ , bgroup "zipping"
498+ [ benchFold "zip" zip sourceUnfoldrM
774499 ]
775−
776−o_1_space_zipping :: Int -> Benchmark
777−o_1_space_zipping streamLen =
778− bgroup "zipping"
779− [ benchFold "zipWith" zipWith (unfoldrM streamLen)
780− , benchFold "zipWithM" zipWithM (unfoldrM streamLen)
500+ , bgroup "mixed"
501+ [ benchFold "scan-map" (scanMap 1) sourceUnfoldrM
502+ , benchFold "drop-map" (dropMap 1) sourceUnfoldrM
503+ , benchFold "drop-scan" (dropScan 1) sourceUnfoldrM
504+ , benchFold "take-drop" (takeDrop 1) sourceUnfoldrM
505+ , benchFold "take-scan" (takeScan 1) sourceUnfoldrM
506+ , benchFold "take-map" (takeMap 1) sourceUnfoldrM
507+ , benchFold "filter-drop" (filterDrop 1) sourceUnfoldrM
508+ , benchFold "filter-take" (filterTake 1) sourceUnfoldrM
509+ , benchFold "filter-scan" (filterScan 1) sourceUnfoldrM
510+ , benchFold "filter-map" (filterMap 1) sourceUnfoldrM
781511 ]
782−
783−o_1_space_mixed :: Int -> Benchmark
784−o_1_space_mixed streamLen =
785− bgroup "mixed"
786− [ benchFold "scan-map" (scanMap 1) (unfoldrM streamLen)
787− , benchFold "drop-map" (dropMap 1) (unfoldrM streamLen)
788− , benchFold "drop-scan" (dropScan 1) (unfoldrM streamLen)
789− , benchFold "take-drop" (takeDrop streamLen 1) (unfoldrM streamLen)
790− , benchFold "take-scan" (takeScan streamLen 1) (unfoldrM streamLen)
791− , benchFold "take-map" (takeMap streamLen 1) (unfoldrM streamLen)
792− , benchFold "filter-drop" (filterDrop streamLen 1) (unfoldrM streamLen)
793− , benchFold "filter-take" (filterTake streamLen 1) (unfoldrM streamLen)
794− , benchFold "filter-scan" (filterScan 1) (unfoldrM streamLen)
795− , benchFold "filter-map" (filterMap streamLen 1) (unfoldrM streamLen)
512+ , bgroup "mixedX2"
513+ [ benchFold "scan-map" (scanMap 2) sourceUnfoldrM
514+ , benchFold "drop-map" (dropMap 2) sourceUnfoldrM
515+ , benchFold "drop-scan" (dropScan 2) sourceUnfoldrM
516+ , benchFold "take-drop" (takeDrop 2) sourceUnfoldrM
517+ , benchFold "take-scan" (takeScan 2) sourceUnfoldrM
518+ , benchFold "take-map" (takeMap 2) sourceUnfoldrM
519+ , benchFold "filter-drop" (filterDrop 2) sourceUnfoldrM
520+ , benchFold "filter-take" (filterTake 2) sourceUnfoldrM
521+ , benchFold "filter-scan" (filterScan 2) sourceUnfoldrM
522+ , benchFold "filter-map" (filterMap 2) sourceUnfoldrM
796523 ]
797−
798−o_1_space_mixedX2 :: Int -> Benchmark
799−o_1_space_mixedX2 streamLen =
800− bgroup "mixedX2"
801− [ benchFold "scan-map" (scanMap 2) (unfoldrM streamLen)
802− , benchFold "drop-map" (dropMap 2) (unfoldrM streamLen)
803− , benchFold "drop-scan" (dropScan 2) (unfoldrM streamLen)
804− , benchFold "take-drop" (takeDrop streamLen 2) (unfoldrM streamLen)
805− , benchFold "take-scan" (takeScan streamLen 2) (unfoldrM streamLen)
806− , benchFold "take-map" (takeMap streamLen 2) (unfoldrM streamLen)
807− , benchFold "filter-drop" (filterDrop streamLen 2) (unfoldrM streamLen)
808− , benchFold "filter-take" (filterTake streamLen 2) (unfoldrM streamLen)
809− , benchFold "filter-scan" (filterScan 2) (unfoldrM streamLen)
810− , benchFold "filter-map" (filterMap streamLen 2) (unfoldrM streamLen)
524+ , bgroup "mixedX4"
525+ [ benchFold "scan-map" (scanMap 4) sourceUnfoldrM
526+ , benchFold "drop-map" (dropMap 4) sourceUnfoldrM
527+ , benchFold "drop-scan" (dropScan 4) sourceUnfoldrM
528+ , benchFold "take-drop" (takeDrop 4) sourceUnfoldrM
529+ , benchFold "take-scan" (takeScan 4) sourceUnfoldrM
530+ , benchFold "take-map" (takeMap 4) sourceUnfoldrM
531+ , benchFold "filter-drop" (filterDrop 4) sourceUnfoldrM
532+ , benchFold "filter-take" (filterTake 4) sourceUnfoldrM
533+ , benchFold "filter-scan" (filterScan 4) sourceUnfoldrM
534+ , benchFold "filter-map" (filterMap 4) sourceUnfoldrM
811535 ]
812−
813−o_1_space_mixedX4 :: Int -> Benchmark
814−o_1_space_mixedX4 streamLen =
815− bgroup "mixedX4"
816− [ benchFold "scan-map" (scanMap 4) (unfoldrM streamLen)
817− , benchFold "drop-map" (dropMap 4) (unfoldrM streamLen)
818− , benchFold "drop-scan" (dropScan 4) (unfoldrM streamLen)
819− , benchFold "take-drop" (takeDrop streamLen 4) (unfoldrM streamLen)
820− , benchFold "take-scan" (takeScan streamLen 4) (unfoldrM streamLen)
821− , benchFold "take-map" (takeMap streamLen 4) (unfoldrM streamLen)
822− , benchFold "filter-drop" (filterDrop streamLen 4) (unfoldrM streamLen)
823− , benchFold "filter-take" (filterTake streamLen 4) (unfoldrM streamLen)
824− , benchFold "filter-scan" (filterScan 4) (unfoldrM streamLen)
825− , benchFold "filter-map" (filterMap streamLen 4) (unfoldrM streamLen)
826− ]
827−
828−o_1_space_list :: Int -> Benchmark
829−o_1_space_list streamLen =
830− bgroup "list"
831− [ bgroup "elimination"
832− [ benchList "last" (\xs -> [List.last xs]) (unfoldrList streamLen)
833− ]
834− , bgroup "nested"
835− [ benchList "toNullAp" toNullApNestedList (unfoldrList streamLen2)
836− , benchList "toNull" toNullNestedList (unfoldrList streamLen2)
837− , benchList "toNull3" toNullNestedList3 (unfoldrList streamLen3)
838− , benchList "filterAllIn" filterAllInNestedList (unfoldrList streamLen2)
839− , benchList "filterAllOut" filterAllOutNestedList (unfoldrList streamLen2)
840− ]
841− ]
842− where
843−
844− streamLen2 = round (P.fromIntegral streamLen**(1/2::P.Double)) -- double nested loop
845− streamLen3 = round (P.fromIntegral streamLen**(1/3::P.Double)) -- triple nested loop
846−
847−o_1_space :: Int -> Benchmark
848−o_1_space streamLen =
849− bgroup (o_1_space_prefix moduleName)
850− [ o_1_space_generation streamLen
851− , o_1_space_elimination streamLen
852− , o_1_space_nested streamLen
853− , o_1_space_transformation streamLen
854− , o_1_space_transformationX4 streamLen
855− , o_1_space_concat streamLen
856− , o_1_space_filtering streamLen
857− , o_1_space_filteringX4 streamLen
858− , o_1_space_zipping streamLen
859− , o_1_space_mixed streamLen
860− , o_1_space_mixedX2 streamLen
861− , o_1_space_mixedX4 streamLen
862− , o_1_space_list streamLen
863536 ]
537+ ]
864538
865−o_n_heap :: Int -> Benchmark
866−o_n_heap streamLen =
867− bgroup (o_n_heap_prefix moduleName)
539+o_n_heap :: [Benchmark]
540+o_n_heap =
541+ [ bgroup "streamK"
868542 [ bgroup "transformation"
869− [ benchFold "foldlS" (foldlS 1) (unfoldrM streamLen)
543+ [ benchFold "foldlS" (foldlS 1) sourceUnfoldrM
870544 ]
871545 ]
546+ ]
872547
873548{-# INLINE benchK #-}
874549benchK :: P.String -> (Int -> Stream P.IO Int) -> Benchmark
875−benchK name f = bench name $ nfIO $ randomRIO (1,1) >>= drain . f
550+benchK name f = bench name $ nfIO $ randomRIO (1,1) >>= toNull . f
876551
877−o_n_stack :: Int -> Int -> Int -> Benchmark
878−o_n_stack streamLen iterStreamLen maxIters =
879− bgroup (o_n_stack_prefix moduleName)
552+o_n_stack :: [Benchmark]
553+o_n_stack =
554+ [ bgroup "streamK"
880555 [ bgroup "elimination"
881− [ benchFold "tail" tail (unfoldrM streamLen)
882− , benchFold "nullTail" nullTail (unfoldrM streamLen)
883− , benchFold "headTail" headTail (unfoldrM streamLen)
556+ [ benchFold "tail" tail sourceUnfoldrM
557+ , benchFold "nullTail" nullTail sourceUnfoldrM
558+ , benchFold "headTail" headTail sourceUnfoldrM
884559 ]
885560 , bgroup "transformation"
886561 [
887562 -- XXX why do these need so much stack
888− benchFold "intersperse" (intersperse streamLen 1) (unfoldrM streamLen2)
889− , benchFold "interspersePure" (intersperse streamLen 1) (unfoldr streamLen2)
563+ benchFold "intersperse" (intersperse 1) (sourceUnfoldrMN value2)
564+ , benchFold "interspersePure" (intersperse 1) (sourceUnfoldrN value2)
890565 ]
891566 , bgroup "transformationX4"
892567 [
893− benchFold "intersperse" (intersperse streamLen 4) (unfoldrM streamLen16)
568+ benchFold "intersperse" (intersperse 4) (sourceUnfoldrMN value16)
894569 ]
895570 , bgroup "iterated"
896− [ benchK "mapM" (iterateMapM iterStreamLen maxIters)
897− , benchK "scan(1/10)" (iterateScan iterStreamLen maxIters)
898− , benchK "filterEven" (iterateFilterEven iterStreamLen maxIters)
899− , benchK "takeAll" (iterateTakeAll streamLen iterStreamLen maxIters)
900− , benchK "dropOne" (iterateDropOne iterStreamLen maxIters)
901− , benchK "dropWhileFalse(1/10)" (iterateDropWhileFalse streamLen iterStreamLen maxIters)
902− , benchK "dropWhileTrue" (iterateDropWhileTrue streamLen iterStreamLen maxIters)
571+ [ benchK "mapM" iterateMapM
572+ , benchK "scan(1/10)" iterateScan
573+ , benchK "filterEven" iterateFilterEven
574+ , benchK "takeAll" iterateTakeAll
575+ , benchK "dropOne" iterateDropOne
576+ , benchK "dropWhileFalse(1/10)" iterateDropWhileFalse
577+ , benchK "dropWhileTrue" iterateDropWhileTrue
903578 ]
904579 ]
905− where
906− streamLen2 = round (P.fromIntegral streamLen**(1/2::P.Double)) -- double nested loop
907− streamLen16 = round (P.fromIntegral streamLen**(1/16::P.Double)) -- triple nested loop
580+ ]
908581
909−o_n_space :: Int -> Benchmark
910−o_n_space streamLen =
911− bgroup (o_n_space_prefix moduleName)
582+o_n_space :: [Benchmark]
583+o_n_space =
584+ [ bgroup "streamK"
912585 [ bgroup "elimination"
913− [ benchFold "toList" toList (unfoldrM streamLen)
586+ [ benchFold "toList" toList sourceUnfoldrM
914587 ]
915588 ]
589+ ]
916590
917591{-# INLINE benchList #-}
918592benchList :: P.String -> ([Int] -> [Int]) -> (Int -> [Int]) -> Benchmark
919593benchList name run f = bench name $ nfIO $ randomRIO (1,1) >>= return . run . f
920594
921−main :: IO ()
922−main =
923− defaultMain
924− [ o_1_space streamLen
925− , o_n_stack streamLen iterStreamLen maxIters
926− , o_n_heap streamLen
927− , o_n_space streamLen
595+o_1_space_list :: [Benchmark]
596+o_1_space_list =
597+ [ bgroup "list"
598+ [ bgroup "elimination"
599+ [ benchList "last" (\xs -> [List.last xs]) (sourceUnfoldrList value)
928600 ]
929−
930− where
931−
932− streamLen = 100000
933− maxIters = 10000
934− iterStreamLen = 10
601+ , bgroup "nested"
602+ [ benchList "toNullAp" toNullApNestedList (sourceUnfoldrList value2)
603+ , benchList "toNull" toNullNestedList (sourceUnfoldrList value2)
604+ , benchList "toNull3" toNullNestedList3 (sourceUnfoldrList value3)
605+ , benchList "filterAllIn" filterAllInNestedList (sourceUnfoldrList value2)
606+ , benchList "filterAllOut" filterAllOutNestedList (sourceUnfoldrList value2)
607+ ]
608+ ]
609+ ]