1{-# OPTIONS_HADDOCK hide #-}
2{-# LANGUAGE BangPatterns #-}
4{-# LANGUAGE MagicHash #-}
5{-# LANGUAGE RecordWildCards #-}
6{-# LANGUAGE UnboxedTuples #-}
7{-# LANGUAGE ScopedTypeVariables #-}
12-- Module : Streamly.Internal.Memory.ArrayStream
13-- Copyright : (c) 2019 Composewell Technologies
16-- Maintainer : streamly@composewell.com
17-- Stability : experimental
20-- Combinators to efficiently manipulate streams of arrays.
22module Streamly.Internal.Memory.ArrayStream
27 -- * Flattening to elements
44import Control.Monad.IO.Class (MonadIO(..))
45-- import Data.Functor.Identity (Identity)
46import Data.Word (Word8)
47import Foreign.ForeignPtr (withForeignPtr)
48import Foreign.Ptr (minusPtr, plusPtr, castPtr)
49import Foreign.Storable (Storable(..))
50import Prelude hiding (length, null, last, map, (!!), read, concat)
52import Streamly.Internal.Memory.Array.Types (Array(..), length)
53import Streamly.Streams.Serial (SerialT)
54import Streamly.Streams.StreamK.Type (IsStream)
56import qualified Streamly.Internal.Memory.Array as A
57import qualified Streamly.Internal.Memory.Array.Types as A
58import qualified Streamly.Internal.Prelude as S
59import qualified Streamly.Streams.StreamD as D
60import qualified Streamly.Streams.Prelude as P
62-- XXX efficiently compare two streams of arrays. Two streams can have chunks
63-- of different sizes, we can handle that in the stream comparison abstraction.
64-- This could be useful e.g. to fast compare whether two files differ.
66-- | Convert a stream of arrays into a stream of their elements.
68-- Same as the following but more efficient:
70-- > concat = S.concatMap A.read
74concat :: (IsStream t, MonadIO m, Storable a) => t m (Array a) -> t m a
75-- concat m = D.fromStreamD $ A.flattenArrays (D.toStreamD m)
76-- concat m = D.fromStreamD $ D.concatMap A.toStreamD (D.toStreamD m)
77concat m = D.fromStreamD $ D.concatMapU A.read (D.toStreamD m)
79-- XXX should we have a reverseArrays API to reverse the stream of arrays
82-- | Convert a stream of arrays into a stream of their elements reversing the
83-- contents of each array before flattening.
86{-# INLINE concatRev #-}
87concatRev :: (IsStream t, MonadIO m, Storable a) => t m (Array a) -> t m a
88concatRev m = D.fromStreamD $ A.flattenArraysRev (D.toStreamD m)
90-- | Flatten a stream of arrays after inserting the given element between
94{-# INLINE interpose #-}
95interpose :: (MonadIO m, IsStream t, Storable a) => a -> t m (Array a) -> t m a
96interpose x = S.interpose x A.read
98{-# INLINE intercalateSuffix #-}
99intercalateSuffix :: (MonadIO m, IsStream t, Storable a)
100 => Array a -> t m (Array a) -> t m a
101intercalateSuffix arr = S.intercalateSuffix arr A.read
103-- | Flatten a stream of arrays appending the given element after each
107{-# INLINE interposeSuffix #-}
108interposeSuffix :: (MonadIO m, IsStream t, Storable a)
109 => a -> t m (Array a) -> t m a
110-- interposeSuffix x = D.fromStreamD . A.unlines x . D.toStreamD
111interposeSuffix x = S.interposeSuffix x A.read
113-- | Split a stream of arrays on a given separator byte, dropping the separator
114-- and coalescing all the arrays between two separators into a single array.
117{-# INLINE splitOn #-}
119 :: (IsStream t, MonadIO m)
124 D.fromStreamD $ D.splitInnerBy (A.breakOn byte) A.spliceTwo $ D.toStreamD s
126{-# INLINE splitOnSuffix #-}
128 :: (IsStream t, MonadIO m)
132-- splitOn byte s = D.fromStreamD $ A.splitOn byte $ D.toStreamD s
133splitOnSuffix byte s =
134 D.fromStreamD $ D.splitInnerBySuffix (A.breakOn byte) A.spliceTwo $ D.toStreamD s
136-- | Coalesce adjacent arrays in incoming stream to form bigger arrays of a
137-- maximum specified size in bytes.
140{-# INLINE compact #-}
141compact :: (MonadIO m, Storable a)
142 => Int -> SerialT m (Array a) -> SerialT m (Array a)
143compact n xs = D.fromStreamD $ A.packArraysChunksOf n (D.toStreamD xs)
145-- | @arraysOf n stream@ groups the elements in the input stream into arrays of
148-- Same as the following but more efficient:
150-- > arraysOf n = S.chunksOf n (A.writeN n)
153{-# INLINE arraysOf #-}
154arraysOf :: (IsStream t, MonadIO m, Storable a)
155 => Int -> t m a -> t m (Array a)
157 D.fromStreamD $ A.fromStreamDArraysOf n (D.toStreamD str)
159-- XXX Both of these implementations of splicing seem to perform equally well.
160-- We need to perform benchmarks over a range of sizes though.
162-- CAUTION! length must more than equal to lengths of all the arrays in the
164{-# INLINE spliceArraysLenUnsafe #-}
165spliceArraysLenUnsafe :: (MonadIO m, Storable a)
166 => Int -> SerialT m (Array a) -> m (Array a)
167spliceArraysLenUnsafe len buffered = do
168 arr <- liftIO $ A.newArray len
169 end <- S.foldlM' writeArr (aEnd arr) buffered
170 return $ arr {aEnd = end}
174 writeArr dst Array{..} =
175 liftIO $ withForeignPtr aStart $ \src -> do
176 let count = aEnd `minusPtr` src
177 A.memcpy (castPtr dst) (castPtr src) count
178 return $ dst `plusPtr` count
180{-# INLINE _spliceArraysBuffered #-}
181_spliceArraysBuffered :: (MonadIO m, Storable a)
182 => SerialT m (Array a) -> m (Array a)
183_spliceArraysBuffered s = do
184 buffered <- P.foldr S.cons S.nil s
185 len <- S.sum (S.map length buffered)
186 spliceArraysLenUnsafe len s
188{-# INLINE spliceArraysRealloced #-}
189spliceArraysRealloced :: forall m a. (MonadIO m, Storable a)
190 => SerialT m (Array a) -> m (Array a)
191spliceArraysRealloced s = do
192 idst <- liftIO $ A.newArray (A.bytesToElemCount (undefined :: a)
195 arr <- S.foldlM' A.spliceWithDoubling idst s
196 liftIO $ A.shrinkToFit arr
198-- | Given a stream of arrays, splice them all together to generate a single
199-- array. The stream must be /finite/.
202{-# INLINE toArray #-}
203toArray :: (MonadIO m, Storable a) => SerialT m (Array a) -> m (Array a)
204toArray = spliceArraysRealloced
205-- spliceArrays = _spliceArraysBuffered
207-- exponentially increasing sizes of the chunks upto the max limit.
208-- XXX this will be easier to implement with parsers/terminating folds
209-- With this we should be able to reduce the number of chunks/allocations.
210-- The reallocation/copy based toArray can also be implemented using this.
213{-# INLINE toArraysInRange #-}
214toArraysInRange :: (IsStream t, MonadIO m, Storable a)
215 => Int -> Int -> Fold m (Array a) b -> Fold m a b
216toArraysInRange low high (Fold step initial extract) =
220-- | Fold the input to a pure buffered stream (List) of arrays.
221{-# INLINE _toArraysOf #-}
222_toArraysOf :: (MonadIO m, Storable a)
223 => Int -> Fold m a (SerialT Identity (Array a))
224_toArraysOf n = FL.lchunksOf n (A.writeNF n) FL.toStream
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list