on master help sign in
normalize zip benches, add mergeAsyncByM benchmark

Do not use "tail" in zip benchmarks otherwise the tail perf can impact these benchmarks. Also it makes them consistent with other such benchmarks. Use half the stream size for each side of zip so that we effectively create a stream of size similar to other linear benchmarks.

commit26354ba701c0d2d6c0be653138baba370b97e1a3 authorHarendra Kumar <harendra.kumar@gmail.com> dateWed 22 Jan 2020, 03:22 (7 years ago) parent785630e
2 files changed+33−23
benchmark/LinearAsync.hs+9−5 benchmark/Streamly/Benchmark/Prelude.hs+24−18
benchmark/LinearAsync.hs+9−5
@@ -28,9 +28,9 @@ benchIO value name f = bench name $ nfIO $ randomRIO (1,1) >>= f . Ops.source va
2828-- | Takes a source, and uses it with a default drain/fold method.
2929{-# INLINE benchSrcIO #-}
3030benchSrcIO
31− :: (t IO Int -> SerialT IO Int)
31+ :: (t IO a -> SerialT IO a)
3232 -> String
33− -> (Int -> t IO Int)
33+ -> (Int -> t IO a)
3434 -> Benchmark
3535benchSrcIO t name f
3636 = bench name $ nfIO $ randomRIO (1,1) >>= Ops.toNull t . f
@@ -130,10 +130,14 @@ main = do
130130 (Ops.concatStreamsWith ahead (value2 * 2) (value2 `div` 2))
131131 ]
132132 , bgroup "zip"
133− [ benchIO value "zip" Ops.zipAsync
134− , benchIO value "zipM" Ops.zipAsyncM
135− , benchIO value "zipAp" Ops.zipAsyncAp
133+ [ benchSrcIO serially "zipAsync (2,x/2)" (Ops.zipAsync (value `div` 2))
134+ , benchSrcIO serially "zipAsyncM (2,x/2)"
135+ (Ops.zipAsyncM (value `div` 2))
136+ , benchSrcIO serially "zipAsyncAp (2,x/2)"
137+ (Ops.zipAsyncAp (value `div` 2))
136138 , benchIO value "fmap zipAsyncly" $ Ops.fmap' zipAsyncly 1
139+ , benchSrcIO serially "mergeAsyncByM (2,x/2)"
140+ (Ops.mergeAsyncByM (value `div` 2))
137141 -- Parallel stages in a pipeline
138142 , benchIO value "parAppMap" Ops.parAppMap
139143 , benchIO value "parAppSum" Ops.parAppSum
benchmark/Streamly/Benchmark/Prelude.hs+24−18
@@ -29,7 +29,6 @@ import Control.Monad.IO.Class (MonadIO(..))
2929import Control.Monad.State.Strict (StateT, get, put)
3030import Data.Functor.Identity (Identity, runIdentity)
3131import Data.IORef (newIORef, modifyIORef')
32−import Data.Maybe (fromJust)
3332import GHC.Generics (Generic)
3433import Prelude
3534 (Monad, Int, (+), ($), (.), return, fmap, even, (>), (<=), (==), (>=),
@@ -718,25 +717,32 @@ inspect $ 'zipM `hasNoType` ''D.Step
718717#endif
719718
720719{-# INLINE zipAsync #-}
720+zipAsync :: (S.IsStream t, S.MonadAsync m) => Int -> Int -> t m (Int, Int)
721+zipAsync count n = do
722+ S.zipAsyncWith (,)
723+ (sourceUnfoldrMN count n)
724+ (sourceUnfoldrMN count (n + 1))
725+
721726{-# INLINE zipAsyncM #-}
727+zipAsyncM :: (S.IsStream t, S.MonadAsync m) => Int -> Int -> t m (Int, Int)
728+zipAsyncM count n = do
729+ S.zipAsyncWithM (curry return)
730+ (sourceUnfoldrMN count n)
731+ (sourceUnfoldrMN count (n + 1))
732+
722733{-# INLINE zipAsyncAp #-}
723−zipAsync, zipAsyncAp, zipAsyncM :: S.MonadAsync m => Stream m Int -> m ()
724−
725−zipAsync src = do
726− r <- S.tail src
727− let src1 = fromJust r
728− transform (S.zipAsyncWith (,) src src1)
729−
730−zipAsyncM src = do
731− r <- S.tail src
732− let src1 = fromJust r
733− transform (S.zipAsyncWithM (curry return) src src1)
734−
735−zipAsyncAp src = do
736− r <- S.tail src
737− let src1 = fromJust r
738− transform (S.zipAsyncly $ (,) <$> S.serially src
739− <*> S.serially src1)
734+zipAsyncAp :: (S.IsStream t, S.MonadAsync m) => Int -> Int -> t m (Int, Int)
735+zipAsyncAp count n = do
736+ S.zipAsyncly $ (,)
737+ <$> (sourceUnfoldrMN count n)
738+ <*> (sourceUnfoldrMN count (n + 1))
739+
740+{-# INLINE mergeAsyncByM #-}
741+mergeAsyncByM :: (S.IsStream t, S.MonadAsync m) => Int -> Int -> t m Int
742+mergeAsyncByM count n = do
743+ S.mergeAsyncByM (\a b -> return (a `compare` b))
744+ (sourceUnfoldrMN count n)
745+ (sourceUnfoldrMN count (n + 1))
740746
741747-------------------------------------------------------------------------------
742748-- Multi-stream folds