on master help sign in
Add mapM_ benchmark, add build/augment
commit9c8a7504fa6f8395901e762274eaebc224003748 authorHarendra Kumar <harendra.kumar@gmail.com> dateThu 27 Dec 2018, 13:59 (8 years ago) parenta30ef3e
4 files changed+43−14
benchmark/BaseStreams.hs+1 benchmark/NanoBenchmarks.hs+5−3 benchmark/StreamKOps.hs+9−5 src/Streamly/Streams/StreamK.hs+28−6
benchmark/BaseStreams.hs+1
@@ -165,6 +165,7 @@ main =
165165 ]
166166 , bgroup "elimination"
167167 [ benchIO "toNull" K.toNull K.sourceUnfoldrM
168+ , benchIO "mapM_" K.mapM_ K.sourceUnfoldrM
168169 , benchIO "uncons" K.uncons K.sourceUnfoldrM
169170 , benchFold "init" K.init K.sourceUnfoldrM
170171 , benchFold "tail" K.tail K.sourceUnfoldrM
benchmark/NanoBenchmarks.hs+5−3
@@ -3,7 +3,9 @@
33-- at GHC generated code for optimizing specific problematic cases.
44-------------------------------------------------------------------------------
55
6+{-# LANGUAGE FlexibleContexts #-}
67{-# LANGUAGE ScopedTypeVariables #-}
8+import Streamly.SVar (MonadAsync)
79import qualified Streamly.Streams.StreamK as S
810import Gauge
911import System.Random
@@ -12,7 +14,7 @@ maxValue :: Int
1214maxValue = 100000
1315
1416{-# INLINE sourceUnfoldrM #-}
15−sourceUnfoldrM :: Monad m => S.Stream m Int
17+sourceUnfoldrM :: MonadAsync m => S.Stream m Int
1618sourceUnfoldrM = S.unfoldrM step 0
1719 where
1820 step cnt =
@@ -21,7 +23,7 @@ sourceUnfoldrM = S.unfoldrM step 0
2123 else return (Just (cnt, cnt + 1))
2224
2325{-# INLINE sourceUnfoldrMN #-}
24−sourceUnfoldrMN :: Monad m => Int -> S.Stream m Int
26+sourceUnfoldrMN :: MonadAsync m => Int -> S.Stream m Int
2527sourceUnfoldrMN n = S.unfoldrM step n
2628 where
2729 step cnt =
@@ -74,7 +76,7 @@ dropWhileFalseX4 = S.runStream
7476
7577{-# INLINE iterateSource #-}
7678iterateSource
77− :: Monad m
79+ :: MonadAsync m
7880 => (S.Stream m Int -> S.Stream m Int) -> Int -> Int -> S.Stream m Int
7981iterateSource g i n = f i (sourceUnfoldrMN n)
8082 where
benchmark/StreamKOps.hs+9−5
@@ -14,7 +14,7 @@ import Control.Monad (when)
1414import Data.Maybe (isJust)
1515import Prelude
1616 (Monad, Int, (+), ($), (.), return, even, (>), (<=), div,
17− subtract, undefined, Maybe(..), not, mapM_, (>>=),
17+ subtract, undefined, Maybe(..), not, (>>=),
1818 maxBound)
1919import qualified Prelude as P
2020
@@ -116,6 +116,10 @@ source = sourceUnfoldrM
116116runStream :: Monad m => Stream m a -> m ()
117117runStream = S.runStream
118118
119+{-# INLINE mapM_ #-}
120+mapM_ :: Monad m => Stream m a -> m ()
121+mapM_ = S.mapM_ (\_ -> return ())
122+
119123toNull = runStream
120124uncons s = do
121125 r <- S.uncons s
@@ -127,19 +131,19 @@ uncons s = do
127131init :: (Monad m, S.IsStream t) => t m a -> m ()
128132init s = do
129133 t <- S.init s
130− mapM_ S.runStream t
134+ P.mapM_ S.runStream t
131135
132136{-# INLINE tail #-}
133137tail :: (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
135139
136140nullTail s = do
137141 r <- S.null s
138− when (not r) $ S.tail s >>= mapM_ nullTail
142+ when (not r) $ S.tail s >>= P.mapM_ nullTail
139143
140144headTail s = do
141145 h <- S.head s
142− when (isJust h) $ S.tail s >>= mapM_ headTail
146+ when (isJust h) $ S.tail s >>= P.mapM_ headTail
143147
144148toList = S.toList
145149foldl = S.foldl' (+) 0
src/Streamly/Streams/StreamK.hs+28−6
@@ -9,6 +9,8 @@
99{-# LANGUAGE ScopedTypeVariables #-}
1010{-# LANGUAGE UndecidableInstances #-} -- XXX
1111
12+#include "inline.hs"
13+
1214-- |
1315-- Module : Streamly.Streams.StreamK
1416-- Copyright : (c) 2017 Harendra Kumar
@@ -227,14 +229,30 @@ uncons m =
227229-- Generation
228230-------------------------------------------------------------------------------
229231
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+
230248{-# INLINE unfoldr #-}
231249unfoldr :: IsStream t => (b -> Maybe (a, b)) -> b -> t m a
232−unfoldr step = go
233− where
234− go s = mkStream $ \_ yld _ stp ->
235− case step s of
236− Nothing -> stp
237− Just (a, b) -> yld a (go b)
250+unfoldr step b0 = build $ \cns nl ->
251+ let go s =
252+ case step s of
253+ Just (a, b) -> a `cns` go b
254+ Nothing -> nl
255+ in go b0
238256
239257{-# INLINE unfoldrM #-}
240258unfoldrM :: (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
405423-- Specialized folds
406424------------------------------------------------------------------------------
407425
426+-- |
427+-- > runStream = foldl' (\_ _ -> ()) ()
428+-- > runStream = mapM_ (\_ -> return ())
408429{-# INLINE runStream #-}
409430runStream :: (Monad m, IsStream t) => t m a -> m ()
410431runStream = go
@@ -642,6 +663,7 @@ findIndices p = go 0
642663
643664-- | Apply a monadic action to each element of the stream and discard the
644665-- output of the action.
666+{-# INLINE mapM_ #-}
645667mapM_ :: (IsStream t, Monad m) => (a -> m b) -> t m a -> m ()
646668mapM_ f m = go m
647669 where