@@ -8,7 +8,6 @@
8{-# LANGUAGE CPP #-}
8{-# LANGUAGE CPP #-}
9{-# LANGUAGE FlexibleContexts #-}
9{-# LANGUAGE FlexibleContexts #-}
10{-# LANGUAGE ScopedTypeVariables #-}
10{-# LANGUAGE ScopedTypeVariables #-}
11−{-# OPTIONS_GHC -Wno-orphans #-}
13#ifdef __HADDOCK_VERSION__
12#ifdef __HADDOCK_VERSION__
14#undef INSPECTION
13#undef INSPECTION
@@ -21,23 +20,19 @@
22module Main (main) where
21module Main (main) where
24−#if !MIN_VERSION_base(4,18,0)
25−import Control.Applicative (liftA2)
27import Control.Monad (when)
23import Control.Monad (when)
28import Data.Maybe (isJust)
24import Data.Maybe (isJust)
29import System.Random (randomRIO)
25import System.Random (randomRIO)
30import Prelude hiding
26import Prelude hiding
31− ( Foldable(..), tail, mapM_, last, map, mapM, concatMap, zipWith, init, iterate
27+ (tail, mapM_, foldl, last, map, mapM, concatMap, zip, init)
35import qualified Prelude as P
29import qualified Prelude as P
36import qualified Data.List as List
30import qualified Data.List as List
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
40−import Test.Tasty.Bench (bench, nfIO, bgroup, Benchmark, defaultMain)
35+import Gauge (bench, nfIO, bgroup, Benchmark, defaultMain)
42import Streamly.Benchmark.Common
37import Streamly.Benchmark.Common
@@ -45,161 +40,88 @@ import Streamly.Benchmark.Common
45import Test.Inspection
40import Test.Inspection
50−Benchmarks that need to be added
109−-- toStreamK (Probably can be skipped)
116-------------------------------------------------------------------------------
43-------------------------------------------------------------------------------
117-- Stream generation and elimination
44-- Stream generation and elimination
118-------------------------------------------------------------------------------
45-------------------------------------------------------------------------------
120−type Stream m a = S.StreamK m a
47+type Stream m a = S.Stream m a
49+{-# INLINE sourceUnfoldr #-}
50+sourceUnfoldr :: Int -> Int -> Stream m Int
51+sourceUnfoldr value n = S.unfoldr step n
56+ else Just (cnt, cnt + 1)
122−{-# INLINE unfoldr #-}
58+{-# INLINE sourceUnfoldrN #-}
123−unfoldr :: Int -> Int -> Stream m Int
59+sourceUnfoldrN :: Int -> Int -> Stream m Int
124−unfoldr streamLen n = S.unfoldr step n
60+sourceUnfoldrN m n = S.unfoldr step n
126 step cnt =
62 step cnt =
127− if cnt > n + streamLen
63+ if cnt > n + m
128 then Nothing
64 then Nothing
129 else Just (cnt, cnt + 1)
65 else Just (cnt, cnt + 1)
131−{-# INLINE unfoldrM #-}
67+{-# INLINE sourceUnfoldrM #-}
132−unfoldrM :: Monad m => Int -> Int -> Stream m Int
68+sourceUnfoldrM :: S.MonadAsync m => Int -> Int -> Stream m Int
133−unfoldrM streamLen n = S.unfoldrMWith S.consM step n
69+sourceUnfoldrM value n = S.unfoldrM step n
135 step cnt =
71 step cnt =
136− if cnt > n + streamLen
72+ if cnt > n + value
137 then return Nothing
73 then return Nothing
138 else return (Just (cnt, cnt + 1))
74 else return (Just (cnt, cnt + 1))
140−{-# INLINE repeat #-}
76+{-# INLINE sourceUnfoldrMN #-}
141−repeat :: Int -> Int -> Stream m Int
142−repeat streamLen = S.take streamLen . S.repeat
144−{-# INLINE repeatM #-}
145−repeatM :: Monad m => Int -> Int -> Stream m Int
77+sourceUnfoldrMN :: S.MonadAsync m => Int -> Int -> Stream m Int
146−repeatM streamLen = S.take streamLen . S.repeatM . return
78+sourceUnfoldrMN m n = S.unfoldrM step n
148−{-# INLINE replicate #-}
80+ step cnt =
149−replicate :: Int -> Int -> Stream m Int
81+ if cnt > n + m
150−replicate = S.replicate
82+ then return Nothing
83+ else return (Just (cnt, cnt + 1))
152−{-# INLINE replicateM #-}
85+{-# INLINE sourceFromFoldable #-}
153−replicateM :: Monad m => Int -> Int -> Stream m Int
86+sourceFromFoldable :: Int -> Int -> Stream m Int
154−replicateM streamLen = S.replicateMWith S.consM streamLen . return
87+sourceFromFoldable value n = S.fromFoldable [n..n+value]
156−{-# INLINE iterate #-}
89+{-# INLINE sourceFromFoldableM #-}
157−iterate :: Int -> Int -> Stream m Int
90+sourceFromFoldableM :: S.MonadAsync m => Int -> Int -> Stream m Int
158−iterate streamLen = S.take streamLen . S.iterate (+1)
91+sourceFromFoldableM value n =
92+ Prelude.foldr S.consM S.nil (Prelude.fmap return [n..n+value])
160−{-# INLINE iterateM #-}
94+{-# INLINABLE concatMapFoldableWith #-}
161−iterateM :: Monad m => Int -> Int -> Stream m Int
95+concatMapFoldableWith :: (S.IsStream t, Foldable f)
162−iterateM streamLen = S.take streamLen . S.iterateM (return . (+1)) . return
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
164−{-# INLINE fromFoldable #-}
99+{-# INLINE sourceFoldMapWith #-}
165−fromFoldable :: Int -> Int -> Stream m Int
100+sourceFoldMapWith :: Int -> Int -> Stream m Int
166−fromFoldable streamLen n = S.fromFoldable [n..n+streamLen]
101+sourceFoldMapWith value n = concatMapFoldableWith S.serial S.yield [n..n+value]
168−{-# INLINE fromFoldableM #-}
103+{-# INLINE sourceFoldMapWithM #-}
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])
173−{-# INLINABLE concatMapFoldableWith #-}
174−concatMapFoldableWith :: P.Foldable f
175− => (Stream m b -> Stream m b -> Stream m b)
176− -> (a -> Stream m b)
179−concatMapFoldableWith f g = P.foldr (f . g) S.nil
181−{-# INLINE concatMapFoldableSerial #-}
182−concatMapFoldableSerial :: Int -> Int -> Stream m Int
183−concatMapFoldableSerial streamLen n =
184− concatMapFoldableWith S.append S.fromPure [n..n+streamLen]
186−{-# INLINE concatMapFoldableSerialM #-}
187−concatMapFoldableSerialM :: Monad m => Int -> Int -> Stream m Int
104+sourceFoldMapWithM :: Monad m => Int -> Int -> Stream m Int
188−concatMapFoldableSerialM streamLen n =
105+sourceFoldMapWithM value n =
189− concatMapFoldableWith S.append (S.fromEffect . return) [n..n+streamLen]
106+ concatMapFoldableWith S.serial (S.yieldM . return) [n..n+value]
191-------------------------------------------------------------------------------
108-------------------------------------------------------------------------------
192-- Elimination
109-- Elimination
193-------------------------------------------------------------------------------
110-------------------------------------------------------------------------------
195−{-# INLINE drain #-}
112+{-# INLINE runStream #-}
196−drain :: Monad m => Stream m a -> m ()
113+runStream :: Monad m => Stream m a -> m ()
197−drain = S.drain
114+runStream = S.drain
115+-- runStream = S.mapM_ (\_ -> return ())
199{-# INLINE mapM_ #-}
117{-# INLINE mapM_ #-}
200mapM_ :: Monad m => Stream m a -> m ()
118mapM_ :: Monad m => Stream m a -> m ()
201mapM_ = S.mapM_ (\_ -> return ())
119mapM_ = S.mapM_ (\_ -> return ())
121+{-# INLINE toNull #-}
122+toNull :: Monad m => Stream m Int -> m ()
203{-# INLINE uncons #-}
125{-# INLINE uncons #-}
204uncons :: Monad m => Stream m Int -> m ()
126uncons :: Monad m => Stream m Int -> m ()
205uncons s = do
127uncons s = do
@@ -209,13 +131,13 @@ uncons s = do
209 Just (_, t) -> uncons t
131 Just (_, t) -> uncons t
211{-# INLINE init #-}
133{-# INLINE init #-}
212−init :: Monad m => Stream m a -> m ()
134+init :: (Monad m, S.IsStream t) => t m a -> m ()
213init s = do
135init s = do
214 t <- S.init s
136 t <- S.init s
215 P.mapM_ S.drain t
137 P.mapM_ S.drain t
217{-# INLINE tail #-}
139{-# INLINE tail #-}
218−tail :: Monad m => Stream m a -> m ()
140+tail :: (Monad m, S.IsStream t) => t m a -> m ()
219tail s = S.tail s >>= P.mapM_ tail
141tail s = S.tail s >>= P.mapM_ tail
221{-# INLINE nullTail #-}
143{-# INLINE nullTail #-}
@@ -234,9 +156,9 @@ headTail s = do
234toList :: Monad m => Stream m Int -> m [Int]
156toList :: Monad m => Stream m Int -> m [Int]
235toList = S.toList
157toList = S.toList
237−{-# INLINE foldl' #-}
159+{-# INLINE foldl #-}
238−foldl' :: Monad m => Stream m Int -> m Int
160+foldl :: Monad m => Stream m Int -> m Int
239−foldl' = S.foldl' (+) 0
161+foldl = S.foldl' (+) 0
241{-# INLINE last #-}
163{-# INLINE last #-}
242last :: Monad m => Stream m Int -> m (Maybe Int)
164last :: Monad m => Stream m Int -> m (Maybe Int)
@@ -246,36 +168,40 @@ last = S.last
246-- Transformation
168-- Transformation
247-------------------------------------------------------------------------------
169-------------------------------------------------------------------------------
171+{-# INLINE transform #-}
172+transform :: Monad m => Stream m a -> m ()
173+transform = runStream
249{-# INLINE composeN #-}
175{-# INLINE composeN #-}
251 :: Monad m
177 :: Monad m
252 => Int -> (Stream m Int -> Stream m Int) -> Stream m Int -> m ()
178 => Int -> (Stream m Int -> Stream m Int) -> Stream m Int -> m ()
253composeN n f =
179composeN n f =
254 case n of
180 case n of
255− 1 -> drain . f
181+ 1 -> transform . f
256− 2 -> drain . f . f
182+ 2 -> transform . f . f
257− 3 -> drain . f . f . f
183+ 3 -> transform . f . f . f
258− 4 -> drain . f . f . f . f
184+ 4 -> transform . f . f . f . f
259 _ -> undefined
185 _ -> undefined
261−{-# INLINE scanl' #-}
187+{-# INLINE scan #-}
262−scanl' :: Monad m => Int -> Stream m Int -> m ()
188+scan :: Monad m => Int -> Stream m Int -> m ()
263−scanl' n = composeN n $ S.scanl' (+) 0
189+scan n = composeN n $ S.scanl' (+) 0
265{-# INLINE map #-}
191{-# INLINE map #-}
266map :: Monad m => Int -> Stream m Int -> m ()
192map :: Monad m => Int -> Stream m Int -> m ()
267−map n = composeN n $ S.map (+ 1)
193+map n = composeN n $ P.fmap (+ 1)
269{-# INLINE fmapK #-}
195{-# INLINE fmapK #-}
270fmapK :: Monad m => Int -> Stream m Int -> m ()
196fmapK :: Monad m => Int -> Stream m Int -> m ()
271fmapK n = composeN n $ P.fmap (+ 1)
197fmapK n = composeN n $ P.fmap (+ 1)
273{-# INLINE mapM #-}
199{-# INLINE mapM #-}
274−mapM :: Monad m => Int -> Stream m Int -> m ()
200+mapM :: S.MonadAsync m => Int -> Stream m Int -> m ()
275−mapM n = composeN n $ S.mapMWith S.consM return
201+mapM n = composeN n $ S.mapM return
277{-# INLINE mapMSerial #-}
203{-# INLINE mapMSerial #-}
278−mapMSerial :: Monad m => Int -> Stream m Int -> m ()
204+mapMSerial :: S.MonadAsync m => Int -> Stream m Int -> m ()
279mapMSerial n = composeN n $ S.mapMSerial return
205mapMSerial n = composeN n $ S.mapMSerial return
281{-# INLINE filterEven #-}
207{-# INLINE filterEven #-}
@@ -284,11 +210,11 @@ filterEven n = composeN n $ S.filter even
285{-# INLINE filterAllOut #-}
211{-# INLINE filterAllOut #-}
286filterAllOut :: Monad m => Int -> Int -> Stream m Int -> m ()
212filterAllOut :: 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)
289{-# INLINE filterAllIn #-}
215{-# INLINE filterAllIn #-}
290filterAllIn :: Monad m => Int -> Int -> Stream m Int -> m ()
216filterAllIn :: 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)
293{-# INLINE _takeOne #-}
219{-# INLINE _takeOne #-}
294_takeOne :: Monad m => Int -> Stream m Int -> m ()
220_takeOne :: Monad m => Int -> Stream m Int -> m ()
@@ -296,11 +222,11 @@ _takeOne n = composeN n $ S.take 1
297{-# INLINE takeAll #-}
223{-# INLINE takeAll #-}
298takeAll :: Monad m => Int -> Int -> Stream m Int -> m ()
224takeAll :: 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
301{-# INLINE takeWhileTrue #-}
227{-# INLINE takeWhileTrue #-}
302takeWhileTrue :: Monad m => Int -> Int -> Stream m Int -> m ()
228takeWhileTrue :: 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)
305{-# INLINE dropOne #-}
231{-# INLINE dropOne #-}
306dropOne :: Monad m => Int -> Stream m Int -> m ()
232dropOne :: Monad m => Int -> Stream m Int -> m ()
@@ -308,11 +234,11 @@ dropOne n = composeN n $ S.drop 1
309{-# INLINE dropAll #-}
235{-# INLINE dropAll #-}
310dropAll :: Monad m => Int -> Int -> Stream m Int -> m ()
236dropAll :: 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
313{-# INLINE dropWhileTrue #-}
239{-# INLINE dropWhileTrue #-}
314dropWhileTrue :: Monad m => Int -> Int -> Stream m Int -> m ()
240dropWhileTrue :: 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)
317{-# INLINE dropWhileFalse #-}
243{-# INLINE dropWhileFalse #-}
318dropWhileFalse :: Monad m => Int -> Stream m Int -> m ()
244dropWhileFalse :: Monad m => Int -> Stream m Int -> m ()
@@ -327,8 +253,8 @@ foldlS :: Monad m => Int -> Stream m Int -> m ()
327foldlS n = composeN n $ S.foldlS (flip S.cons) S.nil
253foldlS n = composeN n $ S.foldlS (flip S.cons) S.nil
329{-# INLINE intersperse #-}
255{-# INLINE intersperse #-}
330−intersperse :: Monad m => Int -> Int -> Stream m Int -> m ()
256+intersperse :: S.MonadAsync m => Int -> Int -> Stream m Int -> m ()
331−intersperse streamLen n = composeN n $ S.intersperse streamLen
257+intersperse maxValue n = composeN n $ S.intersperse maxValue
333-------------------------------------------------------------------------------
259-------------------------------------------------------------------------------
334-- Iteration
260-- Iteration
@@ -336,67 +262,51 @@ intersperse streamLen n = composeN n $ S.intersperse streamLen
337{-# INLINE iterateSource #-}
263{-# INLINE iterateSource #-}
338iterateSource
264iterateSource
339− :: Monad m => Int -> (Stream m Int -> Stream m Int) -> Int -> Int -> Stream m Int
266+ => Int -> (Stream m Int -> Stream m Int) -> Int -> Int -> Stream m Int
340−iterateSource iterStreamLen g i n = f i (unfoldrM iterStreamLen n)
267+iterateSource iterStreamLen g i n = f i (sourceUnfoldrMN iterStreamLen n)
342 f (0 :: Int) m = g m
269 f (0 :: Int) m = g m
343 f x m = g (f (x P.- 1) m)
270 f x m = g (f (x P.- 1) m)
345-- this is quadratic
272-- this is quadratic
346{-# INLINE iterateScan #-}
273{-# INLINE iterateScan #-}
347−iterateScan :: Monad m => Int -> Int -> Int -> Stream m Int
274+iterateScan :: S.MonadAsync m => Int -> Int -> Int -> Stream m Int
348−iterateScan iterStreamLen maxIters =
349− iterateSource iterStreamLen (S.scanl' (+) 0) (maxIters `div` 10)
275+iterateScan iterStreamLen maxIters = iterateSource iterStreamLen (S.scanl' (+) 0) (maxIters `div` 10)
351-- this is quadratic
277-- this is quadratic
352{-# INLINE iterateDropWhileFalse #-}
278{-# INLINE iterateDropWhileFalse #-}
353−iterateDropWhileFalse :: Monad m => Int -> Int -> Int -> Int -> Stream m Int
279+iterateDropWhileFalse :: S.MonadAsync m => Int -> Int -> Int -> Int -> Stream m Int
354−iterateDropWhileFalse streamLen iterStreamLen maxIters =
280+iterateDropWhileFalse maxValue iterStreamLen maxIters =
355− iterateSource iterStreamLen (S.dropWhile (> streamLen)) (maxIters `div` 10)
281+ iterateSource iterStreamLen (S.dropWhile (> maxValue)) (maxIters `div` 10)
357{-# INLINE iterateMapM #-}
283{-# INLINE iterateMapM #-}
358−iterateMapM :: Monad m => Int -> Int -> Int -> Stream m Int
284+iterateMapM :: S.MonadAsync m => Int -> Int -> Int -> Stream m Int
359−iterateMapM iterStreamLen =
360− iterateSource iterStreamLen (S.mapMWith S.consM return)
285+iterateMapM iterStreamLen maxIters = iterateSource iterStreamLen (S.mapM return) maxIters
362{-# INLINE iterateFilterEven #-}
287{-# INLINE iterateFilterEven #-}
363−iterateFilterEven :: Monad m => Int -> Int -> Int -> Stream m Int
288+iterateFilterEven :: S.MonadAsync m => Int -> Int -> Int -> Stream m Int
364−iterateFilterEven iterStreamLen = iterateSource iterStreamLen (S.filter even)
289+iterateFilterEven iterStreamLen maxIters = iterateSource iterStreamLen (S.filter even) maxIters
366{-# INLINE iterateTakeAll #-}
291{-# INLINE iterateTakeAll #-}
367−iterateTakeAll :: Monad m => Int -> Int -> Int -> Int -> Stream m Int
292+iterateTakeAll :: S.MonadAsync m => Int -> Int -> Int -> Int -> Stream m Int
368−iterateTakeAll streamLen iterStreamLen =
369− iterateSource iterStreamLen (S.take streamLen)
293+iterateTakeAll maxValue iterStreamLen maxIters = iterateSource iterStreamLen (S.take maxValue) maxIters
371{-# INLINE iterateDropOne #-}
295{-# INLINE iterateDropOne #-}
372−iterateDropOne :: Monad m => Int -> Int -> Int -> Stream m Int
296+iterateDropOne :: S.MonadAsync m => Int -> Int -> Int -> Stream m Int
373−iterateDropOne iterStreamLen = iterateSource iterStreamLen (S.drop 1)
297+iterateDropOne iterStreamLen maxIters = iterateSource iterStreamLen (S.drop 1) maxIters
375{-# INLINE iterateDropWhileTrue #-}
299{-# INLINE iterateDropWhileTrue #-}
376−iterateDropWhileTrue ::
377− Monad m => Int -> Int -> Int -> Int -> Stream m Int
300+iterateDropWhileTrue :: S.MonadAsync m => Int -> Int -> Int -> Int -> Stream m Int
378−iterateDropWhileTrue streamLen iterStreamLen =
379− iterateSource iterStreamLen (S.dropWhile (<= streamLen))
301+iterateDropWhileTrue maxValue iterStreamLen maxIters = iterateSource iterStreamLen (S.dropWhile (<= maxValue)) maxIters
381-------------------------------------------------------------------------------
303-------------------------------------------------------------------------------
382-- Zipping
304-- Zipping
383-------------------------------------------------------------------------------
305-------------------------------------------------------------------------------
385−{-# INLINE zipWith #-}
307+{-# INLINE zip #-}
386−zipWith :: Monad m => Stream m Int -> m ()
308+zip :: Monad m => Stream m Int -> m ()
387−zipWith src = drain $ S.zipWith (,) src src
309+zip src = transform $ S.zipWith (,) src src
389−{-# INLINE zipWithM #-}
390−zipWithM :: Monad m => Stream m Int -> m ()
391−zipWithM src = drain $ S.zipWithM (curry return) src src
393−{-# INLINE sortByK #-}
394−sortByK :: (a -> a -> Ordering) -> Stream m a -> Stream m a
395−sortByK f = S.mergeMapWith (S.mergeBy f) S.fromPure
397−{-# INLINE sortBy #-}
398−sortBy :: Monad m => Stream m Int -> m ()
399−sortBy = drain . sortByK compare
401-------------------------------------------------------------------------------
311-------------------------------------------------------------------------------
402-- Mixed Composition
312-- Mixed Composition
@@ -416,31 +326,32 @@ dropScan n = composeN n $ S.scanl' (+) 0 . S.drop 1
417{-# INLINE takeDrop #-}
327{-# INLINE takeDrop #-}
418takeDrop :: Monad m => Int -> Int -> Stream m Int -> m ()
328takeDrop :: 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
421{-# INLINE takeScan #-}
331{-# INLINE takeScan #-}
422takeScan :: Monad m => Int -> Int -> Stream m Int -> m ()
332takeScan :: 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
425{-# INLINE takeMap #-}
335{-# INLINE takeMap #-}
426takeMap :: Monad m => Int -> Int -> Stream m Int -> m ()
336takeMap :: 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
429{-# INLINE filterDrop #-}
339{-# INLINE filterDrop #-}
430filterDrop :: Monad m => Int -> Int -> Stream m Int -> m ()
340filterDrop :: 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)
433{-# INLINE filterTake #-}
343{-# INLINE filterTake #-}
434filterTake :: Monad m => Int -> Int -> Stream m Int -> m ()
344filterTake :: 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)
347+-- Initially, this used maxBound not maxValue
437{-# INLINE filterScan #-}
348{-# INLINE filterScan #-}
438−filterScan :: Monad m => Int -> Stream m Int -> m ()
349+filterScan :: Monad m => Int -> Int -> Stream m Int -> m ()
439−filterScan n = composeN n $ S.scanl' (+) 0 . S.filter (<= maxBound)
350+filterScan maxValue n = composeN n $ S.scanl' (+) 0 . S.filter (<= maxValue)
441{-# INLINE filterMap #-}
352{-# INLINE filterMap #-}
442filterMap :: Monad m => Int -> Int -> Stream m Int -> m ()
353filterMap :: 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)
445-------------------------------------------------------------------------------
356-------------------------------------------------------------------------------
446-- ConcatMap
357-- ConcatMap
@@ -452,8 +363,8 @@ filterMap streamLen n = composeN n $ S.map (subtract 1) . S.filter (<= streamLen
452concatMap :: Int -> Int -> Int -> IO ()
363concatMap :: Int -> Int -> Int -> IO ()
453concatMap outer inner n =
364concatMap outer inner n =
454 S.drain $ S.concatMap
365 S.drain $ S.concatMap
455− (\_ -> unfoldrM inner n)
366+ (\_ -> sourceUnfoldrMN inner n)
456− (unfoldrM outer n)
367+ (sourceUnfoldrMN outer n)
458#ifdef INSPECTION
369#ifdef INSPECTION
459inspect $ hasNoTypeClasses 'concatMap
370inspect $ hasNoTypeClasses 'concatMap
@@ -465,8 +376,8 @@ inspect $ hasNoTypeClasses 'concatMap
465concatMapPure :: Int -> Int -> Int -> IO ()
376concatMapPure :: Int -> Int -> Int -> IO ()
466concatMapPure outer inner n =
377concatMapPure outer inner n =
467 S.drain $ S.concatMap
378 S.drain $ S.concatMap
468− (\_ -> unfoldr inner n)
379+ (\_ -> sourceUnfoldrN inner n)
469− (unfoldr outer n)
380+ (sourceUnfoldrN outer n)
471#ifdef INSPECTION
382#ifdef INSPECTION
472inspect $ hasNoTypeClasses 'concatMapPure
383inspect $ hasNoTypeClasses 'concatMapPure
@@ -477,7 +388,7 @@ inspect $ hasNoTypeClasses 'concatMapPure
477{-# INLINE concatMapRepl #-}
388{-# INLINE concatMapRepl #-}
478concatMapRepl :: Int -> Int -> Int -> IO ()
389concatMapRepl :: Int -> Int -> Int -> IO ()
479concatMapRepl outer inner n =
390concatMapRepl 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)
482#ifdef INSPECTION
393#ifdef INSPECTION
483inspect $ hasNoTypeClasses 'concatMapRepl
394inspect $ hasNoTypeClasses 'concatMapRepl
@@ -489,70 +400,44 @@ inspect $ hasNoTypeClasses 'concatMapRepl
489sourceConcatMapId :: Monad m
400sourceConcatMapId :: Monad m
490 => Int -> Int -> Stream m (Stream m Int)
401 => Int -> Int -> Stream m (Stream m Int)
491sourceConcatMapId val n =
402sourceConcatMapId val n =
492− S.fromFoldable $ fmap (S.fromEffect . return) [n..n+val]
403+ S.fromFoldable $ fmap (S.yieldM . return) [n..n+val]
494−{-# INLINE concatMapBySerial #-}
405+{-# INLINE concatStreamsWith #-}
495−concatMapBySerial :: Int -> Int -> Int -> IO ()
406+concatStreamsWith :: Int -> Int -> Int -> IO ()
496−concatMapBySerial outer inner n =
407+concatStreamsWith outer inner n =
497− S.drain $ S.concatMapWith S.append
408+ S.drain $ S.concatMapBy S.serial
498− (unfoldrM inner)
409+ (sourceUnfoldrMN inner)
499− (unfoldrM outer n)
410+ (sourceUnfoldrMN outer n)
501-------------------------------------------------------------------------------
412-------------------------------------------------------------------------------
502-- Nested Composition
413-- Nested Composition
503-------------------------------------------------------------------------------
414-------------------------------------------------------------------------------
505−instance Monad m => Applicative (S.StreamK m) where
506− {-# INLINE pure #-}
416+{-# INLINE toNullApNested #-}
509− {-# INLINE (<*>) #-}
510− (<*>) = S.crossApply
512− {-# INLINE liftA2 #-}
513− liftA2 f x = (<*>) (fmap f x)
516− (*>) = S.crossApplySnd
519− (<*) = S.crossApplyFst
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
524−instance Monad m => Monad (S.StreamK m) where
525− {-# INLINE return #-}
528− {-# INLINE (>>=) #-}
529− (>>=) = flip S.concatMap
531−{-# INLINE drainApplicative #-}
532−drainApplicative :: Monad m => Stream m Int -> m ()
417+toNullApNested :: Monad m => Stream m Int -> m ()
533−drainApplicative s = drain $ do
418+toNullApNested s = runStream $ do
534 (+) <$> s <*> s
419 (+) <$> s <*> s
536−{-# INLINE drainMonad #-}
421+{-# INLINE toNullNested #-}
537−drainMonad :: Monad m => Stream m Int -> m ()
422+toNullNested :: Monad m => Stream m Int -> m ()
538−drainMonad s = drain $ do
423+toNullNested s = runStream $ do
541 return $ x + y
426 return $ x + y
543−{-# INLINE drainMonad3 #-}
428+{-# INLINE toNullNested3 #-}
544−drainMonad3 :: Monad m => Stream m Int -> m ()
429+toNullNested3 :: Monad m => Stream m Int -> m ()
545−drainMonad3 s = drain $ do
430+toNullNested3 s = runStream $ do
549 return $ x + y + z
434 return $ x + y + z
551−{-# INLINE filterAllOutMonad #-}
436+{-# INLINE filterAllOutNested #-}
552−filterAllOutMonad
437+filterAllOutNested
553 :: Monad m
438 :: Monad m
554 => Stream m Int -> m ()
439 => Stream m Int -> m ()
555−filterAllOutMonad str = drain $ do
440+filterAllOutNested str = runStream $ do
558 let s = x + y
443 let s = x + y
@@ -560,11 +445,11 @@ filterAllOutMonad str = drain $ do
560 then return s
445 then return s
561 else S.nil
446 else S.nil
563−{-# INLINE filterAllInMonad #-}
448+{-# INLINE filterAllInNested #-}
564−filterAllInMonad
449+filterAllInNested
565 :: Monad m
450 :: Monad m
566 => Stream m Int -> m ()
451 => Stream m Int -> m ()
567−filterAllInMonad str = drain $ do
452+filterAllInNested str = runStream $ do
570 let s = x + y
455 let s = x + y
@@ -576,14 +461,9 @@ filterAllInMonad str = drain $ do
576-- Nested Composition Pure lists
461-- Nested Composition Pure lists
577-------------------------------------------------------------------------------
462-------------------------------------------------------------------------------
579−-- There are several list benchmarks here for comparison with lists. It is easy
464+{-# INLINE sourceUnfoldrList #-}
580−-- and convenient to see the comparisons when they are here, otherwise we'll
465+sourceUnfoldrList :: Int -> Int -> [Int]
581−-- have to add a separate module for list benchmarks with the same names and
582−-- then add a comparison in bench.sh.
584−{-# INLINE unfoldrList #-}
585−unfoldrList :: Int -> Int -> [Int]
586−unfoldrList maxval n = List.unfoldr step n
466+sourceUnfoldrList maxval n = List.unfoldr step n
588 step cnt =
468 step cnt =
589 if cnt > n + maxval
469 if cnt > n + maxval
@@ -634,301 +514,298 @@ filterAllInNestedList str = do
634-------------------------------------------------------------------------------
514-------------------------------------------------------------------------------
636moduleName :: String
516moduleName :: String
637−moduleName = "Data.StreamK"
517+moduleName = "Data.Stream.StreamK"
639o_1_space_generation :: Int -> Benchmark
519o_1_space_generation :: Int -> Benchmark
640−o_1_space_generation streamLen =
520+o_1_space_generation value =
641 bgroup "generation"
521 bgroup "generation"
642− [ benchFold "unfoldr" drain (unfoldr streamLen)
522+ [ benchFold "unfoldr" toNull (sourceUnfoldr value)
643− , benchFold "unfoldrM" drain (unfoldrM streamLen)
523+ , benchFold "unfoldrM" toNull (sourceUnfoldrM value)
644− , benchFold "repeat" drain (repeat streamLen)
524+
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)
651− , benchFold "fromFoldable" drain (fromFoldable streamLen)
525+ , benchFold "fromFoldable" toNull (sourceFromFoldable value)
652− , benchFold "fromFoldableM" drain (fromFoldableM streamLen)
526+ , benchFold "fromFoldableM" toNull (sourceFromFoldableM value)
654 -- appends
528 -- appends
655− , benchFold "concatMapFoldableWith" drain (concatMapFoldableSerial streamLen)
529+ , benchFold "concatMapFoldableWith" toNull (sourceFoldMapWith value)
656− , benchFold "concatMapFoldableWithM" drain (concatMapFoldableSerialM streamLen)
530+ , benchFold "concatMapFoldableWithM" toNull (sourceFoldMapWithM value)
659o_1_space_elimination :: Int -> Benchmark
533o_1_space_elimination :: Int -> Benchmark
660−o_1_space_elimination streamLen =
534+o_1_space_elimination value =
661 bgroup "elimination"
535 bgroup "elimination"
662− [ benchFold "toNull" drain (unfoldrM streamLen)
536+ [ benchFold "toNull" toNull (sourceUnfoldrM value)
663− , benchFold "mapM_" mapM_ (unfoldrM streamLen)
537+ , benchFold "mapM_" mapM_ (sourceUnfoldrM value)
664− , benchFold "uncons" uncons (unfoldrM streamLen)
538+ , benchFold "uncons" uncons (sourceUnfoldrM value)
665− , benchFold "init" init (unfoldrM streamLen)
539+ , benchFold "init" init (sourceUnfoldrM value)
666− , benchFold "foldl'" foldl' (unfoldrM streamLen)
540+ , benchFold "foldl'" foldl (sourceUnfoldrM value)
667− , benchFold "last" last (unfoldrM streamLen)
541+ , benchFold "last" last (sourceUnfoldrM value)
670o_1_space_nested :: Int -> Benchmark
544o_1_space_nested :: Int -> Benchmark
671−o_1_space_nested streamLen =
545+o_1_space_nested value =
672 bgroup "nested"
546 bgroup "nested"
673− [ benchFold "drainApplicative" drainApplicative (unfoldrM streamLen2)
547+ [ benchFold "toNullAp" toNullApNested (sourceUnfoldrMN value2)
674− , benchFold "drainMonad" drainMonad (unfoldrM streamLen2)
548+ , benchFold "toNull" toNullNested (sourceUnfoldrMN value2)
675− , benchFold "drainMonad3" drainMonad3 (unfoldrM streamLen3)
549+ , benchFold "toNull3" toNullNested3 (sourceUnfoldrMN value3)
676− , benchFold "filterAllInMonad" filterAllInMonad (unfoldrM streamLen2)
550+ , benchFold "filterAllIn" filterAllInNested (sourceUnfoldrMN value2)
677− , benchFold "filterAllOutMonad" filterAllOutMonad (unfoldrM streamLen2)
551+ , benchFold "filterAllOut" filterAllOutNested (sourceUnfoldrMN value2)
678− , benchFold "drainApplicative (pure)" drainApplicative (unfoldr streamLen2)
552+ , benchFold "toNullApPure" toNullApNested (sourceUnfoldrN value2)
679− , benchFold "drainMonad (pure)" drainMonad (unfoldr streamLen2)
553+ , benchFold "toNullPure" toNullNested (sourceUnfoldrN value2)
680− , benchFold "drainMonad3 (pure)" drainMonad3 (unfoldr streamLen3)
554+ , benchFold "toNull3Pure" toNullNested3 (sourceUnfoldrN value3)
681− , benchFold "filterAllInMonad (pure)" filterAllInMonad (unfoldr streamLen2)
555+ , benchFold "filterAllInPure" filterAllInNested (sourceUnfoldrN value2)
682− , benchFold "filterAllOutMonad (pure)" filterAllOutMonad (unfoldr streamLen2)
556+ , benchFold "filterAllOutPure" filterAllOutNested (sourceUnfoldrN value2)
685− streamLen2 = round (P.fromIntegral streamLen**(1/2::P.Double)) -- double nested loop
559+ value2 = round (P.fromIntegral value**(1/2::P.Double)) -- double nested loop
686− streamLen3 = round (P.fromIntegral streamLen**(1/3::P.Double)) -- triple nested loop
560+ value3 = round (P.fromIntegral value**(1/3::P.Double)) -- triple nested loop
688o_1_space_transformation :: Int -> Benchmark
562o_1_space_transformation :: Int -> Benchmark
689−o_1_space_transformation streamLen =
563+o_1_space_transformation value =
690 bgroup "transformation"
564 bgroup "transformation"
691− [ benchFold "foldrS" (foldrS 1) (unfoldrM streamLen)
566+ [ benchFold "foldrS" (foldrS 1) (sourceUnfoldrM value)
692− , benchFold "scanl'" (scanl' 1) (unfoldrM streamLen)
567+ , benchFold "scan" (scan 1) (sourceUnfoldrM value)
693− , benchFold "map" (map 1) (unfoldrM streamLen)
568+ , benchFold "map" (map 1) (sourceUnfoldrM value)
694− , benchFold "fmap" (fmapK 1) (unfoldrM streamLen)
569+ , benchFold "fmap" (fmapK 1) (sourceUnfoldrM value)
695− , benchFold "mapM" (mapM 1) (unfoldrM streamLen)
570+ , benchFold "mapM" (mapM 1) (sourceUnfoldrM value)
696− , benchFold "mapMSerial" (mapMSerial 1) (unfoldrM streamLen)
571+ , benchFold "mapMSerial" (mapMSerial 1) (sourceUnfoldrM value)
699−o_1_space_transformationX4 :: Int -> Benchmark
700−o_1_space_transformationX4 streamLen =
701− bgroup "transformationX4"
573+ , bgroup "X4"
702− [ benchFold "scanl'" (scanl' 4) (unfoldrM streamLen)
574+ [ benchFold "scan" (scan 4) (sourceUnfoldrM value)
703− , benchFold "map" (map 4) (unfoldrM streamLen)
575+ , benchFold "map" (map 4) (sourceUnfoldrM value)
704− , benchFold "fmap" (fmapK 4) (unfoldrM streamLen)
576+ , benchFold "fmap" (fmapK 4) (sourceUnfoldrM value)
705− , benchFold "mapM" (mapM 4) (unfoldrM streamLen)
577+ , benchFold "mapM" (mapM 4) (sourceUnfoldrM value)
706− , benchFold "mapMSerial" (mapMSerial 4) (unfoldrM streamLen)
578+ , benchFold "mapMSerial" (mapMSerial 4) (sourceUnfoldrM value)
707 -- XXX this is horribly slow
579 -- XXX this is horribly slow
708− -- , benchFold "concatMap" (concatMap 4) (unfoldrM streamLen16)
580+ -- , benchFold "concatMap" (concatMap 4) (sourceUnfoldrMN value16)
711o_1_space_concat :: Int -> Benchmark
584o_1_space_concat :: Int -> Benchmark
712−o_1_space_concat streamLen =
585+o_1_space_concat value =
713 bgroup "concat"
586 bgroup "concat"
714 [ benchIOSrc1 "concatMapPure (n of 1)"
587 [ benchIOSrc1 "concatMapPure (n of 1)"
715− (concatMapPure streamLen 1)
588+ (concatMapPure value 1)
716 , benchIOSrc1 "concatMapPure (sqrt n of sqrt n)"
589 , benchIOSrc1 "concatMapPure (sqrt n of sqrt n)"
717− (concatMapPure streamLen2 streamLen2)
590+ (concatMapPure value2 value2)
718 , benchIOSrc1 "concatMapPure (1 of n)"
591 , benchIOSrc1 "concatMapPure (1 of n)"
719− (concatMapPure 1 streamLen)
592+ (concatMapPure 1 value)
721 , benchIOSrc1 "concatMap (n of 1)"
594 , benchIOSrc1 "concatMap (n of 1)"
722− (concatMap streamLen 1)
595+ (concatMap value 1)
723 , benchIOSrc1 "concatMap (sqrt n of sqrt n)"
596 , benchIOSrc1 "concatMap (sqrt n of sqrt n)"
724− (concatMap streamLen2 streamLen2)
597+ (concatMap value2 value2)
725 , benchIOSrc1 "concatMap (1 of n)"
598 , benchIOSrc1 "concatMap (1 of n)"
726− (concatMap 1 streamLen)
599+ (concatMap 1 value)
728 , benchIOSrc1 "concatMapRepl (sqrt n of sqrt n)"
601 , benchIOSrc1 "concatMapRepl (sqrt n of sqrt n)"
729− (concatMapRepl streamLen2 streamLen2)
602+ (concatMapRepl value2 value2)
731 -- This is for comparison with concatMapFoldableWith
604 -- This is for comparison with concatMapFoldableWith
732 , benchIOSrc1 "concatMapWithId (n of 1) (fromFoldable)"
605 , benchIOSrc1 "concatMapWithId (n of 1) (fromFoldable)"
734− . S.concatMapWith S.append id
735− . sourceConcatMapId streamLen)
606+ (S.drain . S.concatMapBy S.serial id . sourceConcatMapId value)
737− , benchIOSrc1 "concatMapBy serial (n of 1)"
608+ , benchIOSrc1 "concatMapWith (n of 1)"
738− (concatMapBySerial streamLen 1)
609+ (concatStreamsWith value 1)
739− , benchIOSrc1 "concatMapBy serial (sqrt n of sqrt n)"
610+ , benchIOSrc1 "concatMapWith (sqrt n of sqrt n)"
740− (concatMapBySerial streamLen2 streamLen2)
611+ (concatStreamsWith value2 value2)
741− , benchIOSrc1 "concatMapBy serial (1 of n)"
612+ , benchIOSrc1 "concatMapWith (1 of n)"
742− (concatMapBySerial 1 streamLen)
613+ (concatStreamsWith 1 value)
743− , benchFold "sortBy" sortBy (unfoldrM streamLen)
746− streamLen2 = round (P.fromIntegral streamLen**(1/2::P.Double)) -- double nested loop
616+ value2 = round (P.fromIntegral value**(1/2::P.Double)) -- double nested loop
748o_1_space_filtering :: Int -> Benchmark
618o_1_space_filtering :: Int -> Benchmark
749−o_1_space_filtering streamLen =
619+o_1_space_filtering value =
750 bgroup "filtering"
620 bgroup "filtering"
751− [ benchFold "filter-even" (filterEven 1) (unfoldrM streamLen)
622+ [ benchFold "filter-even" (filterEven 1) (sourceUnfoldrM value)
752− , benchFold "filter-all-out" (filterAllOut streamLen 1) (unfoldrM streamLen)
623+ , benchFold "filter-all-out" (filterAllOut maxValue 1) (sourceUnfoldrM value)
753− , benchFold "filter-all-in" (filterAllIn streamLen 1) (unfoldrM streamLen)
624+ , benchFold "filter-all-in" (filterAllIn maxValue 1) (sourceUnfoldrM value)
754− , benchFold "take-all" (takeAll streamLen 1) (unfoldrM streamLen)
625+ , benchFold "take-all" (takeAll maxValue 1) (sourceUnfoldrM value)
755− , benchFold "takeWhile-true" (takeWhileTrue streamLen 1) (unfoldrM streamLen)
626+ , benchFold "takeWhile-true" (takeWhileTrue maxValue 1) (sourceUnfoldrM value)
756− , benchFold "drop-one" (dropOne 1) (unfoldrM streamLen)
627+ , benchFold "drop-one" (dropOne 1) (sourceUnfoldrM value)
757− , benchFold "drop-all" (dropAll streamLen 1) (unfoldrM streamLen)
628+ , benchFold "drop-all" (dropAll maxValue 1) (sourceUnfoldrM value)
758− , benchFold "dropWhile-true" (dropWhileTrue streamLen 1) (unfoldrM streamLen)
629+ , benchFold "dropWhile-true" (dropWhileTrue maxValue 1) (sourceUnfoldrM value)
759− , benchFold "dropWhile-false" (dropWhileFalse 1) (unfoldrM streamLen)
630+ , benchFold "dropWhile-false" (dropWhileFalse 1) (sourceUnfoldrM value)
762−o_1_space_filteringX4 :: Int -> Benchmark
763−o_1_space_filteringX4 streamLen =
764− bgroup "filteringX4"
632+ , bgroup "X4"
765− [ benchFold "filter-even" (filterEven 4) (unfoldrM streamLen)
633+ [ benchFold "filter-even" (filterEven 4) (sourceUnfoldrM value)
766− , benchFold "filter-all-out" (filterAllOut streamLen 4) (unfoldrM streamLen)
634+ , benchFold "filter-all-out" (filterAllOut maxValue 4) (sourceUnfoldrM value)
767− , benchFold "filter-all-in" (filterAllIn streamLen 4) (unfoldrM streamLen)
635+ , benchFold "filter-all-in" (filterAllIn maxValue 4) (sourceUnfoldrM value)
768− , benchFold "take-all" (takeAll streamLen 4) (unfoldrM streamLen)
636+ , benchFold "take-all" (takeAll maxValue 4) (sourceUnfoldrM value)
769− , benchFold "takeWhile-true" (takeWhileTrue streamLen 4) (unfoldrM streamLen)
637+ , benchFold "takeWhile-true" (takeWhileTrue maxValue 4) (sourceUnfoldrM value)
770− , benchFold "drop-one" (dropOne 4) (unfoldrM streamLen)
638+ , benchFold "drop-one" (dropOne 4) (sourceUnfoldrM value)
771− , benchFold "drop-all" (dropAll streamLen 4) (unfoldrM streamLen)
639+ , benchFold "drop-all" (dropAll maxValue 4) (sourceUnfoldrM value)
772− , benchFold "dropWhile-true" (dropWhileTrue streamLen 4) (unfoldrM streamLen)
640+ , benchFold "dropWhile-true" (dropWhileTrue maxValue 4) (sourceUnfoldrM value)
773− , benchFold "dropWhile-false" (dropWhileFalse 4) (unfoldrM streamLen)
641+ , benchFold "dropWhile-false" (dropWhileFalse 4) (sourceUnfoldrM value)
776o_1_space_zipping :: Int -> Benchmark
649o_1_space_zipping :: Int -> Benchmark
777−o_1_space_zipping streamLen =
650+o_1_space_zipping value =
778 bgroup "zipping"
651 bgroup "zipping"
779− [ benchFold "zipWith" zipWith (unfoldrM streamLen)
652+ [ benchFold "zip" zip (sourceUnfoldrM value)
780− , benchFold "zipWithM" zipWithM (unfoldrM streamLen)
783o_1_space_mixed :: Int -> Benchmark
655o_1_space_mixed :: Int -> Benchmark
784−o_1_space_mixed streamLen =
656+o_1_space_mixed value =
785 bgroup "mixed"
657 bgroup "mixed"
786− [ benchFold "scan-map" (scanMap 1) (unfoldrM streamLen)
659+ [ benchFold "scan-map" (scanMap 1) (sourceUnfoldrM value)
787− , benchFold "drop-map" (dropMap 1) (unfoldrM streamLen)
660+ , benchFold "drop-map" (dropMap 1) (sourceUnfoldrM value)
788− , benchFold "drop-scan" (dropScan 1) (unfoldrM streamLen)
661+ , benchFold "drop-scan" (dropScan 1) (sourceUnfoldrM value)
789− , benchFold "take-drop" (takeDrop streamLen 1) (unfoldrM streamLen)
662+ , benchFold "take-drop" (takeDrop maxValue 1) (sourceUnfoldrM value)
790− , benchFold "take-scan" (takeScan streamLen 1) (unfoldrM streamLen)
663+ , benchFold "take-scan" (takeScan maxValue 1) (sourceUnfoldrM value)
791− , benchFold "take-map" (takeMap streamLen 1) (unfoldrM streamLen)
664+ , benchFold "take-map" (takeMap maxValue 1) (sourceUnfoldrM value)
792− , benchFold "filter-drop" (filterDrop streamLen 1) (unfoldrM streamLen)
665+ , benchFold "filter-drop" (filterDrop maxValue 1) (sourceUnfoldrM value)
793− , benchFold "filter-take" (filterTake streamLen 1) (unfoldrM streamLen)
666+ , benchFold "filter-take" (filterTake maxValue 1) (sourceUnfoldrM value)
794− , benchFold "filter-scan" (filterScan 1) (unfoldrM streamLen)
667+ , benchFold "filter-scan" (filterScan maxValue 1) (sourceUnfoldrM value)
795− , benchFold "filter-map" (filterMap streamLen 1) (unfoldrM streamLen)
668+ , benchFold "filter-map" (filterMap maxValue 1) (sourceUnfoldrM value)
798−o_1_space_mixedX2 :: Int -> Benchmark
799−o_1_space_mixedX2 streamLen =
800− bgroup "mixedX2"
670+ , bgroup "X2"
801− [ benchFold "scan-map" (scanMap 2) (unfoldrM streamLen)
671+ [ benchFold "scan-map" (scanMap 2) (sourceUnfoldrM value)
802− , benchFold "drop-map" (dropMap 2) (unfoldrM streamLen)
672+ , benchFold "drop-map" (dropMap 2) (sourceUnfoldrM value)
803− , benchFold "drop-scan" (dropScan 2) (unfoldrM streamLen)
673+ , benchFold "drop-scan" (dropScan 2) (sourceUnfoldrM value)
804− , benchFold "take-drop" (takeDrop streamLen 2) (unfoldrM streamLen)
674+ , benchFold "take-drop" (takeDrop maxValue 2) (sourceUnfoldrM value)
805− , benchFold "take-scan" (takeScan streamLen 2) (unfoldrM streamLen)
675+ , benchFold "take-scan" (takeScan maxValue 2) (sourceUnfoldrM value)
806− , benchFold "take-map" (takeMap streamLen 2) (unfoldrM streamLen)
676+ , benchFold "take-map" (takeMap maxValue 2) (sourceUnfoldrM value)
807− , benchFold "filter-drop" (filterDrop streamLen 2) (unfoldrM streamLen)
677+ , benchFold "filter-drop" (filterDrop maxValue 2) (sourceUnfoldrM value)
808− , benchFold "filter-take" (filterTake streamLen 2) (unfoldrM streamLen)
678+ , benchFold "filter-take" (filterTake maxValue 2) (sourceUnfoldrM value)
809− , benchFold "filter-scan" (filterScan 2) (unfoldrM streamLen)
679+ , benchFold "filter-scan" (filterScan maxValue 2) (sourceUnfoldrM value)
810− , benchFold "filter-map" (filterMap streamLen 2) (unfoldrM streamLen)
680+ , benchFold "filter-map" (filterMap maxValue 2) (sourceUnfoldrM value)
813−o_1_space_mixedX4 :: Int -> Benchmark
814−o_1_space_mixedX4 streamLen =
815− bgroup "mixedX4"
682+ , bgroup "X4"
816− [ benchFold "scan-map" (scanMap 4) (unfoldrM streamLen)
683+ [ benchFold "scan-map" (scanMap 4) (sourceUnfoldrM value)
817− , benchFold "drop-map" (dropMap 4) (unfoldrM streamLen)
684+ , benchFold "drop-map" (dropMap 4) (sourceUnfoldrM value)
818− , benchFold "drop-scan" (dropScan 4) (unfoldrM streamLen)
685+ , benchFold "drop-scan" (dropScan 4) (sourceUnfoldrM value)
819− , benchFold "take-drop" (takeDrop streamLen 4) (unfoldrM streamLen)
686+ , benchFold "take-drop" (takeDrop maxValue 4) (sourceUnfoldrM value)
820− , benchFold "take-scan" (takeScan streamLen 4) (unfoldrM streamLen)
687+ , benchFold "take-scan" (takeScan maxValue 4) (sourceUnfoldrM value)
821− , benchFold "take-map" (takeMap streamLen 4) (unfoldrM streamLen)
688+ , benchFold "take-map" (takeMap maxValue 4) (sourceUnfoldrM value)
822− , benchFold "filter-drop" (filterDrop streamLen 4) (unfoldrM streamLen)
689+ , benchFold "filter-drop" (filterDrop maxValue 4) (sourceUnfoldrM value)
823− , benchFold "filter-take" (filterTake streamLen 4) (unfoldrM streamLen)
690+ , benchFold "filter-take" (filterTake maxValue 4) (sourceUnfoldrM value)
824− , benchFold "filter-scan" (filterScan 4) (unfoldrM streamLen)
691+ , benchFold "filter-scan" (filterScan maxValue 4) (sourceUnfoldrM value)
825− , benchFold "filter-map" (filterMap streamLen 4) (unfoldrM streamLen)
692+ , benchFold "filter-map" (filterMap maxValue 4) (sourceUnfoldrM value)
828o_1_space_list :: Int -> Benchmark
699o_1_space_list :: Int -> Benchmark
829−o_1_space_list streamLen =
700+o_1_space_list value =
830 bgroup "list"
701 bgroup "list"
831 [ bgroup "elimination"
702 [ bgroup "elimination"
832− [ benchList "last" (\xs -> [List.last xs]) (unfoldrList streamLen)
703+ [ benchList "last" (\xs -> [List.last xs]) (sourceUnfoldrList value)
834 , bgroup "nested"
705 , bgroup "nested"
835− [ benchList "toNullAp" toNullApNestedList (unfoldrList streamLen2)
706+ [ benchList "toNullAp" toNullApNestedList (sourceUnfoldrList value2)
836− , benchList "toNull" toNullNestedList (unfoldrList streamLen2)
707+ , benchList "toNull" toNullNestedList (sourceUnfoldrList value2)
837− , benchList "toNull3" toNullNestedList3 (unfoldrList streamLen3)
708+ , benchList "toNull3" toNullNestedList3 (sourceUnfoldrList value3)
838− , benchList "filterAllIn" filterAllInNestedList (unfoldrList streamLen2)
709+ , benchList "filterAllIn" filterAllInNestedList (sourceUnfoldrList value2)
839− , benchList "filterAllOut" filterAllOutNestedList (unfoldrList streamLen2)
710+ , benchList "filterAllOut" filterAllOutNestedList (sourceUnfoldrList value2)
844− streamLen2 = round (P.fromIntegral streamLen**(1/2::P.Double)) -- double nested loop
715+ value2 = round (P.fromIntegral value**(1/2::P.Double)) -- double nested loop
845− streamLen3 = round (P.fromIntegral streamLen**(1/3::P.Double)) -- triple nested loop
716+ value3 = round (P.fromIntegral value**(1/3::P.Double)) -- triple nested loop
847−o_1_space :: Int -> Benchmark
718+o_1_space :: Benchmark
848−o_1_space streamLen =
719+o_1_space =
849 bgroup (o_1_space_prefix moduleName)
720 bgroup (o_1_space_prefix moduleName)
850− [ o_1_space_generation streamLen
721+ [ o_1_space_generation value
851− , o_1_space_elimination streamLen
722+ , o_1_space_elimination value
852− , o_1_space_nested streamLen
723+ , o_1_space_nested value
853− , o_1_space_transformation streamLen
724+ , o_1_space_transformation value
854− , o_1_space_transformationX4 streamLen
855− , o_1_space_concat streamLen
725+ , o_1_space_concat value
856− , o_1_space_filtering streamLen
726+ , o_1_space_filtering value
857− , o_1_space_filteringX4 streamLen
858− , o_1_space_zipping streamLen
727+ , o_1_space_zipping value
859− , o_1_space_mixed streamLen
728+ , o_1_space_mixed value
860− , o_1_space_mixedX2 streamLen
861− , o_1_space_mixedX4 streamLen
862− , o_1_space_list streamLen
729+ , o_1_space_list value
865−o_n_heap :: Int -> Benchmark
734+o_n_heap :: [Benchmark]
866−o_n_heap streamLen =
735+o_n_heap =
867− bgroup (o_n_heap_prefix moduleName)
736+ [ bgroup (o_n_heap_prefix moduleName)
868 [ bgroup "transformation"
737 [ bgroup "transformation"
869− [ benchFold "foldlS" (foldlS 1) (unfoldrM streamLen)
738+ [ benchFold "foldlS" (foldlS 1) (sourceUnfoldrM value)
873{-# INLINE benchK #-}
746{-# INLINE benchK #-}
874benchK :: P.String -> (Int -> Stream P.IO Int) -> Benchmark
747benchK :: P.String -> (Int -> Stream P.IO Int) -> Benchmark
875−benchK name f = bench name $ nfIO $ randomRIO (1,1) >>= drain . f
748+benchK name f = bench name $ nfIO $ randomRIO (1,1) >>= toNull . f
877−o_n_stack :: Int -> Int -> Int -> Benchmark
750+o_n_stack :: [Benchmark]
878−o_n_stack streamLen iterStreamLen maxIters =
751+o_n_stack =
879− bgroup (o_n_stack_prefix moduleName)
752+ [ bgroup (o_n_stack_prefix moduleName)
880 [ bgroup "elimination"
753 [ bgroup "elimination"
881− [ benchFold "tail" tail (unfoldrM streamLen)
754+ [ benchFold "tail" tail (sourceUnfoldrM value)
882− , benchFold "nullTail" nullTail (unfoldrM streamLen)
755+ , benchFold "nullTail" nullTail (sourceUnfoldrM value)
883− , benchFold "headTail" headTail (unfoldrM streamLen)
756+ , benchFold "headTail" headTail (sourceUnfoldrM value)
885 , bgroup "transformation"
758 , bgroup "transformation"
887 -- XXX why do these need so much stack
760 -- XXX why do these need so much stack
888− benchFold "intersperse" (intersperse streamLen 1) (unfoldrM streamLen2)
761+ benchFold "intersperse" (intersperse maxValue 1) (sourceUnfoldrMN value2)
889− , benchFold "interspersePure" (intersperse streamLen 1) (unfoldr streamLen2)
762+ , benchFold "interspersePure" (intersperse maxValue 1) (sourceUnfoldrN value2)
891 , bgroup "transformationX4"
764 , bgroup "transformationX4"
893− benchFold "intersperse" (intersperse streamLen 4) (unfoldrM streamLen16)
766+ benchFold "intersperse" (intersperse maxValue 4) (sourceUnfoldrMN value16)
895 , bgroup "iterated"
768 , bgroup "iterated"
896 [ benchK "mapM" (iterateMapM iterStreamLen maxIters)
769 [ benchK "mapM" (iterateMapM iterStreamLen maxIters)
897 , benchK "scan(1/10)" (iterateScan iterStreamLen maxIters)
770 , benchK "scan(1/10)" (iterateScan iterStreamLen maxIters)
898 , benchK "filterEven" (iterateFilterEven iterStreamLen maxIters)
771 , benchK "filterEven" (iterateFilterEven iterStreamLen maxIters)
899− , benchK "takeAll" (iterateTakeAll streamLen iterStreamLen maxIters)
772+ , benchK "takeAll" (iterateTakeAll maxValue iterStreamLen maxIters)
900 , benchK "dropOne" (iterateDropOne iterStreamLen maxIters)
773 , benchK "dropOne" (iterateDropOne iterStreamLen maxIters)
901− , benchK "dropWhileFalse(1/10)" (iterateDropWhileFalse streamLen iterStreamLen maxIters)
774+ , benchK "dropWhileFalse(1/10)" (iterateDropWhileFalse maxValue iterStreamLen maxIters)
902− , benchK "dropWhileTrue" (iterateDropWhileTrue streamLen iterStreamLen maxIters)
775+ , benchK "dropWhileTrue" (iterateDropWhileTrue maxValue iterStreamLen maxIters)
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
782+ value2 = round (P.fromIntegral value**(1/2::P.Double)) -- double nested loop
783+ value16 = round (P.fromIntegral value**(1/16::P.Double)) -- triple nested loop
909−o_n_space :: Int -> Benchmark
788+o_n_space :: [Benchmark]
910−o_n_space streamLen =
789+o_n_space =
911− bgroup (o_n_space_prefix moduleName)
790+ [ bgroup (o_n_space_prefix moduleName)
912 [ bgroup "elimination"
791 [ bgroup "elimination"
913− [ benchFold "toList" toList (unfoldrM streamLen)
792+ [ benchFold "toList" toList (sourceUnfoldrM value)
917{-# INLINE benchList #-}
801{-# INLINE benchList #-}
918benchList :: P.String -> ([Int] -> [Int]) -> (Int -> [Int]) -> Benchmark
802benchList :: P.String -> ([Int] -> [Int]) -> (Int -> [Int]) -> Benchmark
919benchList name run f = bench name $ nfIO $ randomRIO (1,1) >>= return . run . f
803benchList name run f = bench name $ nfIO $ randomRIO (1,1) >>= return . run . f
921main :: IO ()
805main :: IO ()
923− defaultMain
806+main = defaultMain $ concat
924− [ o_1_space streamLen
807+ [ [ o_1_space ]
925− , o_n_stack streamLen iterStreamLen maxIters
808+ , o_n_stack
926− , o_n_heap streamLen
809+ , o_n_heap
927− , o_n_space streamLen
810+ , o_n_space