on master help sign in
Add mapM_ benchmark, add build/augment
9c8a750Harendra Kumar
benchmark/BaseStreams.hs+1
@@ -165,6 +165,7 @@ main =
165 ]
165 ]
166 , bgroup "elimination"
166 , bgroup "elimination"
167 [ benchIO "toNull" K.toNull K.sourceUnfoldrM
167 [ benchIO "toNull" K.toNull K.sourceUnfoldrM
168+ , benchIO "mapM_" K.mapM_ K.sourceUnfoldrM
168 , benchIO "uncons" K.uncons K.sourceUnfoldrM
169 , benchIO "uncons" K.uncons K.sourceUnfoldrM
169 , benchFold "init" K.init K.sourceUnfoldrM
170 , benchFold "init" K.init K.sourceUnfoldrM
170 , benchFold "tail" K.tail K.sourceUnfoldrM
171 , benchFold "tail" K.tail K.sourceUnfoldrM
benchmark/NanoBenchmarks.hs+5−3
@@ -3,7 +3,9 @@
3-- at GHC generated code for optimizing specific problematic cases.
3-- at GHC generated code for optimizing specific problematic cases.
4-------------------------------------------------------------------------------
4-------------------------------------------------------------------------------
5
5
6+{-# LANGUAGE FlexibleContexts #-}
6{-# LANGUAGE ScopedTypeVariables #-}
7{-# LANGUAGE ScopedTypeVariables #-}
8+import Streamly.SVar (MonadAsync)
7import qualified Streamly.Streams.StreamK as S
9import qualified Streamly.Streams.StreamK as S
8import Gauge
10import Gauge
9import System.Random
11import System.Random
@@ -12,7 +14,7 @@ maxValue :: Int
12maxValue = 100000
14maxValue = 100000
13
15
14{-# INLINE sourceUnfoldrM #-}
16{-# INLINE sourceUnfoldrM #-}
15−sourceUnfoldrM :: Monad m => S.Stream m Int
17+sourceUnfoldrM :: MonadAsync m => S.Stream m Int
16sourceUnfoldrM = S.unfoldrM step 0
18sourceUnfoldrM = S.unfoldrM step 0
17 where
19 where
18 step cnt =
20 step cnt =
@@ -21,7 +23,7 @@ sourceUnfoldrM = S.unfoldrM step 0
21 else return (Just (cnt, cnt + 1))
23 else return (Just (cnt, cnt + 1))
22
24
23{-# INLINE sourceUnfoldrMN #-}
25{-# INLINE sourceUnfoldrMN #-}
24−sourceUnfoldrMN :: Monad m => Int -> S.Stream m Int
26+sourceUnfoldrMN :: MonadAsync m => Int -> S.Stream m Int
25sourceUnfoldrMN n = S.unfoldrM step n
27sourceUnfoldrMN n = S.unfoldrM step n
26 where
28 where
27 step cnt =
29 step cnt =
@@ -74,7 +76,7 @@ dropWhileFalseX4 = S.runStream
74
76
75{-# INLINE iterateSource #-}
77{-# INLINE iterateSource #-}
76iterateSource
78iterateSource
77− :: Monad m
79+ :: MonadAsync m
78 => (S.Stream m Int -> S.Stream m Int) -> Int -> Int -> S.Stream m Int
80 => (S.Stream m Int -> S.Stream m Int) -> Int -> Int -> S.Stream m Int
79iterateSource g i n = f i (sourceUnfoldrMN n)
81iterateSource g i n = f i (sourceUnfoldrMN n)
80 where
82 where
benchmark/StreamKOps.hs+9−5
@@ -14,7 +14,7 @@ import Control.Monad (when)
14import Data.Maybe (isJust)
14import Data.Maybe (isJust)
15import Prelude
15import Prelude
16 (Monad, Int, (+), ($), (.), return, even, (>), (<=), div,
16 (Monad, Int, (+), ($), (.), return, even, (>), (<=), div,
17− subtract, undefined, Maybe(..), not, mapM_, (>>=),
17+ subtract, undefined, Maybe(..), not, (>>=),
18 maxBound)
18 maxBound)
19import qualified Prelude as P
19import qualified Prelude as P
20
20
@@ -116,6 +116,10 @@ source = sourceUnfoldrM
116runStream :: Monad m => Stream m a -> m ()
116runStream :: Monad m => Stream m a -> m ()
117runStream = S.runStream
117runStream = S.runStream
118
118
119+{-# INLINE mapM_ #-}
120+mapM_ :: Monad m => Stream m a -> m ()
121+mapM_ = S.mapM_ (\_ -> return ())
122+
119toNull = runStream
123toNull = runStream
120uncons s = do
124uncons s = do
121 r <- S.uncons s
125 r <- S.uncons s
@@ -127,19 +131,19 @@ uncons s = do
127init :: (Monad m, S.IsStream t) => t m a -> m ()
131init :: (Monad m, S.IsStream t) => t m a -> m ()
128init s = do
132init s = do
129 t <- S.init s
133 t <- S.init s
130− mapM_ S.runStream t
134+ P.mapM_ S.runStream t
131
135
132{-# INLINE tail #-}
136{-# INLINE tail #-}
133tail :: (Monad m, S.IsStream t) => t m a -> m ()
137tail :: (Monad m, S.IsStream t) => t m a -> m ()
134−tail s = S.tail s >>= mapM_ tail
138+tail s = S.tail s >>= P.mapM_ tail
135
139
136nullTail s = do
140nullTail s = do
137 r <- S.null s
141 r <- S.null s
138− when (not r) $ S.tail s >>= mapM_ nullTail
142+ when (not r) $ S.tail s >>= P.mapM_ nullTail
139
143
140headTail s = do
144headTail s = do
141 h <- S.head s
145 h <- S.head s
142− when (isJust h) $ S.tail s >>= mapM_ headTail
146+ when (isJust h) $ S.tail s >>= P.mapM_ headTail
143
147
144toList = S.toList
148toList = S.toList
145foldl = S.foldl' (+) 0
149foldl = S.foldl' (+) 0
src/Streamly/Streams/StreamK.hs+28−6
@@ -9,6 +9,8 @@
9{-# LANGUAGE ScopedTypeVariables #-}
9{-# LANGUAGE ScopedTypeVariables #-}
10{-# LANGUAGE UndecidableInstances #-} -- XXX
10{-# LANGUAGE UndecidableInstances #-} -- XXX
11
11
12+#include "inline.hs"
13+
12-- |
14-- |
13-- Module : Streamly.Streams.StreamK
15-- Module : Streamly.Streams.StreamK
14-- Copyright : (c) 2017 Harendra Kumar
16-- Copyright : (c) 2017 Harendra Kumar
@@ -227,14 +229,30 @@ uncons m =
227-- Generation
229-- Generation
228-------------------------------------------------------------------------------
230-------------------------------------------------------------------------------
229
231
232+{-# INLINE_NORMAL build #-}
233+build :: IsStream t => forall a. (forall b. (a -> b -> b) -> b -> b) -> t m a
234+build g = g cons nil
235+
236+{-# INLINE_NORMAL _augment #-}
237+_augment
238+ :: IsStream t
239+ => forall a. (forall b. (a -> b -> b) -> b -> b) -> t m a -> t m a
240+_augment g xs = g cons xs
241+
242+{-# INLINE_NORMAL _buildM #-}
243+_buildM
244+ :: (IsStream t, MonadAsync m)
245+ => forall a. ((m a -> t m a -> t m a) -> t m a -> t m a) -> t m a
246+_buildM g = g consM nil
247+
230{-# INLINE unfoldr #-}
248{-# INLINE unfoldr #-}
231unfoldr :: IsStream t => (b -> Maybe (a, b)) -> b -> t m a
249unfoldr :: IsStream t => (b -> Maybe (a, b)) -> b -> t m a
232−unfoldr step = go
250+unfoldr step b0 = build $ \cns nl ->
233− where
251+ let go s =
234− go s = mkStream $ \_ yld _ stp ->
235− case step s of
252+ case step s of
236− Nothing -> stp
237− Just (a, b) -> yld a (go b)
253+ Just (a, b) -> a `cns` go b
254+ Nothing -> nl
255+ in go b0
238
256
239{-# INLINE unfoldrM #-}
257{-# INLINE unfoldrM #-}
240unfoldrM :: (IsStream t, MonadAsync m) => (b -> m (Maybe (a, b))) -> b -> t m a
258unfoldrM :: (IsStream t, MonadAsync m) => (b -> m (Maybe (a, b))) -> b -> t m a
@@ -405,6 +423,9 @@ foldlM' step begin = foldxM step (return begin) return
405-- Specialized folds
423-- Specialized folds
406------------------------------------------------------------------------------
424------------------------------------------------------------------------------
407
425
426+-- |
427+-- > runStream = foldl' (\_ _ -> ()) ()
428+-- > runStream = mapM_ (\_ -> return ())
408{-# INLINE runStream #-}
429{-# INLINE runStream #-}
409runStream :: (Monad m, IsStream t) => t m a -> m ()
430runStream :: (Monad m, IsStream t) => t m a -> m ()
410runStream = go
431runStream = go
@@ -642,6 +663,7 @@ findIndices p = go 0
642
663
643-- | Apply a monadic action to each element of the stream and discard the
664-- | Apply a monadic action to each element of the stream and discard the
644-- output of the action.
665-- output of the action.
666+{-# INLINE mapM_ #-}
645mapM_ :: (IsStream t, Monad m) => (a -> m b) -> t m a -> m ()
667mapM_ :: (IsStream t, Monad m) => (a -> m b) -> t m a -> m ()
646mapM_ f m = go m
668mapM_ f m = go m
647 where
669 where