on master help sign in
Use array copy (memcpy) instead of stream copy
7a8b362Harendra Kumar
core/src/Streamly/Internal/Data/MutArray/Type.hs+8−5
@@ -1383,7 +1383,6 @@ pinnedChunksOf :: forall m a. (MonadIO m, Unbox a)
1383-- pinnedChunksOf n = D.foldMany (pinnedWriteN n)
1383-- pinnedChunksOf n = D.foldMany (pinnedWriteN n)
1384pinnedChunksOf = chunksOfAs Pinned
1384pinnedChunksOf = chunksOfAs Pinned
1385
1385
1386−-- XXX This should take a PinnedState
1387-- XXX buffer to a list instead?
1386-- XXX buffer to a list instead?
1388-- | Buffer the stream into arrays in memory.
1387-- | Buffer the stream into arrays in memory.
1389{-# INLINE arrayStreamKFromStreamDAs #-}
1388{-# INLINE arrayStreamKFromStreamDAs #-}
@@ -2088,15 +2087,19 @@ arrayStreamKLength as = K.foldl' (+) 0 (K.map length as)
2088--
2087--
2089{-# INLINE fromArrayStreamK #-}
2088{-# INLINE fromArrayStreamK #-}
2090fromArrayStreamK :: (Unbox a, MonadIO m) =>
2089fromArrayStreamK :: (Unbox a, MonadIO m) =>
2091− StreamK m (MutArray a) -> m (MutArray a)
2090+ PinnedState -> StreamK m (MutArray a) -> m (MutArray a)
2092−fromArrayStreamK as = do
2091+fromArrayStreamK ps as = do
2093 len <- arrayStreamKLength as
2092 len <- arrayStreamKLength as
2093+ arr <- newAs ps len
2094+ -- XXX is StreamK fold faster or StreamD fold?
2095+ K.foldlM' spliceUnsafe (pure arr) as
2094− fromStreamDN len $ D.unfoldMany reader $ D.fromStreamK as
2096+ -- fromStreamDN len $ D.unfoldMany reader $ D.fromStreamK as
2095
2097
2096{-# INLINE fromStreamDAs #-}
2098{-# INLINE fromStreamDAs #-}
2097fromStreamDAs ::
2099fromStreamDAs ::
2098 (MonadIO m, Unbox a) => PinnedState -> D.Stream m a -> m (MutArray a)
2100 (MonadIO m, Unbox a) => PinnedState -> D.Stream m a -> m (MutArray a)
2101+fromStreamDAs ps m =
2099−fromStreamDAs ps m = arrayStreamKFromStreamDAs ps m >>= fromArrayStreamK
2102+ arrayStreamKFromStreamDAs Unpinned m >>= fromArrayStreamK ps
2100
2103
2101-- CAUTION: a very large number (millions) of arrays can degrade performance
2104-- CAUTION: a very large number (millions) of arrays can degrade performance
2102-- due to GC overhead because we need to buffer the arrays before we flatten
2105-- due to GC overhead because we need to buffer the arrays before we flatten
core/src/Streamly/Internal/Data/StreamK.hs−20
@@ -37,8 +37,6 @@ module Streamly.Internal.Data.StreamK
37 -- * Elimination
37 -- * Elimination
38 -- ** General Folds
38 -- ** General Folds
39 , foldr1
39 , foldr1
40− , foldlM'
41− , foldlMx'
42 , fold
40 , fold
43 , foldBreak
41 , foldBreak
44 , foldEither
42 , foldEither
@@ -306,19 +304,6 @@ foldr1 step m = do
306 yieldk a r = fmap (step p) (go a r)
304 yieldk a r = fmap (step p) (go a r)
307 in foldStream defState yieldk single stp m1
305 in foldStream defState yieldk single stp m1
308
306
309−-- XXX replace the recursive "go" with explicit continuations.
310−-- | Like 'foldx', but with a monadic step function.
311−{-# INLINABLE foldlMx' #-}
312−foldlMx' :: Monad m
313− => (x -> a -> m x) -> m x -> (x -> m b) -> StreamK m a -> m b
314−foldlMx' step begin done = go begin
315− where
316− go !acc m1 =
317− let stop = acc >>= done
318− single a = acc >>= \b -> step b a >>= done
319− yieldk a r = acc >>= \b -> step b a >>= \x -> go (return x) r
320− in foldStream defState yieldk single stop m1
321−
322-- | Fold a stream using the supplied left 'Fold' and reducing the resulting
307-- | Fold a stream using the supplied left 'Fold' and reducing the resulting
323-- expression strictly at each step. The behavior is similar to 'foldl''. A
308-- expression strictly at each step. The behavior is similar to 'foldl''. A
324-- 'Fold' can terminate early without consuming the full stream. See the
309-- 'Fold' can terminate early without consuming the full stream. See the
@@ -470,11 +455,6 @@ foldConcat
470 Stream.Skip s -> go1 SPEC fs s
455 Stream.Skip s -> go1 SPEC fs s
471 Stream.Stop -> return $ Left fs
456 Stream.Stop -> return $ Left fs
472
457
473−-- | Like 'foldl'' but with a monadic step function.
474−{-# INLINE foldlM' #-}
475−foldlM' :: Monad m => (b -> a -> m b) -> m b -> StreamK m a -> m b
476−foldlM' step begin = foldlMx' step begin return
477−
478------------------------------------------------------------------------------
458------------------------------------------------------------------------------
479-- Specialized folds
459-- Specialized folds
480------------------------------------------------------------------------------
460------------------------------------------------------------------------------
core/src/Streamly/Internal/Data/StreamK/Type.hs+20
@@ -84,6 +84,8 @@ module Streamly.Internal.Data.StreamK.Type
84 -- ** Strict Left Folds
84 -- ** Strict Left Folds
85 , Streamly.Internal.Data.StreamK.Type.foldl'
85 , Streamly.Internal.Data.StreamK.Type.foldl'
86 , foldlx'
86 , foldlx'
87+ , foldlMx'
88+ , foldlM'
87
89
88 -- ** Lazy Right Folds
90 -- ** Lazy Right Folds
89 , Streamly.Internal.Data.StreamK.Type.foldr
91 , Streamly.Internal.Data.StreamK.Type.foldr
@@ -817,6 +819,24 @@ foldlx' step begin done m = get $ go m begin
817foldl' :: Monad m => (b -> a -> b) -> b -> StreamK m a -> m b
819foldl' :: Monad m => (b -> a -> b) -> b -> StreamK m a -> m b
818foldl' step begin = foldlx' step begin id
820foldl' step begin = foldlx' step begin id
819
821
822+-- XXX replace the recursive "go" with explicit continuations.
823+-- | Like 'foldx', but with a monadic step function.
824+{-# INLINABLE foldlMx' #-}
825+foldlMx' :: Monad m
826+ => (x -> a -> m x) -> m x -> (x -> m b) -> StreamK m a -> m b
827+foldlMx' step begin done = go begin
828+ where
829+ go !acc m1 =
830+ let stop = acc >>= done
831+ single a = acc >>= \b -> step b a >>= done
832+ yieldk a r = acc >>= \b -> step b a >>= \x -> go (return x) r
833+ in foldStream defState yieldk single stop m1
834+
835+-- | Like 'foldl'' but with a monadic step function.
836+{-# INLINE foldlM' #-}
837+foldlM' :: Monad m => (b -> a -> m b) -> m b -> StreamK m a -> m b
838+foldlM' step begin = foldlMx' step begin return
839+
820------------------------------------------------------------------------------
840------------------------------------------------------------------------------
821-- Specialized folds
841-- Specialized folds
822------------------------------------------------------------------------------
842------------------------------------------------------------------------------