on master help sign in
Inline the consume state in foldMany1
ccbaa15Harendra Kumar
src/Streamly/Internal/Data/Stream/StreamD/Type.hs+14−14
@@ -842,8 +842,7 @@ foldMany (Fold fstep initial extract) (Stream step state) =
842data FoldMany1 s fs b a
842data FoldMany1 s fs b a
843 = FoldMany1Start s
843 = FoldMany1Start s
844 | FoldMany1First fs s
844 | FoldMany1First fs s
845− | FoldMany1Consume s fs a
845+ | FoldMany1Loop s fs
846− | FoldMany1Gen s fs
847 | FoldMany1Yield b (FoldMany1 s fs b a)
846 | FoldMany1Yield b (FoldMany1 s fs b a)
848 | FoldMany1Done
847 | FoldMany1Done
849
848
@@ -870,6 +869,15 @@ foldMany1 (Fold fstep initial extract) (Stream step state) =
870
869
871 where
870 where
872
871
872+ {-# INLINE consume #-}
873+ consume x s fs = do
874+ res <- fstep fs x
875+ return
876+ $ Skip
877+ $ case res of
878+ FL.Done b -> FoldMany1Yield b (FoldMany1Start s)
879+ FL.Partial ps -> FoldMany1Loop s ps
880+
873 {-# INLINE_LATE step' #-}
881 {-# INLINE_LATE step' #-}
874 step' _ (FoldMany1Start st) = do
882 step' _ (FoldMany1Start st) = do
875 r <- initial
883 r <- initial
@@ -881,22 +889,14 @@ foldMany1 (Fold fstep initial extract) (Stream step state) =
881 step' gst (FoldMany1First fs st) = do
889 step' gst (FoldMany1First fs st) = do
882 r <- step (adaptState gst) st
890 r <- step (adaptState gst) st
883 case r of
891 case r of
884− Yield x s -> do
892+ Yield x s -> consume x s fs
885− return $ Skip $ FoldMany1Consume s fs x
886 Skip s -> return $ Skip (FoldMany1Start s)
893 Skip s -> return $ Skip (FoldMany1Start s)
887 Stop -> return Stop
894 Stop -> return Stop
888− step' _ (FoldMany1Consume st fs x) = do
895+ step' gst (FoldMany1Loop st fs) = do
889− r <- fstep fs x
890− return
891− $ Skip
892− $ case r of
893− FL.Done b -> FoldMany1Yield b (FoldMany1Start st)
894− FL.Partial fs1 -> FoldMany1Gen st fs1
895− step' gst (FoldMany1Gen st fs) = do
896 r <- step (adaptState gst) st
896 r <- step (adaptState gst) st
897 case r of
897 case r of
898− Yield x s -> return $ Skip $ FoldMany1Consume s fs x
898+ Yield x s -> consume x s fs
899− Skip s -> return $ Skip (FoldMany1Gen s fs)
899+ Skip s -> return $ Skip (FoldMany1Loop s fs)
900 Stop -> do
900 Stop -> do
901 b <- extract fs
901 b <- extract fs
902 return $ Skip (FoldMany1Yield b FoldMany1Done)
902 return $ Skip (FoldMany1Yield b FoldMany1Done)