@@ -8,6 +8,7 @@
88{-# LANGUAGE CPP #-}
99{-# LANGUAGE FlexibleContexts #-}
1010{-# LANGUAGE ScopedTypeVariables #-}
11+{-# OPTIONS_GHC -Wno-orphans #-}
1112
1213#ifdef __HADDOCK_VERSION__
1314#undef INSPECTION
@@ -20,19 +21,23 @@
2021
2122module Main (main) where
2223
24+#if !MIN_VERSION_base(4,18,0)
25+import Control.Applicative (liftA2)
26+#endif
2327import Control.Monad (when)
2428import Data.Maybe (isJust)
2529import System.Random (randomRIO)
2630import Prelude hiding
27− (tail, mapM_, foldl, last, map, mapM, concatMap, zip, init)
31+ ( Foldable(..), tail, mapM_, last, map, mapM, concatMap, zipWith, init, iterate
32+ , repeat, replicate
33+ )
2834
2935import qualified Prelude as P
3036import qualified Data.List as List
3137
32−import qualified Streamly.Internal.Data.Stream.StreamK as S
33−import qualified Streamly.Internal.Data.SVar as S
38+import qualified Streamly.Internal.Data.StreamK as S
3439
35−import Gauge (bench, nfIO, bgroup, Benchmark, defaultMain)
40+import Test.Tasty.Bench (bench, nfIO, bgroup, Benchmark, defaultMain)
3641
3742import Streamly.Benchmark.Common
3843
@@ -40,70 +45,161 @@ import Streamly.Benchmark.Common
4045import Test.Inspection
4146#endif
4247
48+{-
49+
50+Benchmarks that need to be added
51+
52+-- fromList
53+
54+-- bindWith
55+-- concatPairsWith
56+-- apWith
57+-- apSerial
58+-- apSerialDiscardFst
59+-- apSerialDiscardSnd
60+
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
111+
112+-- scanlx'
113+
114+-}
115+
43116-------------------------------------------------------------------------------
44117-- Stream generation and elimination
45118-------------------------------------------------------------------------------
46119
47−type Stream m a = S.Stream m a
120+type Stream m a = S.StreamK m a
48121
49−{-# INLINE sourceUnfoldr #-}
50−sourceUnfoldr :: Int -> Int -> Stream m Int
51−sourceUnfoldr value n = S.unfoldr step n
122+{-# INLINE unfoldr #-}
123+unfoldr :: Int -> Int -> Stream m Int
124+unfoldr streamLen n = S.unfoldr step n
52125 where
53126 step cnt =
54− if cnt > n + value
127+ if cnt > n + streamLen
55128 then Nothing
56129 else Just (cnt, cnt + 1)
57130
58−{-# INLINE sourceUnfoldrM #-}
59−sourceUnfoldrM :: S.MonadAsync m => Int -> Int -> Stream m Int
60−sourceUnfoldrM value n = S.unfoldrM step n
131+{-# INLINE unfoldrM #-}
132+unfoldrM :: Monad m => Int -> Int -> Stream m Int
133+unfoldrM streamLen n = S.unfoldrMWith S.consM step n
61134 where
62135 step cnt =
63− if cnt > n + value
136+ if cnt > n + streamLen
64137 then return Nothing
65138 else return (Just (cnt, cnt + 1))
66139
67−{-# INLINE sourceFromFoldable #-}
68−sourceFromFoldable :: Int -> Int -> Stream m Int
69−sourceFromFoldable value n = S.fromFoldable [n..n+value]
140+{-# INLINE repeat #-}
141+repeat :: Int -> Int -> Stream m Int
142+repeat streamLen = S.take streamLen . S.repeat
70143
71−{-# INLINE sourceFromFoldableM #-}
72−sourceFromFoldableM :: S.MonadAsync m => Int -> Int -> Stream m Int
73−sourceFromFoldableM value n =
74− Prelude.foldr S.consM S.nil (Prelude.fmap return [n..n+value])
144+{-# INLINE repeatM #-}
145+repeatM :: Monad m => Int -> Int -> Stream m Int
146+repeatM streamLen = S.take streamLen . S.repeatM . return
75147
76−{-# INLINABLE concatMapFoldableWith #-}
77−concatMapFoldableWith :: (S.IsStream t, Foldable f)
78− => (t m b -> t m b -> t m b) -> (a -> t m b) -> f a -> t m b
79−concatMapFoldableWith f g = Prelude.foldr (f . g) S.nil
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)
80159
81−{-# INLINE sourceFoldMapWith #-}
82−sourceFoldMapWith :: Int -> Int -> Stream m Int
83−sourceFoldMapWith value n = concatMapFoldableWith S.serial S.yield [n..n+value]
160+{-# INLINE iterateM #-}
161+iterateM :: Monad m => Int -> Int -> Stream m Int
162+iterateM streamLen = S.take streamLen . S.iterateM (return . (+1)) . return
84163
85−{-# INLINE sourceFoldMapWithM #-}
86−sourceFoldMapWithM :: Monad m => Int -> Int -> Stream m Int
87−sourceFoldMapWithM value n =
88− concatMapFoldableWith S.serial (S.yieldM . return) [n..n+value]
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]
89190
90191-------------------------------------------------------------------------------
91192-- Elimination
92193-------------------------------------------------------------------------------
93194
94−{-# INLINE runStream #-}
95−runStream :: Monad m => Stream m a -> m ()
96−runStream = S.drain
97−-- runStream = S.mapM_ (\_ -> return ())
195+{-# INLINE drain #-}
196+drain :: Monad m => Stream m a -> m ()
197+drain = S.drain
98198
99199{-# INLINE mapM_ #-}
100200mapM_ :: Monad m => Stream m a -> m ()
101201mapM_ = S.mapM_ (\_ -> return ())
102202
103−{-# INLINE toNull #-}
104−toNull :: Monad m => Stream m Int -> m ()
105−toNull = runStream
106−
107203{-# INLINE uncons #-}
108204uncons :: Monad m => Stream m Int -> m ()
109205uncons s = do
@@ -113,13 +209,13 @@ uncons s = do
113209 Just (_, t) -> uncons t
114210
115211{-# INLINE init #-}
116−init :: (Monad m, S.IsStream t) => t m a -> m ()
212+init :: Monad m => Stream m a -> m ()
117213init s = do
118214 t <- S.init s
119215 P.mapM_ S.drain t
120216
121217{-# INLINE tail #-}
122−tail :: (Monad m, S.IsStream t) => t m a -> m ()
218+tail :: Monad m => Stream m a -> m ()
123219tail s = S.tail s >>= P.mapM_ tail
124220
125221{-# INLINE nullTail #-}
@@ -138,9 +234,9 @@ headTail s = do
138234toList :: Monad m => Stream m Int -> m [Int]
139235toList = S.toList
140236
141−{-# INLINE foldl #-}
142−foldl :: Monad m => Stream m Int -> m Int
143−foldl = S.foldl' (+) 0
237+{-# INLINE foldl' #-}
238+foldl' :: Monad m => Stream m Int -> m Int
239+foldl' = S.foldl' (+) 0
144240
145241{-# INLINE last #-}
146242last :: Monad m => Stream m Int -> m (Maybe Int)
@@ -150,40 +246,36 @@ last = S.last
150246-- Transformation
151247-------------------------------------------------------------------------------
152248
153−{-# INLINE transform #-}
154−transform :: Monad m => Stream m a -> m ()
155−transform = runStream
156−
157249{-# INLINE composeN #-}
158250composeN
159251 :: Monad m
160252 => Int -> (Stream m Int -> Stream m Int) -> Stream m Int -> m ()
161253composeN n f =
162254 case n of
163− 1 -> transform . f
164− 2 -> transform . f . f
165− 3 -> transform . f . f . f
166− 4 -> transform . f . f . f . f
255+ 1 -> drain . f
256+ 2 -> drain . f . f
257+ 3 -> drain . f . f . f
258+ 4 -> drain . f . f . f . f
167259 _ -> undefined
168260
169−{-# INLINE scan #-}
170−scan :: Monad m => Int -> Stream m Int -> m ()
171−scan n = composeN n $ S.scanl' (+) 0
261+{-# INLINE scanl' #-}
262+scanl' :: Monad m => Int -> Stream m Int -> m ()
263+scanl' n = composeN n $ S.scanl' (+) 0
172264
173265{-# INLINE map #-}
174266map :: Monad m => Int -> Stream m Int -> m ()
175−map n = composeN n $ P.fmap (+ 1)
267+map n = composeN n $ S.map (+ 1)
176268
177269{-# INLINE fmapK #-}
178270fmapK :: Monad m => Int -> Stream m Int -> m ()
179271fmapK n = composeN n $ P.fmap (+ 1)
180272
181273{-# INLINE mapM #-}
182−mapM :: S.MonadAsync m => Int -> Stream m Int -> m ()
183−mapM n = composeN n $ S.mapM return
274+mapM :: Monad m => Int -> Stream m Int -> m ()
275+mapM n = composeN n $ S.mapMWith S.consM return
184276
185277{-# INLINE mapMSerial #-}
186−mapMSerial :: S.MonadAsync m => Int -> Stream m Int -> m ()
278+mapMSerial :: Monad m => Int -> Stream m Int -> m ()
187279mapMSerial n = composeN n $ S.mapMSerial return
188280
189281{-# INLINE filterEven #-}
@@ -192,11 +284,11 @@ filterEven n = composeN n $ S.filter even
192284
193285{-# INLINE filterAllOut #-}
194286filterAllOut :: Monad m => Int -> Int -> Stream m Int -> m ()
195−filterAllOut maxValue n = composeN n $ S.filter (> maxValue)
287+filterAllOut streamLen n = composeN n $ S.filter (> streamLen)
196288
197289{-# INLINE filterAllIn #-}
198290filterAllIn :: Monad m => Int -> Int -> Stream m Int -> m ()
199−filterAllIn maxValue n = composeN n $ S.filter (<= maxValue)
291+filterAllIn streamLen n = composeN n $ S.filter (<= streamLen)
200292
201293{-# INLINE _takeOne #-}
202294_takeOne :: Monad m => Int -> Stream m Int -> m ()
@@ -204,11 +296,11 @@ _takeOne n = composeN n $ S.take 1
204296
205297{-# INLINE takeAll #-}
206298takeAll :: Monad m => Int -> Int -> Stream m Int -> m ()
207−takeAll maxValue n = composeN n $ S.take maxValue
299+takeAll streamLen n = composeN n $ S.take streamLen
208300
209301{-# INLINE takeWhileTrue #-}
210302takeWhileTrue :: Monad m => Int -> Int -> Stream m Int -> m ()
211−takeWhileTrue maxValue n = composeN n $ S.takeWhile (<= maxValue)
303+takeWhileTrue streamLen n = composeN n $ S.takeWhile (<= streamLen)
212304
213305{-# INLINE dropOne #-}
214306dropOne :: Monad m => Int -> Stream m Int -> m ()
@@ -216,11 +308,11 @@ dropOne n = composeN n $ S.drop 1
216308
217309{-# INLINE dropAll #-}
218310dropAll :: Monad m => Int -> Int -> Stream m Int -> m ()
219−dropAll maxValue n = composeN n $ S.drop maxValue
311+dropAll streamLen n = composeN n $ S.drop streamLen
220312
221313{-# INLINE dropWhileTrue #-}
222314dropWhileTrue :: Monad m => Int -> Int -> Stream m Int -> m ()
223−dropWhileTrue maxValue n = composeN n $ S.dropWhile (<= maxValue)
315+dropWhileTrue streamLen n = composeN n $ S.dropWhile (<= streamLen)
224316
225317{-# INLINE dropWhileFalse #-}
226318dropWhileFalse :: Monad m => Int -> Stream m Int -> m ()
@@ -235,8 +327,8 @@ foldlS :: Monad m => Int -> Stream m Int -> m ()
235327foldlS n = composeN n $ S.foldlS (flip S.cons) S.nil
236328
237329{-# INLINE intersperse #-}
238−intersperse :: S.MonadAsync m => Int -> Int -> Stream m Int -> m ()
239−intersperse maxValue n = composeN n $ S.intersperse maxValue
330+intersperse :: Monad m => Int -> Int -> Stream m Int -> m ()
331+intersperse streamLen n = composeN n $ S.intersperse streamLen
240332
241333-------------------------------------------------------------------------------
242334-- Iteration
@@ -244,51 +336,67 @@ intersperse maxValue n = composeN n $ S.intersperse maxValue
244336
245337{-# INLINE iterateSource #-}
246338iterateSource
247− :: S.MonadAsync m
248− => Int -> (Stream m Int -> Stream m Int) -> Int -> Int -> Stream m Int
249−iterateSource iterStreamLen g i n = f i (sourceUnfoldrM iterStreamLen n)
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)
250341 where
251342 f (0 :: Int) m = g m
252343 f x m = g (f (x P.- 1) m)
253344
254345-- this is quadratic
255346{-# INLINE iterateScan #-}
256−iterateScan :: S.MonadAsync m => Int -> Int -> Int -> Stream m Int
257−iterateScan iterStreamLen maxIters = iterateSource iterStreamLen (S.scanl' (+) 0) (maxIters `div` 10)
347+iterateScan :: Monad m => Int -> Int -> Int -> Stream m Int
348+iterateScan iterStreamLen maxIters =
349+ iterateSource iterStreamLen (S.scanl' (+) 0) (maxIters `div` 10)
258350
259351-- this is quadratic
260352{-# INLINE iterateDropWhileFalse #-}
261−iterateDropWhileFalse :: S.MonadAsync m => Int -> Int -> Int -> Int -> Stream m Int
262−iterateDropWhileFalse maxValue iterStreamLen maxIters =
263− iterateSource iterStreamLen (S.dropWhile (> maxValue)) (maxIters `div` 10)
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)
264356
265357{-# INLINE iterateMapM #-}
266−iterateMapM :: S.MonadAsync m => Int -> Int -> Int -> Stream m Int
267−iterateMapM iterStreamLen maxIters = iterateSource iterStreamLen (S.mapM return) maxIters
358+iterateMapM :: Monad m => Int -> Int -> Int -> Stream m Int
359+iterateMapM iterStreamLen =
360+ iterateSource iterStreamLen (S.mapMWith S.consM return)
268361
269362{-# INLINE iterateFilterEven #-}
270−iterateFilterEven :: S.MonadAsync m => Int -> Int -> Int -> Stream m Int
271−iterateFilterEven iterStreamLen maxIters = iterateSource iterStreamLen (S.filter even) maxIters
363+iterateFilterEven :: Monad m => Int -> Int -> Int -> Stream m Int
364+iterateFilterEven iterStreamLen = iterateSource iterStreamLen (S.filter even)
272365
273366{-# INLINE iterateTakeAll #-}
274−iterateTakeAll :: S.MonadAsync m => Int -> Int -> Int -> Int -> Stream m Int
275−iterateTakeAll maxValue iterStreamLen maxIters = iterateSource iterStreamLen (S.take maxValue) maxIters
367+iterateTakeAll :: Monad m => Int -> Int -> Int -> Int -> Stream m Int
368+iterateTakeAll streamLen iterStreamLen =
369+ iterateSource iterStreamLen (S.take streamLen)
276370
277371{-# INLINE iterateDropOne #-}
278−iterateDropOne :: S.MonadAsync m => Int -> Int -> Int -> Stream m Int
279−iterateDropOne iterStreamLen maxIters = iterateSource iterStreamLen (S.drop 1) maxIters
372+iterateDropOne :: Monad m => Int -> Int -> Int -> Stream m Int
373+iterateDropOne iterStreamLen = iterateSource iterStreamLen (S.drop 1)
280374
281375{-# INLINE iterateDropWhileTrue #-}
282−iterateDropWhileTrue :: S.MonadAsync m => Int -> Int -> Int -> Int -> Stream m Int
283−iterateDropWhileTrue maxValue iterStreamLen maxIters = iterateSource iterStreamLen (S.dropWhile (<= maxValue)) maxIters
376+iterateDropWhileTrue ::
377+ Monad m => Int -> Int -> Int -> Int -> Stream m Int
378+iterateDropWhileTrue streamLen iterStreamLen =
379+ iterateSource iterStreamLen (S.dropWhile (<= streamLen))
284380
285381-------------------------------------------------------------------------------
286382-- Zipping
287383-------------------------------------------------------------------------------
288384
289−{-# INLINE zip #-}
290−zip :: Monad m => Stream m Int -> m ()
291−zip src = transform $ S.zipWith (,) src src
385+{-# INLINE zipWith #-}
386+zipWith :: Monad m => Stream m Int -> m ()
387+zipWith src = drain $ S.zipWith (,) src src
388+
389+{-# INLINE zipWithM #-}
390+zipWithM :: Monad m => Stream m Int -> m ()
391+zipWithM src = drain $ S.zipWithM (curry return) src src
392+
393+{-# INLINE sortByK #-}
394+sortByK :: (a -> a -> Ordering) -> Stream m a -> Stream m a
395+sortByK f = S.mergeMapWith (S.mergeBy f) S.fromPure
396+
397+{-# INLINE sortBy #-}
398+sortBy :: Monad m => Stream m Int -> m ()
399+sortBy = drain . sortByK compare
292400
293401-------------------------------------------------------------------------------
294402-- Mixed Composition
@@ -308,32 +416,31 @@ dropScan n = composeN n $ S.scanl' (+) 0 . S.drop 1
308416
309417{-# INLINE takeDrop #-}
310418takeDrop :: Monad m => Int -> Int -> Stream m Int -> m ()
311−takeDrop maxValue n = composeN n $ S.drop 1 . S.take maxValue
419+takeDrop streamLen n = composeN n $ S.drop 1 . S.take streamLen
312420
313421{-# INLINE takeScan #-}
314422takeScan :: Monad m => Int -> Int -> Stream m Int -> m ()
315−takeScan maxValue n = composeN n $ S.scanl' (+) 0 . S.take maxValue
423+takeScan streamLen n = composeN n $ S.scanl' (+) 0 . S.take streamLen
316424
317425{-# INLINE takeMap #-}
318426takeMap :: Monad m => Int -> Int -> Stream m Int -> m ()
319−takeMap maxValue n = composeN n $ S.map (subtract 1) . S.take maxValue
427+takeMap streamLen n = composeN n $ S.map (subtract 1) . S.take streamLen
320428
321429{-# INLINE filterDrop #-}
322430filterDrop :: Monad m => Int -> Int -> Stream m Int -> m ()
323−filterDrop maxValue n = composeN n $ S.drop 1 . S.filter (<= maxValue)
431+filterDrop streamLen n = composeN n $ S.drop 1 . S.filter (<= streamLen)
324432
325433{-# INLINE filterTake #-}
326434filterTake :: Monad m => Int -> Int -> Stream m Int -> m ()
327−filterTake maxValue n = composeN n $ S.take maxValue . S.filter (<= maxValue)
435+filterTake streamLen n = composeN n $ S.take streamLen . S.filter (<= streamLen)
328436
329−-- Initially, this used maxBound not maxValue
330437{-# INLINE filterScan #-}
331−filterScan :: Monad m => Int -> Int -> Stream m Int -> m ()
332−filterScan maxValue n = composeN n $ S.scanl' (+) 0 . S.filter (<= maxValue)
438+filterScan :: Monad m => Int -> Stream m Int -> m ()
439+filterScan n = composeN n $ S.scanl' (+) 0 . S.filter (<= maxBound)
333440
334441{-# INLINE filterMap #-}
335442filterMap :: Monad m => Int -> Int -> Stream m Int -> m ()
336−filterMap maxValue n = composeN n $ S.map (subtract 1) . S.filter (<= maxValue)
443+filterMap streamLen n = composeN n $ S.map (subtract 1) . S.filter (<= streamLen)
337444
338445-------------------------------------------------------------------------------
339446-- ConcatMap
@@ -345,8 +452,8 @@ filterMap maxValue n = composeN n $ S.map (subtract 1) . S.filter (<= maxValue)
345452concatMap :: Int -> Int -> Int -> IO ()
346453concatMap outer inner n =
347454 S.drain $ S.concatMap
348− (\_ -> sourceUnfoldrM inner n)
349− (sourceUnfoldrM outer n)
455+ (\_ -> unfoldrM inner n)
456+ (unfoldrM outer n)
350457
351458#ifdef INSPECTION
352459inspect $ hasNoTypeClasses 'concatMap
@@ -358,8 +465,8 @@ inspect $ hasNoTypeClasses 'concatMap
358465concatMapPure :: Int -> Int -> Int -> IO ()
359466concatMapPure outer inner n =
360467 S.drain $ S.concatMap
361− (\_ -> sourceUnfoldr inner n)
362− (sourceUnfoldr outer n)
468+ (\_ -> unfoldr inner n)
469+ (unfoldr outer n)
363470
364471#ifdef INSPECTION
365472inspect $ hasNoTypeClasses 'concatMapPure
@@ -370,7 +477,7 @@ inspect $ hasNoTypeClasses 'concatMapPure
370477{-# INLINE concatMapRepl #-}
371478concatMapRepl :: Int -> Int -> Int -> IO ()
372479concatMapRepl outer inner n =
373− S.drain $ S.concatMap (S.replicate inner) (sourceUnfoldrM outer n)
480+ S.drain $ S.concatMap (S.replicate inner) (unfoldrM outer n)
374481
375482#ifdef INSPECTION
376483inspect $ hasNoTypeClasses 'concatMapRepl
@@ -382,44 +489,70 @@ inspect $ hasNoTypeClasses 'concatMapRepl
382489sourceConcatMapId :: Monad m
383490 => Int -> Int -> Stream m (Stream m Int)
384491sourceConcatMapId val n =
385− S.fromFoldable $ fmap (S.yieldM . return) [n..n+val]
492+ S.fromFoldable $ fmap (S.fromEffect . return) [n..n+val]
386493
387−{-# INLINE concatStreamsWith #-}
388−concatStreamsWith :: Int -> Int -> Int -> IO ()
389−concatStreamsWith outer inner n =
390− S.drain $ S.concatMapBy S.serial
391− (sourceUnfoldrM inner)
392− (sourceUnfoldrM outer n)
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)
393500
394501-------------------------------------------------------------------------------
395502-- Nested Composition
396503-------------------------------------------------------------------------------
397504
398−{-# INLINE toNullApNested #-}
399−toNullApNested :: Monad m => Stream m Int -> m ()
400−toNullApNested s = runStream $ do
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
401534 (+) <$> s <*> s
402535
403−{-# INLINE toNullNested #-}
404−toNullNested :: Monad m => Stream m Int -> m ()
405−toNullNested s = runStream $ do
536+{-# INLINE drainMonad #-}
537+drainMonad :: Monad m => Stream m Int -> m ()
538+drainMonad s = drain $ do
406539 x <- s
407540 y <- s
408541 return $ x + y
409542
410−{-# INLINE toNullNested3 #-}
411−toNullNested3 :: Monad m => Stream m Int -> m ()
412−toNullNested3 s = runStream $ do
543+{-# INLINE drainMonad3 #-}
544+drainMonad3 :: Monad m => Stream m Int -> m ()
545+drainMonad3 s = drain $ do
413546 x <- s
414547 y <- s
415548 z <- s
416549 return $ x + y + z
417550
418−{-# INLINE filterAllOutNested #-}
419−filterAllOutNested
551+{-# INLINE filterAllOutMonad #-}
552+filterAllOutMonad
420553 :: Monad m
421554 => Stream m Int -> m ()
422−filterAllOutNested str = runStream $ do
555+filterAllOutMonad str = drain $ do
423556 x <- str
424557 y <- str
425558 let s = x + y
@@ -427,11 +560,11 @@ filterAllOutNested str = runStream $ do
427560 then return s
428561 else S.nil
429562
430−{-# INLINE filterAllInNested #-}
431−filterAllInNested
563+{-# INLINE filterAllInMonad #-}
564+filterAllInMonad
432565 :: Monad m
433566 => Stream m Int -> m ()
434−filterAllInNested str = runStream $ do
567+filterAllInMonad str = drain $ do
435568 x <- str
436569 y <- str
437570 let s = x + y
@@ -443,9 +576,14 @@ filterAllInNested str = runStream $ do
443576-- Nested Composition Pure lists
444577-------------------------------------------------------------------------------
445578
446−{-# INLINE sourceUnfoldrList #-}
447−sourceUnfoldrList :: Int -> Int -> [Int]
448−sourceUnfoldrList maxval n = List.unfoldr step n
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
449587 where
450588 step cnt =
451589 if cnt > n + maxval
@@ -496,271 +634,283 @@ filterAllInNestedList str = do
496634-------------------------------------------------------------------------------
497635
498636moduleName :: String
499−moduleName = "Data.Stream.StreamK"
637+moduleName = "Data.StreamK"
500638
501639o_1_space_generation :: Int -> Benchmark
502−o_1_space_generation value =
640+o_1_space_generation streamLen =
503641 bgroup "generation"
504− [ benchFold "unfoldr" toNull (sourceUnfoldr value)
505− , benchFold "unfoldrM" toNull (sourceUnfoldrM value)
506−
507− , benchFold "fromFoldable" toNull (sourceFromFoldable value)
508− , benchFold "fromFoldableM" toNull (sourceFromFoldableM value)
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)
509653
510654 -- appends
511− , benchFold "concatMapFoldableWith" toNull (sourceFoldMapWith value)
512− , benchFold "concatMapFoldableWithM" toNull (sourceFoldMapWithM value)
655+ , benchFold "concatMapFoldableWith" drain (concatMapFoldableSerial streamLen)
656+ , benchFold "concatMapFoldableWithM" drain (concatMapFoldableSerialM streamLen)
513657 ]
514658
515659o_1_space_elimination :: Int -> Benchmark
516−o_1_space_elimination value =
660+o_1_space_elimination streamLen =
517661 bgroup "elimination"
518− [ benchFold "toNull" toNull (sourceUnfoldrM value)
519− , benchFold "mapM_" mapM_ (sourceUnfoldrM value)
520− , benchFold "uncons" uncons (sourceUnfoldrM value)
521− , benchFold "init" init (sourceUnfoldrM value)
522− , benchFold "foldl'" foldl (sourceUnfoldrM value)
523− , benchFold "last" last (sourceUnfoldrM value)
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)
524668 ]
525669
526670o_1_space_nested :: Int -> Benchmark
527−o_1_space_nested value =
671+o_1_space_nested streamLen =
528672 bgroup "nested"
529− [ benchFold "toNullAp" toNullApNested (sourceUnfoldrM value2)
530− , benchFold "toNull" toNullNested (sourceUnfoldrM value2)
531− , benchFold "toNull3" toNullNested3 (sourceUnfoldrM value3)
532− , benchFold "filterAllIn" filterAllInNested (sourceUnfoldrM value2)
533− , benchFold "filterAllOut" filterAllOutNested (sourceUnfoldrM value2)
534− , benchFold "toNullApPure" toNullApNested (sourceUnfoldr value2)
535− , benchFold "toNullPure" toNullNested (sourceUnfoldr value2)
536− , benchFold "toNull3Pure" toNullNested3 (sourceUnfoldr value3)
537− , benchFold "filterAllInPure" filterAllInNested (sourceUnfoldr value2)
538− , benchFold "filterAllOutPure" filterAllOutNested (sourceUnfoldr value2)
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)
539683 ]
540684 where
541− value2 = round (P.fromIntegral value**(1/2::P.Double)) -- double nested loop
542− value3 = round (P.fromIntegral value**(1/3::P.Double)) -- triple nested loop
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
543687
544688o_1_space_transformation :: Int -> Benchmark
545−o_1_space_transformation value =
689+o_1_space_transformation streamLen =
546690 bgroup "transformation"
547− [ bgroup "X1"
548− [ benchFold "foldrS" (foldrS 1) (sourceUnfoldrM value)
549− , benchFold "scan" (scan 1) (sourceUnfoldrM value)
550− , benchFold "map" (map 1) (sourceUnfoldrM value)
551− , benchFold "fmap" (fmapK 1) (sourceUnfoldrM value)
552− , benchFold "mapM" (mapM 1) (sourceUnfoldrM value)
553− , benchFold "mapMSerial" (mapMSerial 1) (sourceUnfoldrM value)
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)
554697 ]
555− , bgroup "X4"
556− [ benchFold "scan" (scan 4) (sourceUnfoldrM value)
557− , benchFold "map" (map 4) (sourceUnfoldrM value)
558− , benchFold "fmap" (fmapK 4) (sourceUnfoldrM value)
559− , benchFold "mapM" (mapM 4) (sourceUnfoldrM value)
560− , benchFold "mapMSerial" (mapMSerial 4) (sourceUnfoldrM value)
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)
561707 -- XXX this is horribly slow
562− -- , benchFold "concatMap" (concatMap 4) (sourceUnfoldrM value16)
708+ -- , benchFold "concatMap" (concatMap 4) (unfoldrM streamLen16)
563709 ]
564− ]
565710
566711o_1_space_concat :: Int -> Benchmark
567−o_1_space_concat value =
712+o_1_space_concat streamLen =
568713 bgroup "concat"
569714 [ benchIOSrc1 "concatMapPure (n of 1)"
570− (concatMapPure value 1)
715+ (concatMapPure streamLen 1)
571716 , benchIOSrc1 "concatMapPure (sqrt n of sqrt n)"
572− (concatMapPure value2 value2)
717+ (concatMapPure streamLen2 streamLen2)
573718 , benchIOSrc1 "concatMapPure (1 of n)"
574− (concatMapPure 1 value)
719+ (concatMapPure 1 streamLen)
575720
576721 , benchIOSrc1 "concatMap (n of 1)"
577− (concatMap value 1)
722+ (concatMap streamLen 1)
578723 , benchIOSrc1 "concatMap (sqrt n of sqrt n)"
579− (concatMap value2 value2)
724+ (concatMap streamLen2 streamLen2)
580725 , benchIOSrc1 "concatMap (1 of n)"
581− (concatMap 1 value)
726+ (concatMap 1 streamLen)
582727
583728 , benchIOSrc1 "concatMapRepl (sqrt n of sqrt n)"
584− (concatMapRepl value2 value2)
729+ (concatMapRepl streamLen2 streamLen2)
585730
586731 -- This is for comparison with concatMapFoldableWith
587732 , benchIOSrc1 "concatMapWithId (n of 1) (fromFoldable)"
588− (S.drain . S.concatMapBy S.serial id . sourceConcatMapId value)
589−
590− , benchIOSrc1 "concatMapWith (n of 1)"
591− (concatStreamsWith value 1)
592− , benchIOSrc1 "concatMapWith (sqrt n of sqrt n)"
593− (concatStreamsWith value2 value2)
594− , benchIOSrc1 "concatMapWith (1 of n)"
595− (concatStreamsWith 1 value)
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)
596744 ]
597745 where
598− value2 = round (P.fromIntegral value**(1/2::P.Double)) -- double nested loop
746+ streamLen2 = round (P.fromIntegral streamLen**(1/2::P.Double)) -- double nested loop
599747
600748o_1_space_filtering :: Int -> Benchmark
601−o_1_space_filtering value =
749+o_1_space_filtering streamLen =
602750 bgroup "filtering"
603− [ bgroup "X1"
604− [ benchFold "filter-even" (filterEven 1) (sourceUnfoldrM value)
605− , benchFold "filter-all-out" (filterAllOut maxValue 1) (sourceUnfoldrM value)
606− , benchFold "filter-all-in" (filterAllIn maxValue 1) (sourceUnfoldrM value)
607− , benchFold "take-all" (takeAll maxValue 1) (sourceUnfoldrM value)
608− , benchFold "takeWhile-true" (takeWhileTrue maxValue 1) (sourceUnfoldrM value)
609− , benchFold "drop-one" (dropOne 1) (sourceUnfoldrM value)
610− , benchFold "drop-all" (dropAll maxValue 1) (sourceUnfoldrM value)
611− , benchFold "dropWhile-true" (dropWhileTrue maxValue 1) (sourceUnfoldrM value)
612− , benchFold "dropWhile-false" (dropWhileFalse 1) (sourceUnfoldrM value)
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)
613760 ]
614− , bgroup "X4"
615− [ benchFold "filter-even" (filterEven 4) (sourceUnfoldrM value)
616− , benchFold "filter-all-out" (filterAllOut maxValue 4) (sourceUnfoldrM value)
617− , benchFold "filter-all-in" (filterAllIn maxValue 4) (sourceUnfoldrM value)
618− , benchFold "take-all" (takeAll maxValue 4) (sourceUnfoldrM value)
619− , benchFold "takeWhile-true" (takeWhileTrue maxValue 4) (sourceUnfoldrM value)
620− , benchFold "drop-one" (dropOne 4) (sourceUnfoldrM value)
621− , benchFold "drop-all" (dropAll maxValue 4) (sourceUnfoldrM value)
622− , benchFold "dropWhile-true" (dropWhileTrue maxValue 4) (sourceUnfoldrM value)
623− , benchFold "dropWhile-false" (dropWhileFalse 4) (sourceUnfoldrM value)
624− ]
625− ]
626− where
627−
628− maxValue = value
629761
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)
774+ ]
630775
631776o_1_space_zipping :: Int -> Benchmark
632−o_1_space_zipping value =
777+o_1_space_zipping streamLen =
633778 bgroup "zipping"
634− [ benchFold "zip" zip (sourceUnfoldrM value)
779+ [ benchFold "zipWith" zipWith (unfoldrM streamLen)
780+ , benchFold "zipWithM" zipWithM (unfoldrM streamLen)
635781 ]
636782
637783o_1_space_mixed :: Int -> Benchmark
638−o_1_space_mixed value =
784+o_1_space_mixed streamLen =
639785 bgroup "mixed"
640− [ bgroup "X1"
641− [ benchFold "scan-map" (scanMap 1) (sourceUnfoldrM value)
642− , benchFold "drop-map" (dropMap 1) (sourceUnfoldrM value)
643− , benchFold "drop-scan" (dropScan 1) (sourceUnfoldrM value)
644− , benchFold "take-drop" (takeDrop maxValue 1) (sourceUnfoldrM value)
645− , benchFold "take-scan" (takeScan maxValue 1) (sourceUnfoldrM value)
646− , benchFold "take-map" (takeMap maxValue 1) (sourceUnfoldrM value)
647− , benchFold "filter-drop" (filterDrop maxValue 1) (sourceUnfoldrM value)
648− , benchFold "filter-take" (filterTake maxValue 1) (sourceUnfoldrM value)
649− , benchFold "filter-scan" (filterScan maxValue 1) (sourceUnfoldrM value)
650− , benchFold "filter-map" (filterMap maxValue 1) (sourceUnfoldrM value)
651− ]
652− , bgroup "X2"
653− [ benchFold "scan-map" (scanMap 2) (sourceUnfoldrM value)
654− , benchFold "drop-map" (dropMap 2) (sourceUnfoldrM value)
655− , benchFold "drop-scan" (dropScan 2) (sourceUnfoldrM value)
656− , benchFold "take-drop" (takeDrop maxValue 2) (sourceUnfoldrM value)
657− , benchFold "take-scan" (takeScan maxValue 2) (sourceUnfoldrM value)
658− , benchFold "take-map" (takeMap maxValue 2) (sourceUnfoldrM value)
659− , benchFold "filter-drop" (filterDrop maxValue 2) (sourceUnfoldrM value)
660− , benchFold "filter-take" (filterTake maxValue 2) (sourceUnfoldrM value)
661− , benchFold "filter-scan" (filterScan maxValue 2) (sourceUnfoldrM value)
662− , benchFold "filter-map" (filterMap maxValue 2) (sourceUnfoldrM value)
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)
663796 ]
664− , bgroup "X4"
665− [ benchFold "scan-map" (scanMap 4) (sourceUnfoldrM value)
666− , benchFold "drop-map" (dropMap 4) (sourceUnfoldrM value)
667− , benchFold "drop-scan" (dropScan 4) (sourceUnfoldrM value)
668− , benchFold "take-drop" (takeDrop maxValue 4) (sourceUnfoldrM value)
669− , benchFold "take-scan" (takeScan maxValue 4) (sourceUnfoldrM value)
670− , benchFold "take-map" (takeMap maxValue 4) (sourceUnfoldrM value)
671− , benchFold "filter-drop" (filterDrop maxValue 4) (sourceUnfoldrM value)
672− , benchFold "filter-take" (filterTake maxValue 4) (sourceUnfoldrM value)
673− , benchFold "filter-scan" (filterScan maxValue 4) (sourceUnfoldrM value)
674− , benchFold "filter-map" (filterMap maxValue 4) (sourceUnfoldrM value)
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)
675811 ]
676− ]
677− where
678812
679− maxValue = value
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+ ]
680827
681828o_1_space_list :: Int -> Benchmark
682−o_1_space_list value =
829+o_1_space_list streamLen =
683830 bgroup "list"
684831 [ bgroup "elimination"
685− [ benchList "last" (\xs -> [List.last xs]) (sourceUnfoldrList value)
832+ [ benchList "last" (\xs -> [List.last xs]) (unfoldrList streamLen)
686833 ]
687834 , bgroup "nested"
688− [ benchList "toNullAp" toNullApNestedList (sourceUnfoldrList value2)
689− , benchList "toNull" toNullNestedList (sourceUnfoldrList value2)
690− , benchList "toNull3" toNullNestedList3 (sourceUnfoldrList value3)
691− , benchList "filterAllIn" filterAllInNestedList (sourceUnfoldrList value2)
692− , benchList "filterAllOut" filterAllOutNestedList (sourceUnfoldrList value2)
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)
693840 ]
694841 ]
695842 where
696843
697− value2 = round (P.fromIntegral value**(1/2::P.Double)) -- double nested loop
698− value3 = round (P.fromIntegral value**(1/3::P.Double)) -- triple nested loop
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
699846
700847o_1_space :: Int -> Benchmark
701−o_1_space value =
848+o_1_space streamLen =
702849 bgroup (o_1_space_prefix moduleName)
703− [ o_1_space_generation value
704− , o_1_space_elimination value
705− , o_1_space_nested value
706− , o_1_space_transformation value
707− , o_1_space_concat value
708− , o_1_space_filtering value
709− , o_1_space_zipping value
710− , o_1_space_mixed value
711− , o_1_space_list value
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
712863 ]
713864
714865o_n_heap :: Int -> Benchmark
715−o_n_heap value =
866+o_n_heap streamLen =
716867 bgroup (o_n_heap_prefix moduleName)
717868 [ bgroup "transformation"
718− [ benchFold "foldlS" (foldlS 1) (sourceUnfoldrM value)
869+ [ benchFold "foldlS" (foldlS 1) (unfoldrM streamLen)
719870 ]
720871 ]
721872
722873{-# INLINE benchK #-}
723874benchK :: P.String -> (Int -> Stream P.IO Int) -> Benchmark
724−benchK name f = bench name $ nfIO $ randomRIO (1,1) >>= toNull . f
875+benchK name f = bench name $ nfIO $ randomRIO (1,1) >>= drain . f
725876
726877o_n_stack :: Int -> Int -> Int -> Benchmark
727−o_n_stack value iterStreamLen maxIters =
878+o_n_stack streamLen iterStreamLen maxIters =
728879 bgroup (o_n_stack_prefix moduleName)
729880 [ bgroup "elimination"
730− [ benchFold "tail" tail (sourceUnfoldrM value)
731− , benchFold "nullTail" nullTail (sourceUnfoldrM value)
732− , benchFold "headTail" headTail (sourceUnfoldrM value)
881+ [ benchFold "tail" tail (unfoldrM streamLen)
882+ , benchFold "nullTail" nullTail (unfoldrM streamLen)
883+ , benchFold "headTail" headTail (unfoldrM streamLen)
733884 ]
734885 , bgroup "transformation"
735886 [
736887 -- XXX why do these need so much stack
737− benchFold "intersperse" (intersperse maxValue 1) (sourceUnfoldrM value2)
738− , benchFold "interspersePure" (intersperse maxValue 1) (sourceUnfoldr value2)
888+ benchFold "intersperse" (intersperse streamLen 1) (unfoldrM streamLen2)
889+ , benchFold "interspersePure" (intersperse streamLen 1) (unfoldr streamLen2)
739890 ]
740891 , bgroup "transformationX4"
741892 [
742− benchFold "intersperse" (intersperse maxValue 4) (sourceUnfoldrM value16)
893+ benchFold "intersperse" (intersperse streamLen 4) (unfoldrM streamLen16)
743894 ]
744895 , bgroup "iterated"
745896 [ benchK "mapM" (iterateMapM iterStreamLen maxIters)
746897 , benchK "scan(1/10)" (iterateScan iterStreamLen maxIters)
747898 , benchK "filterEven" (iterateFilterEven iterStreamLen maxIters)
748− , benchK "takeAll" (iterateTakeAll maxValue iterStreamLen maxIters)
899+ , benchK "takeAll" (iterateTakeAll streamLen iterStreamLen maxIters)
749900 , benchK "dropOne" (iterateDropOne iterStreamLen maxIters)
750− , benchK "dropWhileFalse(1/10)" (iterateDropWhileFalse maxValue iterStreamLen maxIters)
751− , benchK "dropWhileTrue" (iterateDropWhileTrue maxValue iterStreamLen maxIters)
901+ , benchK "dropWhileFalse(1/10)" (iterateDropWhileFalse streamLen iterStreamLen maxIters)
902+ , benchK "dropWhileTrue" (iterateDropWhileTrue streamLen iterStreamLen maxIters)
752903 ]
753904 ]
754905 where
755− value2 = round (P.fromIntegral value**(1/2::P.Double)) -- double nested loop
756− value16 = round (P.fromIntegral value**(1/16::P.Double)) -- triple nested loop
757− maxValue = value
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
758908
759909o_n_space :: Int -> Benchmark
760−o_n_space value =
910+o_n_space streamLen =
761911 bgroup (o_n_space_prefix moduleName)
762912 [ bgroup "elimination"
763− [ benchFold "toList" toList (sourceUnfoldrM value)
913+ [ benchFold "toList" toList (unfoldrM streamLen)
764914 ]
765915 ]
766916
@@ -771,14 +921,14 @@ benchList name run f = bench name $ nfIO $ randomRIO (1,1) >>= return . run . f
771921main :: IO ()
772922main =
773923 defaultMain
774− [ o_1_space value
775− , o_n_stack value iterStreamLen maxIters
776− , o_n_heap value
777− , o_n_space value
924+ [ o_1_space streamLen
925+ , o_n_stack streamLen iterStreamLen maxIters
926+ , o_n_heap streamLen
927+ , o_n_space streamLen
778928 ]
779929
780930 where
781931
782− value = 100000
932+ streamLen = 100000
783933 maxIters = 10000
784934 iterStreamLen = 10