on master help sign in
Inline the consume state in foldMany
b8ede3eHarendra Kumar
src/Streamly/Internal/Data/Stream/StreamD/Type.hs+17−18
@@ -789,10 +789,9 @@ instance Monad m => Monad (Stream m) where
789{-# ANN type FoldMany Fuse #-}
789{-# ANN type FoldMany Fuse #-}
790data FoldMany s fs b a
790data FoldMany s fs b a
791 = FoldManyStart s
791 = FoldManyStart s
792− | FoldManyConsume s fs a
792+ | FoldManyLoop s fs
793− | FoldManyBuffer s fs
794 | FoldManyYield b (FoldMany s fs b a)
793 | FoldManyYield b (FoldMany s fs b a)
795− | FoldManyFinish
794+ | FoldManyDone
796
795
797-- | This is the stream equivalent of "Data.Fold.Internal.many". The fold
796-- | This is the stream equivalent of "Data.Fold.Internal.many". The fold
798-- may consume 0 or more elements. It means:
797-- may consume 0 or more elements. It means:
@@ -810,6 +809,15 @@ foldMany (Fold fstep initial extract) (Stream step state) =
810
809
811 where
810 where
812
811
812+ {-# INLINE consume #-}
813+ consume x s fs = do
814+ res <- fstep fs x
815+ return
816+ $ Skip
817+ $ case res of
818+ FL.Done b -> FoldManyYield b (FoldManyStart s)
819+ FL.Partial ps -> FoldManyLoop s ps
820+
813 {-# INLINE_LATE step' #-}
821 {-# INLINE_LATE step' #-}
814 step' _ (FoldManyStart st) = do
822 step' _ (FoldManyStart st) = do
815 r <- initial
823 r <- initial
@@ -817,26 +825,17 @@ foldMany (Fold fstep initial extract) (Stream step state) =
817 $ Skip
825 $ Skip
818 $ case r of
826 $ case r of
819 FL.Done b -> FoldManyYield b (FoldManyStart st)
827 FL.Done b -> FoldManyYield b (FoldManyStart st)
820− FL.Partial fs -> FoldManyBuffer st fs
828+ FL.Partial fs -> FoldManyLoop st fs
821− -- This state is not strictly required but it helps the compiler fuse the
822− -- code.
823− step' _ (FoldManyConsume st fs x) = do
824− r <- fstep fs x
825− return
826− $ Skip
827− $ case r of
828− FL.Done b -> FoldManyYield b (FoldManyStart st)
829− FL.Partial fs1 -> FoldManyBuffer st fs1
830− step' gst (FoldManyBuffer st fs) = do
829+ step' gst (FoldManyLoop st fs) = do
831 r <- step (adaptState gst) st
830 r <- step (adaptState gst) st
832 case r of
831 case r of
833− Yield x s -> return $ Skip $ FoldManyConsume s fs x
832+ Yield x s -> consume x s fs
834− Skip s -> return $ Skip (FoldManyBuffer s fs)
833+ Skip s -> return $ Skip (FoldManyLoop s fs)
835 Stop -> do
834 Stop -> do
836 b <- extract fs
835 b <- extract fs
837− return $ Skip (FoldManyYield b FoldManyFinish)
836+ return $ Skip (FoldManyYield b FoldManyDone)
838 step' _ (FoldManyYield b next) = return $ Yield b next
837 step' _ (FoldManyYield b next) = return $ Yield b next
839− step' _ FoldManyFinish = return Stop
838+ step' _ FoldManyDone = return Stop
840
839
841{-# ANN type FoldMany1 Fuse #-}
840{-# ANN type FoldMany1 Fuse #-}
842data FoldMany1 s fs b a
841data FoldMany1 s fs b a