on master help sign in
split head and tail benchmarks from nullHeadTail
commit615197d1c53f2c03acff9a42ce3f2503a315c9c9 authorHarendra Kumar <harendra.kumar@gmail.com> dateWed 24 Oct 2018, 10:07 (8 years ago) parent829f929
4 files changed+40−19
benchmark/BaseStreams.hs+5−2 benchmark/LinearOps.hs+5−4 benchmark/StreamDOps.hs+19−6 benchmark/StreamKOps.hs+11−7
benchmark/BaseStreams.hs+5−2
@@ -45,7 +45,9 @@ main =
4545 , bgroup "elimination"
4646 [ benchIO "toNull" D.toNull D.sourceUnfoldrM
4747 , benchIO "uncons" D.uncons D.sourceUnfoldrM
48− , benchIO "nullHeadTail" D.nullHeadTail D.sourceUnfoldrM
48+ , benchFold "tail" D.tail D.sourceUnfoldrM
49+ , benchIO "nullTail" D.nullTail D.sourceUnfoldrM
50+ , benchIO "headTail" D.headTail D.sourceUnfoldrM
4951 ]
5052 , bgroup "transformation"
5153 [ benchIO "scanlM'" D.scan D.sourceUnfoldrM
@@ -96,7 +98,8 @@ main =
9698 , benchIO "uncons" K.uncons K.sourceUnfoldrM
9799 , benchFold "init" K.init K.sourceUnfoldrM
98100 , benchFold "tail" K.tail K.sourceUnfoldrM
99− , benchIO "nullHeadTail" K.nullHeadTail K.sourceUnfoldrM
101+ , benchIO "nullTail" K.nullTail K.sourceUnfoldrM
102+ , benchIO "headTail" K.headTail K.sourceUnfoldrM
100103 , benchFold "toList" K.toList K.sourceUnfoldrM
101104 , benchFold "fold" K.foldl K.sourceUnfoldrM
102105 , benchFold "last" K.last K.sourceUnfoldrM
benchmark/LinearOps.hs+5−4
@@ -294,10 +294,6 @@ elemIndices n = composeN n $ S.elemIndices maxValue
294294{-# INLINE concat #-}
295295zip, zipM, concat :: Monad m => Stream m Int -> m ()
296296
297−{-# INLINE zipAsync #-}
298−{-# INLINE zipAsyncM #-}
299−zipAsync, zipAsyncM :: S.MonadAsync m => Stream m Int -> m ()
300−
301297zip src = do
302298 r <- S.tail src
303299 let src1 = fromJust r
@@ -306,6 +302,11 @@ zipM src = do
306302 r <- S.tail src
307303 let src1 = fromJust r
308304 transform (S.zipWithM (curry return) src src1)
305+
306+{-# INLINE zipAsync #-}
307+{-# INLINE zipAsyncM #-}
308+zipAsync, zipAsyncM :: S.MonadAsync m => Stream m Int -> m ()
309+
309310zipAsync src = do
310311 r <- S.tail src
311312 let src1 = fromJust r
benchmark/StreamDOps.hs+19−6
@@ -10,6 +10,7 @@
1010module StreamDOps where
1111
1212import Control.Monad (when)
13+import Data.Maybe (isJust)
1314import Prelude
1415 (Monad, Int, (+), ($), (.), return, (>), even, (<=),
1516 subtract, undefined, Maybe(..), not, mapM_, (>>=))
@@ -25,7 +26,8 @@ maxValue = value + 1000
2526-------------------------------------------------------------------------------
2627
2728{-# INLINE uncons #-}
28−{-# INLINE nullHeadTail #-}
29+{-# INLINE nullTail #-}
30+{-# INLINE headTail #-}
2931{-# INLINE scan #-}
3032{-# INLINE map #-}
3133{-# INLINE filterEven #-}
@@ -43,7 +45,7 @@ maxValue = value + 1000
4345{-# INLINE composeAllInFilters #-}
4446{-# INLINE composeAllOutFilters #-}
4547{-# INLINE composeMapAllInFilter #-}
46−uncons, nullHeadTail, map, scan, filterEven, filterAllOut,
48+uncons, nullTail, headTail, map, scan, filterEven, filterAllOut,
4749 filterAllIn, takeOne, takeAll, takeWhileTrue, dropAll, dropWhileTrue, zip,
4850 -- concat,
4951 composeAllInFilters, composeAllOutFilters, composeMapAllInFilter
@@ -109,16 +111,27 @@ runStream :: Monad m => Stream m a -> m ()
109111runStream = S.runStream
110112
111113toNull = runStream
114+
112115uncons s = do
113116 r <- S.uncons s
114117 case r of
115118 Nothing -> return ()
116119 Just (_, t) -> uncons t
117−nullHeadTail s = do
120+
121+{-# INLINE tail #-}
122+tail :: Monad m => Stream m a -> m ()
123+tail s = S.tail s >>= mapM_ tail
124+
125+nullTail s = do
118126 r <- S.null s
119127 when (not r) $ do
120− _ <- S.head s
121− S.tail s >>= mapM_ nullHeadTail
128+ S.tail s >>= mapM_ nullTail
129+
130+headTail s = do
131+ h <- S.head s
132+ when (isJust h) $
133+ S.tail s >>= mapM_ headTail
134+
122135toList = S.toList
123136foldl = S.foldl' (+) 0
124137last = S.last
@@ -163,7 +176,7 @@ composeAllInFilters = compose (S.filter (<= maxValue))
163176composeAllOutFilters = compose (S.filter (> maxValue))
164177composeMapAllInFilter = compose (S.filter (<= maxValue) . S.map (subtract 1))
165178
166−{-# INLINABLE composeScaling #-}
179+{-# INLINE composeScaling #-}
167180composeScaling :: Monad m => Int -> Stream m Int -> m ()
168181composeScaling m =
169182 case m of
benchmark/StreamKOps.hs+11−7
@@ -10,6 +10,7 @@
1010module StreamKOps where
1111
1212import Control.Monad (when)
13+import Data.Maybe (isJust)
1314import Prelude
1415 (Monad, Int, (+), ($), (.), return, fmap, even, (>), (<=),
1516 subtract, undefined, Maybe(..), not, mapM_, (>>=))
@@ -28,7 +29,8 @@ maxValue = value + 1000
2829
2930{-# INLINE toNull #-}
3031{-# INLINE uncons #-}
31−{-# INLINE nullHeadTail #-}
32+{-# INLINE nullTail #-}
33+{-# INLINE headTail #-}
3234{-# INLINE scan #-}
3335{-# INLINE map #-}
3436{-# INLINE filterEven #-}
@@ -44,7 +46,7 @@ maxValue = value + 1000
4446{-# INLINE composeAllInFilters #-}
4547{-# INLINE composeAllOutFilters #-}
4648{-# INLINE composeMapAllInFilter #-}
47−toNull, uncons, nullHeadTail, scan, map, filterEven, filterAllOut,
49+toNull, uncons, nullTail, headTail, scan, map, filterEven, filterAllOut,
4850 filterAllIn, takeOne, takeAll, takeWhileTrue, dropAll, dropWhileTrue, zip,
4951 concat, composeAllInFilters, composeAllOutFilters,
5052 composeMapAllInFilter
@@ -141,13 +143,15 @@ init s = do
141143tail :: (Monad m, S.IsStream t) => t m a -> m ()
142144tail s = S.tail s >>= mapM_ tail
143145
144−-- | If the stream is not null get its head and tail and then do the same to
145−-- the tail.
146−nullHeadTail s = do
146+nullTail s = do
147147 r <- S.null s
148148 when (not r) $ do
149− _ <- S.head s
150− S.tail s >>= mapM_ nullHeadTail
149+ S.tail s >>= mapM_ nullTail
150+
151+headTail s = do
152+ h <- S.head s
153+ when (isJust h) $
154+ S.tail s >>= mapM_ headTail
151155
152156toList = S.toList
153157foldl = S.foldl' (+) 0