on master help sign in
Make changes for "reader" based impl of concatChunks
commitffea484db84da2e8bd00c40f78ab14c38999e308 authorHarendra Kumar <harendra@composewell.com> dateSun 31 Dec 2023, 17:02 (3 years ago) parent2417874
2 files changed+56−58
core/src/Streamly/Internal/Data/Array.hs−51 core/src/Streamly/Internal/Data/Array/Type.hs+56−7
core/src/Streamly/Internal/Data/Array.hs−51
@@ -23,11 +23,6 @@ module Streamly.Internal.Data.Array
2323 -- Monadic Folds
2424 , writeLastN
2525
26− -- * Unfolds
27− , reader
28− , readerUnsafe
29− , producer -- experimental
30−
3126 -- * Random Access
3227 -- , (!!)
3328 , getIndex
@@ -99,7 +94,6 @@ import Prelude hiding (length, null, last, map, (!!), read, concat)
9994import Streamly.Internal.Data.MutByteArray.Type (PinnedState(..))
10095import Streamly.Internal.Data.Serialize.Type (Serialize)
10196import Streamly.Internal.Data.Fold.Type (Fold(..))
102−import Streamly.Internal.Data.Producer.Type (Producer(..))
10397import Streamly.Internal.Data.Stream (Stream)
10498import Streamly.Internal.Data.Tuple.Strict (Tuple3Fused'(..))
10599import Streamly.Internal.Data.Unfold.Type (Unfold(..))
@@ -110,8 +104,6 @@ import qualified Streamly.Internal.Data.MutByteArray.Type as MBA
110104import qualified Streamly.Internal.Data.MutArray as MA
111105import qualified Streamly.Internal.Data.Array.Type as A
112106import qualified Streamly.Internal.Data.Fold as FL
113−import qualified Streamly.Internal.Data.Producer.Type as Producer
114−import qualified Streamly.Internal.Data.Producer as Producer
115107import qualified Streamly.Internal.Data.Ring as RB
116108import qualified Streamly.Internal.Data.Stream as D
117109import qualified Streamly.Internal.Data.Stream as Stream
@@ -148,49 +140,6 @@ import Streamly.Internal.Data.Array.Type
148140-- Elimination
149141-------------------------------------------------------------------------------
150142
151−{-# INLINE_NORMAL producer #-}
152−producer :: forall m a. (Monad m, Unbox a) => Producer m (Array a) a
153−producer =
154− Producer.translate A.unsafeThaw A.unsafeFreeze
155− $ MA.producerWith (return . unsafeInlineIO)
156−
157−-- | Unfold an array into a stream.
158−--
159−{-# INLINE_NORMAL reader #-}
160−reader :: forall m a. (Monad m, Unbox a) => Unfold m (Array a) a
161−reader = Producer.simplify producer
162−
163−-- | Unfold an array into a stream, does not check the end of the array, the
164−-- user is responsible for terminating the stream within the array bounds. For
165−-- high performance application where the end condition can be determined by
166−-- a terminating fold.
167−--
168−-- Written in the hope that it may be faster than "read", however, in the case
169−-- for which this was written, "read" proves to be faster even though the core
170−-- generated with unsafeRead looks simpler.
171−--
172−-- /Pre-release/
173−--
174−{-# INLINE_NORMAL readerUnsafe #-}
175−readerUnsafe :: forall m a. (Monad m, Unbox a) => Unfold m (Array a) a
176−readerUnsafe = Unfold step inject
177− where
178−
179− inject (Array contents start end) =
180− return (ArrayUnsafe contents end start)
181−
182− {-# INLINE_LATE step #-}
183− step (ArrayUnsafe contents end p) = do
184− -- unsafeInlineIO allows us to run this in Identity monad for pure
185− -- toList/foldr case which makes them much faster due to not
186− -- accumulating the list and fusing better with the pure consumers.
187− --
188− -- This should be safe as the array contents are guaranteed to be
189− -- evaluated/written to before we peek at them.
190− let !x = unsafeInlineIO $ peekAt p contents
191− let !p1 = INDEX_NEXT(p,a)
192− return $ D.Yield x (ArrayUnsafe contents end p1)
193−
194143-- |
195144--
196145-- >>> null arr = Array.byteLength arr == 0
core/src/Streamly/Internal/Data/Array/Type.hs+56−7
@@ -54,6 +54,11 @@ module Streamly.Internal.Data.Array.Type
5454 , clone
5555 , pinnedClone
5656
57+ -- * Unfolds
58+ , reader
59+ , readerUnsafe
60+ , producer -- experimental
61+
5762 -- ** Elimination
5863 , unsafeIndexIO
5964 , getIndexUnsafe
@@ -76,7 +81,7 @@ module Streamly.Internal.Data.Array.Type
7681 , pinnedWriteN
7782 , writeNUnsafe
7883 , pinnedWriteNUnsafe
79− , MA.ArrayUnsafe (..)
84+ -- , MA.ArrayUnsafe (..)
8085 , pinnedWriteNAligned
8186 , write
8287 , pinnedWrite
@@ -121,6 +126,7 @@ import GHC.Exts (IsList, IsString(..), Addr#)
121126
122127import GHC.IO (unsafePerformIO)
123128import GHC.Ptr (Ptr(..))
129+import Streamly.Internal.Data.Producer.Type (Producer(..))
124130import Streamly.Internal.Data.MutArray.Type (MutArray(..))
125131import Streamly.Internal.Data.MutByteArray.Type (MutByteArray)
126132import Streamly.Internal.Data.Fold.Type (Fold(..))
@@ -137,6 +143,7 @@ import qualified Streamly.Internal.Data.MutArray.Type as MA
137143import qualified Streamly.Internal.Data.Stream.Type as D
138144import qualified Streamly.Internal.Data.StreamK.Type as K
139145import qualified Streamly.Internal.Data.MutByteArray.Type as Unboxed
146+import qualified Streamly.Internal.Data.Producer as Producer
140147import qualified Streamly.Internal.Data.Unfold.Type as Unfold
141148import qualified Text.ParserCombinators.ReadPrec as ReadPrec
142149
@@ -414,16 +421,15 @@ pinnedChunksOf :: forall m a. (MonadIO m, Unbox a)
414421 => Int -> D.Stream m a -> D.Stream m (Array a)
415422pinnedChunksOf n str = D.map unsafeFreeze $ MA.pinnedChunksOf n str
416423
417−-- | Use the "reader" unfold instead.
418−--
419−-- @concatChunks = unfoldMany reader@
424+-- | Convert a stream of arrays into a stream of their elements.
420425--
421−-- We can try this if there are any fusion issues in the unfold.
426+-- >>> concatChunks = Stream.unfoldMany Array.reader
422427--
423428{-# INLINE_NORMAL concatChunks #-}
424−concatChunks :: forall m a. (MonadIO m, Unbox a)
425− => D.Stream m (Array a) -> D.Stream m a
429+concatChunks :: (MonadIO m, Unbox a) => Stream m (Array a) -> Stream m a
430+-- XXX this requires MonadIO whereas the unfoldMany version does not
426431concatChunks = MA.concatChunks . D.map unsafeThaw
432+-- concatChunks = D.unfoldMany reader
427433
428434{-# DEPRECATED flattenArrays "Please use \"unfoldMany reader\" instead." #-}
429435flattenArrays :: forall m a. (MonadIO m, Unbox a)
@@ -488,6 +494,49 @@ byteLength = MA.byteLength . unsafeThaw
488494length :: Unbox a => Array a -> Int
489495length arr = MA.length (unsafeThaw arr)
490496
497+{-# INLINE_NORMAL producer #-}
498+producer :: forall m a. (Monad m, Unbox a) => Producer m (Array a) a
499+producer =
500+ Producer.translate unsafeThaw unsafeFreeze
501+ $ MA.producerWith (return . unsafeInlineIO)
502+
503+-- | Unfold an array into a stream.
504+--
505+{-# INLINE_NORMAL reader #-}
506+reader :: forall m a. (Monad m, Unbox a) => Unfold m (Array a) a
507+reader = Producer.simplify producer
508+
509+-- | Unfold an array into a stream, does not check the end of the array, the
510+-- user is responsible for terminating the stream within the array bounds. For
511+-- high performance application where the end condition can be determined by
512+-- a terminating fold.
513+--
514+-- Written in the hope that it may be faster than "read", however, in the case
515+-- for which this was written, "read" proves to be faster even though the core
516+-- generated with unsafeRead looks simpler.
517+--
518+-- /Pre-release/
519+--
520+{-# INLINE_NORMAL readerUnsafe #-}
521+readerUnsafe :: forall m a. (Monad m, Unbox a) => Unfold m (Array a) a
522+readerUnsafe = Unfold step inject
523+ where
524+
525+ inject (Array contents start end) =
526+ return (MA.ArrayUnsafe contents end start)
527+
528+ {-# INLINE_LATE step #-}
529+ step (MA.ArrayUnsafe contents end p) = do
530+ -- unsafeInlineIO allows us to run this in Identity monad for pure
531+ -- toList/foldr case which makes them much faster due to not
532+ -- accumulating the list and fusing better with the pure consumers.
533+ --
534+ -- This should be safe as the array contents are guaranteed to be
535+ -- evaluated/written to before we peek at them.
536+ let !x = unsafeInlineIO $ peekAt p contents
537+ let !p1 = INDEX_NEXT(p,a)
538+ return $ D.Yield x (MA.ArrayUnsafe contents end p1)
539+
491540-- | Unfold an array into a stream in reverse order.
492541--
493542{-# INLINE_NORMAL readerRev #-}