on master help sign in
Add INLINE to zipWith and zipWithM
36c22e3Harendra Kumar
benchmark/Streamly/Benchmark/Data/Stream/StreamK.hs+5
@@ -384,6 +384,10 @@ iterateDropWhileTrue streamLen iterStreamLen =
384zipWith :: Monad m => Stream m Int -> m ()
384zipWith :: Monad m => Stream m Int -> m ()
385zipWith src = drain $ S.zipWith (,) src src
385zipWith src = drain $ S.zipWith (,) src src
386
386
387+{-# INLINE zipWithM #-}
388+zipWithM :: Monad m => Stream m Int -> m ()
389+zipWithM src = drain $ S.zipWithM (curry return) src src
390+
387{-# INLINE sortByK #-}
391{-# INLINE sortByK #-}
388sortByK :: (a -> a -> Ordering) -> Stream m a -> Stream m a
392sortByK :: (a -> a -> Ordering) -> Stream m a -> Stream m a
389sortByK f = S.concatPairsWith (S.mergeBy f) S.fromPure
393sortByK f = S.concatPairsWith (S.mergeBy f) S.fromPure
@@ -745,6 +749,7 @@ o_1_space_zipping :: Int -> Benchmark
745o_1_space_zipping streamLen =
749o_1_space_zipping streamLen =
746 bgroup "zipping"
750 bgroup "zipping"
747 [ benchFold "zipWith" zipWith (unfoldrM streamLen)
751 [ benchFold "zipWith" zipWith (unfoldrM streamLen)
752+ , benchFold "zipWithM" zipWithM (unfoldrM streamLen)
748 ]
753 ]
749
754
750o_1_space_mixed :: Int -> Benchmark
755o_1_space_mixed :: Int -> Benchmark
src/Streamly/Internal/Data/Stream/StreamK.hs+8−5
@@ -750,10 +750,12 @@ mapMaybe f = go
750-- | Zip two streams serially using a pure zipping function.
750-- | Zip two streams serially using a pure zipping function.
751--
751--
752-- @since 0.1.0
752-- @since 0.1.0
753−{-# INLINABLE zipWith #-}
753+{-# INLINE zipWith #-}
754zipWith :: (a -> b -> c) -> Stream m a -> Stream m b -> Stream m c
754zipWith :: (a -> b -> c) -> Stream m a -> Stream m b -> Stream m c
755zipWith f = go
755zipWith f = go
756+
756 where
757 where
758+
757 go mx my = mkStream $ \st yld sng stp -> do
759 go mx my = mkStream $ \st yld sng stp -> do
758 let merge a ra =
760 let merge a ra =
759 let single2 b = sng (f a b)
761 let single2 b = sng (f a b)
@@ -766,16 +768,17 @@ zipWith f = go
766-- | Zip two streams serially using a monadic zipping function.
768-- | Zip two streams serially using a monadic zipping function.
767--
769--
768-- @since 0.1.0
770-- @since 0.1.0
769−{-# INLINABLE zipWithM #-}
771+{-# INLINE zipWithM #-}
770zipWithM :: Monad m =>
772zipWithM :: Monad m =>
771 (a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
773 (a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
772zipWithM f = go
774zipWithM f = go
775+
773 where
776 where
777+
774 go mx my = mkStream $ \st yld sng stp -> do
778 go mx my = mkStream $ \st yld sng stp -> do
775 let merge a ra =
779 let merge a ra =
776− let runIt x = foldStream st yld sng stp x
777− single2 b = f a b >>= sng
780+ let single2 b = f a b >>= sng
778− yield2 b rb = f a b >>= \x -> runIt (x `cons` go ra rb)
781+ yield2 b rb = f a b >>= \x -> yld x (go ra rb)
779 in foldStream (adaptState st) yield2 single2 stp my
782 in foldStream (adaptState st) yield2 single2 stp my
780 let single1 a = merge a nil
783 let single1 a = merge a nil
781 yield1 = merge
784 yield1 = merge