@@ -8,7 +8,6 @@
88{-# LANGUAGE CPP #-}
99{-# LANGUAGE FlexibleContexts #-}
1010{-# LANGUAGE ScopedTypeVariables #-}
11−{-# OPTIONS_GHC -Wno-orphans #-}
1211
1312#ifdef __HADDOCK_VERSION__
1413#undef INSPECTION
@@ -21,23 +20,19 @@
2120
2221module Main (main) where
2322
24−#if !MIN_VERSION_base(4,18,0)
25−import Control.Applicative (liftA2)
26−#endif
2723import Control.Monad (when)
2824import Data.Maybe (isJust)
2925import System.Random (randomRIO)
3026import Prelude hiding
31− ( Foldable(..), tail, mapM_, last, map, mapM, concatMap, zipWith, init, iterate
32− , repeat, replicate
33− )
27+ (tail, mapM_, foldl, last, map, mapM, concatMap, zip, init)
3428
3529import qualified Prelude as P
3630import qualified Data.List as List
3731
38−import qualified Streamly.Internal.Data.StreamK as S
32+import qualified Streamly.Internal.Data.Stream.StreamK as S
33+import qualified Streamly.Internal.Data.SVar as S
3934
40−import Test.Tasty.Bench (bench, nfIO, bgroup, Benchmark, defaultMain)
35+import Gauge (bench, nfIO, bgroup, Benchmark, defaultMain)
4136
4237import Streamly.Benchmark.Common
4338
@@ -45,161 +40,88 @@ import Streamly.Benchmark.Common
4540import Test.Inspection
4641#endif
4742
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−
11643-------------------------------------------------------------------------------
11744-- Stream generation and elimination
11845-------------------------------------------------------------------------------
11946
120−type Stream m a = S.StreamK m a
47+type Stream m a = S.Stream m a
12148
122−{-# INLINE unfoldr #-}
123−unfoldr :: Int -> Int -> Stream m Int
124−unfoldr streamLen n = S.unfoldr step n
49+{-# INLINE sourceUnfoldr #-}
50+sourceUnfoldr :: Int -> Int -> Stream m Int
51+sourceUnfoldr value n = S.unfoldr step n
12552 where
12653 step cnt =
127− if cnt > n + streamLen
54+ if cnt > n + value
12855 then Nothing
12956 else Just (cnt, cnt + 1)
13057
131−{-# INLINE unfoldrM #-}
132−unfoldrM :: Monad m => Int -> Int -> Stream m Int
133−unfoldrM streamLen n = S.unfoldrMWith S.consM step n
58+{-# INLINE sourceUnfoldrN #-}
59+sourceUnfoldrN :: Int -> Int -> Stream m Int
60+sourceUnfoldrN m n = S.unfoldr step n
13461 where
13562 step cnt =
136− if cnt > n + streamLen
63+ if cnt > n + m
64+ then Nothing
65+ else Just (cnt, cnt + 1)
66+
67+{-# INLINE sourceUnfoldrM #-}
68+sourceUnfoldrM :: S.MonadAsync m => Int -> Int -> Stream m Int
69+sourceUnfoldrM value n = S.unfoldrM step n
70+ where
71+ step cnt =
72+ if cnt > n + value
13773 then return Nothing
13874 else return (Just (cnt, cnt + 1))
13975
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
76+{-# INLINE sourceUnfoldrMN #-}
77+sourceUnfoldrMN :: S.MonadAsync m => Int -> Int -> Stream m Int
78+sourceUnfoldrMN m n = S.unfoldrM step n
79+ where
80+ step cnt =
81+ if cnt > n + m
82+ then return Nothing
83+ else return (Just (cnt, cnt + 1))
15584
156−{-# INLINE iterate #-}
157−iterate :: Int -> Int -> Stream m Int
158−iterate streamLen = S.take streamLen . S.iterate (+1)
85+{-# INLINE sourceFromFoldable #-}
86+sourceFromFoldable :: Int -> Int -> Stream m Int
87+sourceFromFoldable value n = S.fromFoldable [n..n+value]
15988
160−{-# INLINE iterateM #-}
161−iterateM :: Monad m => Int -> Int -> Stream m Int
162−iterateM streamLen = S.take streamLen . S.iterateM (return . (+1)) . return
89+{-# INLINE sourceFromFoldableM #-}
90+sourceFromFoldableM :: S.MonadAsync m => Int -> Int -> Stream m Int
91+sourceFromFoldableM value n =
92+ Prelude.foldr S.consM S.nil (Prelude.fmap return [n..n+value])
16393
164−{-# INLINE fromFoldable #-}
165−fromFoldable :: Int -> Int -> Stream m Int
166−fromFoldable streamLen n = S.fromFoldable [n..n+streamLen]
94+{-# INLINABLE concatMapFoldableWith #-}
95+concatMapFoldableWith :: (S.IsStream t, Foldable f)
96+ => (t m b -> t m b -> t m b) -> (a -> t m b) -> f a -> t m b
97+concatMapFoldableWith f g = Prelude.foldr (f . g) S.nil
16798
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])
99+{-# INLINE sourceFoldMapWith #-}
100+sourceFoldMapWith :: Int -> Int -> Stream m Int
101+sourceFoldMapWith value n = concatMapFoldableWith S.serial S.yield [n..n+value]
172102
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]
103+{-# INLINE sourceFoldMapWithM #-}
104+sourceFoldMapWithM :: Monad m => Int -> Int -> Stream m Int
105+sourceFoldMapWithM value n =
106+ concatMapFoldableWith S.serial (S.yieldM . return) [n..n+value]
190107
191108-------------------------------------------------------------------------------
192109-- Elimination
193110-------------------------------------------------------------------------------
194111
195−{-# INLINE drain #-}
196−drain :: Monad m => Stream m a -> m ()
197−drain = S.drain
112+{-# INLINE runStream #-}
113+runStream :: Monad m => Stream m a -> m ()
114+runStream = S.drain
115+-- runStream = S.mapM_ (\_ -> return ())
198116
199117{-# INLINE mapM_ #-}
200118mapM_ :: Monad m => Stream m a -> m ()
201119mapM_ = S.mapM_ (\_ -> return ())
202120
121+{-# INLINE toNull #-}
122+toNull :: Monad m => Stream m Int -> m ()
123+toNull = runStream
124+
203125{-# INLINE uncons #-}
204126uncons :: Monad m => Stream m Int -> m ()
205127uncons s = do
@@ -209,13 +131,13 @@ uncons s = do
209131 Just (_, t) -> uncons t
210132
211133{-# INLINE init #-}
212−init :: Monad m => Stream m a -> m ()
134+init :: (Monad m, S.IsStream t) => t m a -> m ()
213135init s = do
214136 t <- S.init s
215137 P.mapM_ S.drain t
216138
217139{-# INLINE tail #-}
218−tail :: Monad m => Stream m a -> m ()
140+tail :: (Monad m, S.IsStream t) => t m a -> m ()
219141tail s = S.tail s >>= P.mapM_ tail
220142
221143{-# INLINE nullTail #-}
@@ -234,9 +156,9 @@ headTail s = do
234156toList :: Monad m => Stream m Int -> m [Int]
235157toList = S.toList
236158
237−{-# INLINE foldl' #-}
238−foldl' :: Monad m => Stream m Int -> m Int
239−foldl' = S.foldl' (+) 0
159+{-# INLINE foldl #-}
160+foldl :: Monad m => Stream m Int -> m Int
161+foldl = S.foldl' (+) 0
240162
241163{-# INLINE last #-}
242164last :: Monad m => Stream m Int -> m (Maybe Int)
@@ -246,36 +168,40 @@ last = S.last
246168-- Transformation
247169-------------------------------------------------------------------------------
248170
171+{-# INLINE transform #-}
172+transform :: Monad m => Stream m a -> m ()
173+transform = runStream
174+
249175{-# INLINE composeN #-}
250176composeN
251177 :: Monad m
252178 => Int -> (Stream m Int -> Stream m Int) -> Stream m Int -> m ()
253179composeN n f =
254180 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
181+ 1 -> transform . f
182+ 2 -> transform . f . f
183+ 3 -> transform . f . f . f
184+ 4 -> transform . f . f . f . f
259185 _ -> undefined
260186
261−{-# INLINE scanl' #-}
262−scanl' :: Monad m => Int -> Stream m Int -> m ()
263−scanl' n = composeN n $ S.scanl' (+) 0
187+{-# INLINE scan #-}
188+scan :: Monad m => Int -> Stream m Int -> m ()
189+scan n = composeN n $ S.scanl' (+) 0
264190
265191{-# INLINE map #-}
266192map :: Monad m => Int -> Stream m Int -> m ()
267−map n = composeN n $ S.map (+ 1)
193+map n = composeN n $ P.fmap (+ 1)
268194
269195{-# INLINE fmapK #-}
270196fmapK :: Monad m => Int -> Stream m Int -> m ()
271197fmapK n = composeN n $ P.fmap (+ 1)
272198
273199{-# INLINE mapM #-}
274−mapM :: Monad m => Int -> Stream m Int -> m ()
275−mapM n = composeN n $ S.mapMWith S.consM return
200+mapM :: S.MonadAsync m => Int -> Stream m Int -> m ()
201+mapM n = composeN n $ S.mapM return
276202
277203{-# INLINE mapMSerial #-}
278−mapMSerial :: Monad m => Int -> Stream m Int -> m ()
204+mapMSerial :: S.MonadAsync m => Int -> Stream m Int -> m ()
279205mapMSerial n = composeN n $ S.mapMSerial return
280206
281207{-# INLINE filterEven #-}
@@ -284,11 +210,11 @@ filterEven n = composeN n $ S.filter even
284210
285211{-# INLINE filterAllOut #-}
286212filterAllOut :: Monad m => Int -> Int -> Stream m Int -> m ()
287−filterAllOut streamLen n = composeN n $ S.filter (> streamLen)
213+filterAllOut maxValue n = composeN n $ S.filter (> maxValue)
288214
289215{-# INLINE filterAllIn #-}
290216filterAllIn :: Monad m => Int -> Int -> Stream m Int -> m ()
291−filterAllIn streamLen n = composeN n $ S.filter (<= streamLen)
217+filterAllIn maxValue n = composeN n $ S.filter (<= maxValue)
292218
293219{-# INLINE _takeOne #-}
294220_takeOne :: Monad m => Int -> Stream m Int -> m ()
@@ -296,11 +222,11 @@ _takeOne n = composeN n $ S.take 1
296222
297223{-# INLINE takeAll #-}
298224takeAll :: Monad m => Int -> Int -> Stream m Int -> m ()
299−takeAll streamLen n = composeN n $ S.take streamLen
225+takeAll maxValue n = composeN n $ S.take maxValue
300226
301227{-# INLINE takeWhileTrue #-}
302228takeWhileTrue :: Monad m => Int -> Int -> Stream m Int -> m ()
303−takeWhileTrue streamLen n = composeN n $ S.takeWhile (<= streamLen)
229+takeWhileTrue maxValue n = composeN n $ S.takeWhile (<= maxValue)
304230
305231{-# INLINE dropOne #-}
306232dropOne :: Monad m => Int -> Stream m Int -> m ()
@@ -308,11 +234,11 @@ dropOne n = composeN n $ S.drop 1
308234
309235{-# INLINE dropAll #-}
310236dropAll :: Monad m => Int -> Int -> Stream m Int -> m ()
311−dropAll streamLen n = composeN n $ S.drop streamLen
237+dropAll maxValue n = composeN n $ S.drop maxValue
312238
313239{-# INLINE dropWhileTrue #-}
314240dropWhileTrue :: Monad m => Int -> Int -> Stream m Int -> m ()
315−dropWhileTrue streamLen n = composeN n $ S.dropWhile (<= streamLen)
241+dropWhileTrue maxValue n = composeN n $ S.dropWhile (<= maxValue)
316242
317243{-# INLINE dropWhileFalse #-}
318244dropWhileFalse :: Monad m => Int -> Stream m Int -> m ()
@@ -327,8 +253,8 @@ foldlS :: Monad m => Int -> Stream m Int -> m ()
327253foldlS n = composeN n $ S.foldlS (flip S.cons) S.nil
328254
329255{-# INLINE intersperse #-}
330−intersperse :: Monad m => Int -> Int -> Stream m Int -> m ()
331−intersperse streamLen n = composeN n $ S.intersperse streamLen
256+intersperse :: S.MonadAsync m => Int -> Int -> Stream m Int -> m ()
257+intersperse maxValue n = composeN n $ S.intersperse maxValue
332258
333259-------------------------------------------------------------------------------
334260-- Iteration
@@ -336,67 +262,51 @@ intersperse streamLen n = composeN n $ S.intersperse streamLen
336262
337263{-# INLINE iterateSource #-}
338264iterateSource
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)
265+ :: S.MonadAsync m
266+ => Int -> (Stream m Int -> Stream m Int) -> Int -> Int -> Stream m Int
267+iterateSource iterStreamLen g i n = f i (sourceUnfoldrMN iterStreamLen n)
341268 where
342269 f (0 :: Int) m = g m
343270 f x m = g (f (x P.- 1) m)
344271
345272-- this is quadratic
346273{-# 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)
274+iterateScan :: S.MonadAsync m => Int -> Int -> Int -> Stream m Int
275+iterateScan iterStreamLen maxIters = iterateSource iterStreamLen (S.scanl' (+) 0) (maxIters `div` 10)
350276
351277-- this is quadratic
352278{-# 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)
279+iterateDropWhileFalse :: S.MonadAsync m => Int -> Int -> Int -> Int -> Stream m Int
280+iterateDropWhileFalse maxValue iterStreamLen maxIters =
281+ iterateSource iterStreamLen (S.dropWhile (> maxValue)) (maxIters `div` 10)
356282
357283{-# INLINE iterateMapM #-}
358−iterateMapM :: Monad m => Int -> Int -> Int -> Stream m Int
359−iterateMapM iterStreamLen =
360− iterateSource iterStreamLen (S.mapMWith S.consM return)
284+iterateMapM :: S.MonadAsync m => Int -> Int -> Int -> Stream m Int
285+iterateMapM iterStreamLen maxIters = iterateSource iterStreamLen (S.mapM return) maxIters
361286
362287{-# INLINE iterateFilterEven #-}
363−iterateFilterEven :: Monad m => Int -> Int -> Int -> Stream m Int
364−iterateFilterEven iterStreamLen = iterateSource iterStreamLen (S.filter even)
288+iterateFilterEven :: S.MonadAsync m => Int -> Int -> Int -> Stream m Int
289+iterateFilterEven iterStreamLen maxIters = iterateSource iterStreamLen (S.filter even) maxIters
365290
366291{-# INLINE iterateTakeAll #-}
367−iterateTakeAll :: Monad m => Int -> Int -> Int -> Int -> Stream m Int
368−iterateTakeAll streamLen iterStreamLen =
369− iterateSource iterStreamLen (S.take streamLen)
292+iterateTakeAll :: S.MonadAsync m => Int -> Int -> Int -> Int -> Stream m Int
293+iterateTakeAll maxValue iterStreamLen maxIters = iterateSource iterStreamLen (S.take maxValue) maxIters
370294
371295{-# INLINE iterateDropOne #-}
372−iterateDropOne :: Monad m => Int -> Int -> Int -> Stream m Int
373−iterateDropOne iterStreamLen = iterateSource iterStreamLen (S.drop 1)
296+iterateDropOne :: S.MonadAsync m => Int -> Int -> Int -> Stream m Int
297+iterateDropOne iterStreamLen maxIters = iterateSource iterStreamLen (S.drop 1) maxIters
374298
375299{-# INLINE iterateDropWhileTrue #-}
376−iterateDropWhileTrue ::
377− Monad m => Int -> Int -> Int -> Int -> Stream m Int
378−iterateDropWhileTrue streamLen iterStreamLen =
379− iterateSource iterStreamLen (S.dropWhile (<= streamLen))
300+iterateDropWhileTrue :: S.MonadAsync m => Int -> Int -> Int -> Int -> Stream m Int
301+iterateDropWhileTrue maxValue iterStreamLen maxIters = iterateSource iterStreamLen (S.dropWhile (<= maxValue)) maxIters
380302
381303-------------------------------------------------------------------------------
382304-- Zipping
383305-------------------------------------------------------------------------------
384306
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
307+{-# INLINE zip #-}
308+zip :: Monad m => Stream m Int -> m ()
309+zip src = transform $ S.zipWith (,) src src
400310
401311-------------------------------------------------------------------------------
402312-- Mixed Composition
@@ -416,31 +326,32 @@ dropScan n = composeN n $ S.scanl' (+) 0 . S.drop 1
416326
417327{-# INLINE takeDrop #-}
418328takeDrop :: Monad m => Int -> Int -> Stream m Int -> m ()
419−takeDrop streamLen n = composeN n $ S.drop 1 . S.take streamLen
329+takeDrop maxValue n = composeN n $ S.drop 1 . S.take maxValue
420330
421331{-# INLINE takeScan #-}
422332takeScan :: Monad m => Int -> Int -> Stream m Int -> m ()
423−takeScan streamLen n = composeN n $ S.scanl' (+) 0 . S.take streamLen
333+takeScan maxValue n = composeN n $ S.scanl' (+) 0 . S.take maxValue
424334
425335{-# INLINE takeMap #-}
426336takeMap :: Monad m => Int -> Int -> Stream m Int -> m ()
427−takeMap streamLen n = composeN n $ S.map (subtract 1) . S.take streamLen
337+takeMap maxValue n = composeN n $ S.map (subtract 1) . S.take maxValue
428338
429339{-# INLINE filterDrop #-}
430340filterDrop :: Monad m => Int -> Int -> Stream m Int -> m ()
431−filterDrop streamLen n = composeN n $ S.drop 1 . S.filter (<= streamLen)
341+filterDrop maxValue n = composeN n $ S.drop 1 . S.filter (<= maxValue)
432342
433343{-# INLINE filterTake #-}
434344filterTake :: Monad m => Int -> Int -> Stream m Int -> m ()
435−filterTake streamLen n = composeN n $ S.take streamLen . S.filter (<= streamLen)
345+filterTake maxValue n = composeN n $ S.take maxValue . S.filter (<= maxValue)
436346
347+-- Initially, this used maxBound not maxValue
437348{-# INLINE filterScan #-}
438−filterScan :: Monad m => Int -> Stream m Int -> m ()
439−filterScan n = composeN n $ S.scanl' (+) 0 . S.filter (<= maxBound)
349+filterScan :: Monad m => Int -> Int -> Stream m Int -> m ()
350+filterScan maxValue n = composeN n $ S.scanl' (+) 0 . S.filter (<= maxValue)
440351
441352{-# INLINE filterMap #-}
442353filterMap :: Monad m => Int -> Int -> Stream m Int -> m ()
443−filterMap streamLen n = composeN n $ S.map (subtract 1) . S.filter (<= streamLen)
354+filterMap maxValue n = composeN n $ S.map (subtract 1) . S.filter (<= maxValue)
444355
445356-------------------------------------------------------------------------------
446357-- ConcatMap
@@ -452,8 +363,8 @@ filterMap streamLen n = composeN n $ S.map (subtract 1) . S.filter (<= streamLen
452363concatMap :: Int -> Int -> Int -> IO ()
453364concatMap outer inner n =
454365 S.drain $ S.concatMap
455− (\_ -> unfoldrM inner n)
456− (unfoldrM outer n)
366+ (\_ -> sourceUnfoldrMN inner n)
367+ (sourceUnfoldrMN outer n)
457368
458369#ifdef INSPECTION
459370inspect $ hasNoTypeClasses 'concatMap
@@ -465,8 +376,8 @@ inspect $ hasNoTypeClasses 'concatMap
465376concatMapPure :: Int -> Int -> Int -> IO ()
466377concatMapPure outer inner n =
467378 S.drain $ S.concatMap
468− (\_ -> unfoldr inner n)
469− (unfoldr outer n)
379+ (\_ -> sourceUnfoldrN inner n)
380+ (sourceUnfoldrN outer n)
470381
471382#ifdef INSPECTION
472383inspect $ hasNoTypeClasses 'concatMapPure
@@ -477,7 +388,7 @@ inspect $ hasNoTypeClasses 'concatMapPure
477388{-# INLINE concatMapRepl #-}
478389concatMapRepl :: Int -> Int -> Int -> IO ()
479390concatMapRepl outer inner n =
480− S.drain $ S.concatMap (S.replicate inner) (unfoldrM outer n)
391+ S.drain $ S.concatMap (S.replicate inner) (sourceUnfoldrMN outer n)
481392
482393#ifdef INSPECTION
483394inspect $ hasNoTypeClasses 'concatMapRepl
@@ -489,70 +400,44 @@ inspect $ hasNoTypeClasses 'concatMapRepl
489400sourceConcatMapId :: Monad m
490401 => Int -> Int -> Stream m (Stream m Int)
491402sourceConcatMapId val n =
492− S.fromFoldable $ fmap (S.fromEffect . return) [n..n+val]
403+ S.fromFoldable $ fmap (S.yieldM . return) [n..n+val]
493404
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)
405+{-# INLINE concatStreamsWith #-}
406+concatStreamsWith :: Int -> Int -> Int -> IO ()
407+concatStreamsWith outer inner n =
408+ S.drain $ S.concatMapBy S.serial
409+ (sourceUnfoldrMN inner)
410+ (sourceUnfoldrMN outer n)
500411
501412-------------------------------------------------------------------------------
502413-- Nested Composition
503414-------------------------------------------------------------------------------
504415
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
416+{-# INLINE toNullApNested #-}
417+toNullApNested :: Monad m => Stream m Int -> m ()
418+toNullApNested s = runStream $ do
534419 (+) <$> s <*> s
535420
536−{-# INLINE drainMonad #-}
537−drainMonad :: Monad m => Stream m Int -> m ()
538−drainMonad s = drain $ do
421+{-# INLINE toNullNested #-}
422+toNullNested :: Monad m => Stream m Int -> m ()
423+toNullNested s = runStream $ do
539424 x <- s
540425 y <- s
541426 return $ x + y
542427
543−{-# INLINE drainMonad3 #-}
544−drainMonad3 :: Monad m => Stream m Int -> m ()
545−drainMonad3 s = drain $ do
428+{-# INLINE toNullNested3 #-}
429+toNullNested3 :: Monad m => Stream m Int -> m ()
430+toNullNested3 s = runStream $ do
546431 x <- s
547432 y <- s
548433 z <- s
549434 return $ x + y + z
550435
551−{-# INLINE filterAllOutMonad #-}
552−filterAllOutMonad
436+{-# INLINE filterAllOutNested #-}
437+filterAllOutNested
553438 :: Monad m
554439 => Stream m Int -> m ()
555−filterAllOutMonad str = drain $ do
440+filterAllOutNested str = runStream $ do
556441 x <- str
557442 y <- str
558443 let s = x + y
@@ -560,11 +445,11 @@ filterAllOutMonad str = drain $ do
560445 then return s
561446 else S.nil
562447
563−{-# INLINE filterAllInMonad #-}
564−filterAllInMonad
448+{-# INLINE filterAllInNested #-}
449+filterAllInNested
565450 :: Monad m
566451 => Stream m Int -> m ()
567−filterAllInMonad str = drain $ do
452+filterAllInNested str = runStream $ do
568453 x <- str
569454 y <- str
570455 let s = x + y
@@ -576,14 +461,9 @@ filterAllInMonad str = drain $ do
576461-- Nested Composition Pure lists
577462-------------------------------------------------------------------------------
578463
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
464+{-# INLINE sourceUnfoldrList #-}
465+sourceUnfoldrList :: Int -> Int -> [Int]
466+sourceUnfoldrList maxval n = List.unfoldr step n
587467 where
588468 step cnt =
589469 if cnt > n + maxval
@@ -634,301 +514,256 @@ filterAllInNestedList str = do
634514-------------------------------------------------------------------------------
635515
636516moduleName :: String
637−moduleName = "Data.StreamK"
638−
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)
517+moduleName = "Data.Stream.StreamK"
518+
519+o_1_space :: [Benchmark]
520+o_1_space =
521+ [ bgroup (o_1_space_prefix moduleName)
522+ [ bgroup "generation"
523+ [ benchFold "unfoldr" toNull (sourceUnfoldr value)
524+ , benchFold "unfoldrM" toNull (sourceUnfoldrM value)
525+
526+ , benchFold "fromFoldable" toNull (sourceFromFoldable value)
527+ , benchFold "fromFoldableM" toNull (sourceFromFoldableM value)
653528
654529 -- appends
655− , benchFold "concatMapFoldableWith" drain (concatMapFoldableSerial streamLen)
656− , benchFold "concatMapFoldableWithM" drain (concatMapFoldableSerialM streamLen)
530+ , benchFold "concatMapFoldableWith" toNull (sourceFoldMapWith value)
531+ , benchFold "concatMapFoldableWithM" toNull (sourceFoldMapWithM value)
657532 ]
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)
533+ , bgroup "elimination"
534+ [ benchFold "toNull" toNull (sourceUnfoldrM value)
535+ , benchFold "mapM_" mapM_ (sourceUnfoldrM value)
536+ , benchFold "uncons" uncons (sourceUnfoldrM value)
537+ , benchFold "init" init (sourceUnfoldrM value)
538+ , benchFold "foldl'" foldl (sourceUnfoldrM value)
539+ , benchFold "last" last (sourceUnfoldrM value)
668540 ]
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)
541+ , bgroup "nested"
542+ [ benchFold "toNullAp" toNullApNested (sourceUnfoldrMN value2)
543+ , benchFold "toNull" toNullNested (sourceUnfoldrMN value2)
544+ , benchFold "toNull3" toNullNested3 (sourceUnfoldrMN value3)
545+ , benchFold "filterAllIn" filterAllInNested (sourceUnfoldrMN value2)
546+ , benchFold "filterAllOut" filterAllOutNested (sourceUnfoldrMN value2)
547+ , benchFold "toNullApPure" toNullApNested (sourceUnfoldrN value2)
548+ , benchFold "toNullPure" toNullNested (sourceUnfoldrN value2)
549+ , benchFold "toNull3Pure" toNullNested3 (sourceUnfoldrN value3)
550+ , benchFold "filterAllInPure" filterAllInNested (sourceUnfoldrN value2)
551+ , benchFold "filterAllOutPure" filterAllOutNested (sourceUnfoldrN value2)
683552 ]
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)
553+ , bgroup "transformation"
554+ [ benchFold "foldrS" (foldrS 1) (sourceUnfoldrM value)
555+ , benchFold "scan" (scan 1) (sourceUnfoldrM value)
556+ , benchFold "map" (map 1) (sourceUnfoldrM value)
557+ , benchFold "fmap" (fmapK 1) (sourceUnfoldrM value)
558+ , benchFold "mapM" (mapM 1) (sourceUnfoldrM value)
559+ , benchFold "mapMSerial" (mapMSerial 1) (sourceUnfoldrM value)
697560 ]
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)
561+ , bgroup "transformationX4"
562+ [ benchFold "scan" (scan 4) (sourceUnfoldrM value)
563+ , benchFold "map" (map 4) (sourceUnfoldrM value)
564+ , benchFold "fmap" (fmapK 4) (sourceUnfoldrM value)
565+ , benchFold "mapM" (mapM 4) (sourceUnfoldrM value)
566+ , benchFold "mapMSerial" (mapMSerial 4) (sourceUnfoldrM value)
707567 -- XXX this is horribly slow
708− -- , benchFold "concatMap" (concatMap 4) (unfoldrM streamLen16)
568+ -- , benchFold "concatMap" (concatMap 4) (sourceUnfoldrMN value16)
709569 ]
710−
711−o_1_space_concat :: Int -> Benchmark
712−o_1_space_concat streamLen =
713− bgroup "concat"
570+ , bgroup "concat"
714571 [ benchIOSrc1 "concatMapPure (n of 1)"
715− (concatMapPure streamLen 1)
572+ (concatMapPure value 1)
716573 , benchIOSrc1 "concatMapPure (sqrt n of sqrt n)"
717− (concatMapPure streamLen2 streamLen2)
574+ (concatMapPure value2 value2)
718575 , benchIOSrc1 "concatMapPure (1 of n)"
719− (concatMapPure 1 streamLen)
576+ (concatMapPure 1 value)
720577
721578 , benchIOSrc1 "concatMap (n of 1)"
722− (concatMap streamLen 1)
579+ (concatMap value 1)
723580 , benchIOSrc1 "concatMap (sqrt n of sqrt n)"
724− (concatMap streamLen2 streamLen2)
581+ (concatMap value2 value2)
725582 , benchIOSrc1 "concatMap (1 of n)"
726− (concatMap 1 streamLen)
583+ (concatMap 1 value)
727584
728585 , benchIOSrc1 "concatMapRepl (sqrt n of sqrt n)"
729− (concatMapRepl streamLen2 streamLen2)
586+ (concatMapRepl value2 value2)
730587
731588 -- This is for comparison with concatMapFoldableWith
732589 , 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)
590+ (S.drain . S.concatMapBy S.serial id . sourceConcatMapId value)
591+
592+ , benchIOSrc1 "concatMapWith (n of 1)"
593+ (concatStreamsWith value 1)
594+ , benchIOSrc1 "concatMapWith (sqrt n of sqrt n)"
595+ (concatStreamsWith value2 value2)
596+ , benchIOSrc1 "concatMapWith (1 of n)"
597+ (concatStreamsWith 1 value)
744598 ]
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)
760− ]
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)
774− ]
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)
599+ , bgroup "filtering"
600+ [ benchFold "filter-even" (filterEven 1) (sourceUnfoldrM value)
601+ , benchFold "filter-all-out" (filterAllOut maxValue 1) (sourceUnfoldrM value)
602+ , benchFold "filter-all-in" (filterAllIn maxValue 1) (sourceUnfoldrM value)
603+ , benchFold "take-all" (takeAll maxValue 1) (sourceUnfoldrM value)
604+ , benchFold "takeWhile-true" (takeWhileTrue maxValue 1) (sourceUnfoldrM value)
605+ , benchFold "drop-one" (dropOne 1) (sourceUnfoldrM value)
606+ , benchFold "drop-all" (dropAll maxValue 1) (sourceUnfoldrM value)
607+ , benchFold "dropWhile-true" (dropWhileTrue maxValue 1) (sourceUnfoldrM value)
608+ , benchFold "dropWhile-false" (dropWhileFalse 1) (sourceUnfoldrM value)
781609 ]
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)
610+ , bgroup "filteringX4"
611+ [ benchFold "filter-even" (filterEven 4) (sourceUnfoldrM value)
612+ , benchFold "filter-all-out" (filterAllOut maxValue 4) (sourceUnfoldrM value)
613+ , benchFold "filter-all-in" (filterAllIn maxValue 4) (sourceUnfoldrM value)
614+ , benchFold "take-all" (takeAll maxValue 4) (sourceUnfoldrM value)
615+ , benchFold "takeWhile-true" (takeWhileTrue maxValue 4) (sourceUnfoldrM value)
616+ , benchFold "drop-one" (dropOne 4) (sourceUnfoldrM value)
617+ , benchFold "drop-all" (dropAll maxValue 4) (sourceUnfoldrM value)
618+ , benchFold "dropWhile-true" (dropWhileTrue maxValue 4) (sourceUnfoldrM value)
619+ , benchFold "dropWhile-false" (dropWhileFalse 4) (sourceUnfoldrM value)
796620 ]
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)
621+ , bgroup "zipping"
622+ [ benchFold "zip" zip (sourceUnfoldrM value)
811623 ]
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)
624+ , bgroup "mixed"
625+ [ benchFold "scan-map" (scanMap 1) (sourceUnfoldrM value)
626+ , benchFold "drop-map" (dropMap 1) (sourceUnfoldrM value)
627+ , benchFold "drop-scan" (dropScan 1) (sourceUnfoldrM value)
628+ , benchFold "take-drop" (takeDrop maxValue 1) (sourceUnfoldrM value)
629+ , benchFold "take-scan" (takeScan maxValue 1) (sourceUnfoldrM value)
630+ , benchFold "take-map" (takeMap maxValue 1) (sourceUnfoldrM value)
631+ , benchFold "filter-drop" (filterDrop maxValue 1) (sourceUnfoldrM value)
632+ , benchFold "filter-take" (filterTake maxValue 1) (sourceUnfoldrM value)
633+ , benchFold "filter-scan" (filterScan maxValue 1) (sourceUnfoldrM value)
634+ , benchFold "filter-map" (filterMap maxValue 1) (sourceUnfoldrM value)
826635 ]
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)
636+ , bgroup "mixedX2"
637+ [ benchFold "scan-map" (scanMap 2) (sourceUnfoldrM value)
638+ , benchFold "drop-map" (dropMap 2) (sourceUnfoldrM value)
639+ , benchFold "drop-scan" (dropScan 2) (sourceUnfoldrM value)
640+ , benchFold "take-drop" (takeDrop maxValue 2) (sourceUnfoldrM value)
641+ , benchFold "take-scan" (takeScan maxValue 2) (sourceUnfoldrM value)
642+ , benchFold "take-map" (takeMap maxValue 2) (sourceUnfoldrM value)
643+ , benchFold "filter-drop" (filterDrop maxValue 2) (sourceUnfoldrM value)
644+ , benchFold "filter-take" (filterTake maxValue 2) (sourceUnfoldrM value)
645+ , benchFold "filter-scan" (filterScan maxValue 2) (sourceUnfoldrM value)
646+ , benchFold "filter-map" (filterMap maxValue 2) (sourceUnfoldrM value)
833647 ]
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)
648+ , bgroup "mixedX4"
649+ [ benchFold "scan-map" (scanMap 4) (sourceUnfoldrM value)
650+ , benchFold "drop-map" (dropMap 4) (sourceUnfoldrM value)
651+ , benchFold "drop-scan" (dropScan 4) (sourceUnfoldrM value)
652+ , benchFold "take-drop" (takeDrop maxValue 4) (sourceUnfoldrM value)
653+ , benchFold "take-scan" (takeScan maxValue 4) (sourceUnfoldrM value)
654+ , benchFold "take-map" (takeMap maxValue 4) (sourceUnfoldrM value)
655+ , benchFold "filter-drop" (filterDrop maxValue 4) (sourceUnfoldrM value)
656+ , benchFold "filter-take" (filterTake maxValue 4) (sourceUnfoldrM value)
657+ , benchFold "filter-scan" (filterScan maxValue 4) (sourceUnfoldrM value)
658+ , benchFold "filter-map" (filterMap maxValue 4) (sourceUnfoldrM value)
840659 ]
841660 ]
661+ ]
842662 where
843663
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
863− ]
664+ value = 100000
665+ value2 = round (P.fromIntegral value**(1/2::P.Double)) -- double nested loop
666+ value3 = round (P.fromIntegral value**(1/3::P.Double)) -- triple nested loop
667+ maxValue = value
864668
865−o_n_heap :: Int -> Benchmark
866−o_n_heap streamLen =
867− bgroup (o_n_heap_prefix moduleName)
669+
670+o_n_heap :: [Benchmark]
671+o_n_heap =
672+ [ bgroup (o_n_heap_prefix moduleName)
868673 [ bgroup "transformation"
869− [ benchFold "foldlS" (foldlS 1) (unfoldrM streamLen)
674+ [ benchFold "foldlS" (foldlS 1) (sourceUnfoldrM value)
870675 ]
871676 ]
677+ ]
678+ where
679+
680+ value = 100000
872681
873682{-# INLINE benchK #-}
874683benchK :: P.String -> (Int -> Stream P.IO Int) -> Benchmark
875−benchK name f = bench name $ nfIO $ randomRIO (1,1) >>= drain . f
684+benchK name f = bench name $ nfIO $ randomRIO (1,1) >>= toNull . f
876685
877−o_n_stack :: Int -> Int -> Int -> Benchmark
878−o_n_stack streamLen iterStreamLen maxIters =
879− bgroup (o_n_stack_prefix moduleName)
686+o_n_stack :: [Benchmark]
687+o_n_stack =
688+ [ bgroup (o_n_stack_prefix moduleName)
880689 [ bgroup "elimination"
881− [ benchFold "tail" tail (unfoldrM streamLen)
882− , benchFold "nullTail" nullTail (unfoldrM streamLen)
883− , benchFold "headTail" headTail (unfoldrM streamLen)
690+ [ benchFold "tail" tail (sourceUnfoldrM value)
691+ , benchFold "nullTail" nullTail (sourceUnfoldrM value)
692+ , benchFold "headTail" headTail (sourceUnfoldrM value)
884693 ]
885694 , bgroup "transformation"
886695 [
887696 -- XXX why do these need so much stack
888− benchFold "intersperse" (intersperse streamLen 1) (unfoldrM streamLen2)
889− , benchFold "interspersePure" (intersperse streamLen 1) (unfoldr streamLen2)
697+ benchFold "intersperse" (intersperse maxValue 1) (sourceUnfoldrMN value2)
698+ , benchFold "interspersePure" (intersperse maxValue 1) (sourceUnfoldrN value2)
890699 ]
891700 , bgroup "transformationX4"
892701 [
893− benchFold "intersperse" (intersperse streamLen 4) (unfoldrM streamLen16)
702+ benchFold "intersperse" (intersperse maxValue 4) (sourceUnfoldrMN value16)
894703 ]
895704 , bgroup "iterated"
896705 [ benchK "mapM" (iterateMapM iterStreamLen maxIters)
897706 , benchK "scan(1/10)" (iterateScan iterStreamLen maxIters)
898707 , benchK "filterEven" (iterateFilterEven iterStreamLen maxIters)
899− , benchK "takeAll" (iterateTakeAll streamLen iterStreamLen maxIters)
708+ , benchK "takeAll" (iterateTakeAll maxValue iterStreamLen maxIters)
900709 , benchK "dropOne" (iterateDropOne iterStreamLen maxIters)
901− , benchK "dropWhileFalse(1/10)" (iterateDropWhileFalse streamLen iterStreamLen maxIters)
902− , benchK "dropWhileTrue" (iterateDropWhileTrue streamLen iterStreamLen maxIters)
710+ , benchK "dropWhileFalse(1/10)" (iterateDropWhileFalse maxValue iterStreamLen maxIters)
711+ , benchK "dropWhileTrue" (iterateDropWhileTrue maxValue iterStreamLen maxIters)
903712 ]
904713 ]
714+ ]
905715 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
908716
909−o_n_space :: Int -> Benchmark
910−o_n_space streamLen =
911− bgroup (o_n_space_prefix moduleName)
717+ value = 100000
718+ value2 = round (P.fromIntegral value**(1/2::P.Double)) -- double nested loop
719+ value16 = round (P.fromIntegral value**(1/16::P.Double)) -- triple nested loop
720+ maxValue = value
721+ maxIters = 10000
722+ iterStreamLen = 10
723+
724+o_n_space :: [Benchmark]
725+o_n_space =
726+ [ bgroup (o_n_space_prefix moduleName)
912727 [ bgroup "elimination"
913− [ benchFold "toList" toList (unfoldrM streamLen)
728+ [ benchFold "toList" toList (sourceUnfoldrM value)
914729 ]
915730 ]
731+ ]
732+ where
733+
734+ value = 100000
735+
916736
917737{-# INLINE benchList #-}
918738benchList :: P.String -> ([Int] -> [Int]) -> (Int -> [Int]) -> Benchmark
919739benchList name run f = bench name $ nfIO $ randomRIO (1,1) >>= return . run . f
920740
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
741+o_1_space_list :: [Benchmark]
742+o_1_space_list =
743+ [ bgroup "list"
744+ [ bgroup "elimination"
745+ [ benchList "last" (\xs -> [List.last xs]) (sourceUnfoldrList value)
928746 ]
929−
747+ , bgroup "nested"
748+ [ benchList "toNullAp" toNullApNestedList (sourceUnfoldrList value2)
749+ , benchList "toNull" toNullNestedList (sourceUnfoldrList value2)
750+ , benchList "toNull3" toNullNestedList3 (sourceUnfoldrList value3)
751+ , benchList "filterAllIn" filterAllInNestedList (sourceUnfoldrList value2)
752+ , benchList "filterAllOut" filterAllOutNestedList (sourceUnfoldrList value2)
753+ ]
754+ ]
755+ ]
930756 where
931757
932− streamLen = 100000
933− maxIters = 10000
934− iterStreamLen = 10
758+ value = 100000
759+ value2 = round (P.fromIntegral value**(1/2::P.Double)) -- double nested loop
760+ value3 = round (P.fromIntegral value**(1/3::P.Double)) -- triple nested loop
761+
762+main :: IO ()
763+main = defaultMain $ concat
764+ [ o_1_space
765+ , [bgroup (o_1_space_prefix moduleName) o_1_space_list]
766+ , o_n_stack
767+ , o_n_heap
768+ , o_n_space
769+ ]