on master help sign in
Make changes to unfolds from stream

* Add fromStreamK * fromStream: signature change, use fromStreamK as fromStreamD has very bad performance characterstics. * Add INLINEs to fromStream * Remove fromStream1 and fromStream2 as they can be expressed trivially in terms of fromStream and lmap.

commitdf6d8e0b04981b988e23aa2c9db843ea409fdc2c authorHarendra Kumar <harendra@composewell.com> dateFri 29 May 2020, 21:41 (6 years ago) parent01fa925
2 files changed+32−29
src/Streamly/Internal/Data/Unfold.hs+31−29 src/Streamly/Internal/Prelude.hs+1
src/Streamly/Internal/Data/Unfold.hs+31−29
@@ -77,8 +77,8 @@ module Streamly.Internal.Data.Unfold
7777
7878 -- * Unfolds
7979 , fromStream
80− , fromStream1
81− , fromStream2
80+ , fromStreamK
81+ , fromStreamD
8282 , nilM
8383 , consM
8484 , effect
@@ -317,38 +317,40 @@ mapMWithInput f (Unfold ustep uinject) = Unfold step inject
317317-- Convert streams into unfolds
318318-------------------------------------------------------------------------------
319319
320−{-# INLINE_LATE streamStep #-}
321−streamStep :: Monad m => Stream m a -> m (Step (Stream m a) a)
322−streamStep (Stream step1 state) = do
323− r <- step1 defState state
324− return $ case r of
325− Yield x s -> Yield x (Stream step1 s)
326− Skip s -> Skip (Stream step1 s)
327− Stop -> Stop
320+-- this is horribly slow
321+{-# INLINE_NORMAL fromStreamD #-}
322+fromStreamD :: Monad m => Unfold m (Stream m a) a
323+fromStreamD = Unfold step return
324+ where
328325
329−-- | Convert a stream into an 'Unfold'. Note that a stream converted to an
330−-- 'Unfold' may not be as efficient as an 'Unfold' in some situations.
331−--
332−-- /Internal/
333−fromStream :: (K.IsStream t, Monad m) => t m b -> Unfold m Void b
334−fromStream str = Unfold streamStep (\_ -> return $ D.toStreamD str)
326+ {-# INLINE_LATE step #-}
327+ step (Stream step1 state1) = do
328+ r <- step1 defState state1
329+ return $ case r of
330+ Yield x s -> Yield x (Stream step1 s)
331+ Skip s -> Skip (Stream step1 s)
332+ Stop -> Stop
335333
336−-- | Convert a single argument stream generator function into an
337−-- 'Unfold'. Note that a stream converted to an 'Unfold' may not be as
338−-- efficient as an 'Unfold' in some situations.
339−--
340−-- /Internal/
341−fromStream1 :: (K.IsStream t, Monad m) => (a -> t m b) -> Unfold m a b
342−fromStream1 f = Unfold streamStep (return . D.toStreamD . f)
334+{-# INLINE_NORMAL fromStreamK #-}
335+fromStreamK :: Monad m => Unfold m (K.Stream m a) a
336+fromStreamK = Unfold step return
337+
338+ where
339+
340+ {-# INLINE_LATE step #-}
341+ step stream = do
342+ r <- K.uncons stream
343+ return $ case r of
344+ Just (x, xs) -> Yield x xs
345+ Nothing -> Stop
343346
344−-- | Convert a two argument stream generator function into an 'Unfold'. Note
345−-- that a stream converted to an 'Unfold' may not be as efficient as an
346−-- 'Unfold' in some situations.
347+-- | Convert a stream into an 'Unfold'. Note that a stream converted to an
348+-- 'Unfold' may not be as efficient as an 'Unfold' in some situations.
347349--
348350-- /Internal/
349−fromStream2 :: (K.IsStream t, Monad m)
350− => (a -> b -> t m c) -> Unfold m (a, b) c
351−fromStream2 f = Unfold streamStep (\(a, b) -> return $ D.toStreamD $ f a b)
351+{-# INLINE_NORMAL fromStream #-}
352+fromStream :: (K.IsStream t, Monad m) => Unfold m (t m a) a
353+fromStream = lmap K.toStream fromStreamK
352354
353355-------------------------------------------------------------------------------
354356-- Unfolds
src/Streamly/Internal/Prelude.hs+1
@@ -3038,6 +3038,7 @@ concatMapWith = K.concatMapBy
30383038-- @
30393039-- concatMap = 'concatMapWith' 'Serial.serial'
30403040-- concatMap f = 'concatMapM' (return . f)
3041+-- concatMap f = 'concatUnfold' (UF.lmap f UF.fromStream)
30413042-- @
30423043--
30433044-- @since 0.6.0