1{-# LANGUAGE BangPatterns #-}
3{-# LANGUAGE MagicHash #-}
4{-# LANGUAGE RecordWildCards #-}
5{-# LANGUAGE UnboxedTuples #-}
6{-# LANGUAGE ScopedTypeVariables #-}
11-- Module : Streamly.Memory.ArrayStream
12-- Copyright : (c) 2019 Composewell Technologies
15-- Maintainer : harendra.kumar@gmail.com
16-- Stability : experimental
19-- Combinators to efficiently manipulate streams of arrays.
21module Streamly.Memory.ArrayStream
26 -- * Flattening to elements
42import Control.Monad.IO.Class (MonadIO(..))
43-- import Data.Functor.Identity (Identity)
44import Data.Word (Word8)
45import Foreign.ForeignPtr (withForeignPtr)
46import Foreign.Ptr (minusPtr, plusPtr, castPtr)
47import Foreign.Storable (Storable(..))
48import Prelude hiding (length, null, last, map, (!!), read, concat)
50import Streamly.Memory.Array.Types (Array(..), length)
51import Streamly.Streams.Serial (SerialT)
52import Streamly.Streams.StreamK.Type (IsStream)
54import qualified Streamly.Unfold as UF
55import qualified Streamly.Memory.Array.Types as A
56import qualified Streamly.Prelude.Internal as S
57import qualified Streamly.Streams.StreamD as D
58import qualified Streamly.Streams.Prelude as P
60-- XXX efficiently compare two streams of arrays. Two streams can have chunks
61-- of different sizes, we can handle that in the stream comparison abstraction.
62-- This could be useful e.g. to fast compare whether two files differ.
64-- | Convert a stream of arrays into a stream of their elements.
66-- Same as the following but more efficient:
68-- > concat = S.concatMap A.read
72concat :: (IsStream t, MonadIO m, Storable a) => t m (Array a) -> t m a
73-- concat m = D.fromStreamD $ A.flattenArrays (D.toStreamD m)
74-- concat m = D.fromStreamD $ D.concatMap A.toStreamD (D.toStreamD m)
75concat m = D.fromStreamD $ D.concatMapU A.readU (D.toStreamD m)
77-- XXX should we have a reverseArrays API to reverse the stream of arrays
80-- | Convert a stream of arrays into a stream of their elements reversing the
81-- contents of each array before flattening.
84{-# INLINE concatRev #-}
85concatRev :: (IsStream t, MonadIO m, Storable a) => t m (Array a) -> t m a
86concatRev m = D.fromStreamD $ A.flattenArraysRev (D.toStreamD m)
88{-# INLINE intercalateSuffix #-}
89intercalateSuffix :: (MonadIO m, IsStream t, Storable a)
90 => Array a -> t m (Array a) -> t m a
91intercalateSuffix arr = S.intercalateSuffix A.readU arr A.readU
93-- | Flatten a stream of arrays appending the given element after each
97{-# INLINE interposeSuffix #-}
98interposeSuffix :: (MonadIO m, IsStream t, Storable a)
99 => a -> t m (Array a) -> t m a
100-- interposeSuffix x = D.fromStreamD . A.unlines x . D.toStreamD
101interposeSuffix x = S.intercalateSuffix UF.singleton x A.readU
103-- | Split a stream of arrays on a given separator byte, dropping the separator
104-- and coalescing all the arrays between two separators into a single array.
107{-# INLINE splitOn #-}
109 :: (IsStream t, MonadIO m)
114 D.fromStreamD $ D.splitInnerBy (A.breakOn byte) A.spliceTwo $ D.toStreamD s
116{-# INLINE splitOnSuffix #-}
118 :: (IsStream t, MonadIO m)
122-- splitOn byte s = D.fromStreamD $ A.splitOn byte $ D.toStreamD s
123splitOnSuffix byte s =
124 D.fromStreamD $ D.splitInnerBySuffix (A.breakOn byte) A.spliceTwo $ D.toStreamD s
126-- | Coalesce adjacent arrays in incoming stream to form bigger arrays of a
127-- maximum specified size in bytes.
130{-# INLINE compact #-}
131compact :: (MonadIO m, Storable a)
132 => Int -> SerialT m (Array a) -> SerialT m (Array a)
133compact n xs = D.fromStreamD $ A.packArraysChunksOf n (D.toStreamD xs)
135-- | @arraysOf n stream@ groups the elements in the input stream into arrays of
138-- Same as the following but more efficient:
140-- > arraysOf n = S.chunksOf n (A.writeN n)
143{-# INLINE arraysOf #-}
144arraysOf :: (IsStream t, MonadIO m, Storable a)
145 => Int -> t m a -> t m (Array a)
147 D.fromStreamD $ A.fromStreamDArraysOf n (D.toStreamD str)
149-- XXX Both of these implementations of splicing seem to perform equally well.
150-- We need to perform benchmarks over a range of sizes though.
152-- CAUTION! length must more than equal to lengths of all the arrays in the
154{-# INLINE spliceArraysLenUnsafe #-}
155spliceArraysLenUnsafe :: (MonadIO m, Storable a)
156 => Int -> SerialT m (Array a) -> m (Array a)
157spliceArraysLenUnsafe len buffered = do
158 arr <- liftIO $ A.newArray len
159 end <- S.foldlM' writeArr (aEnd arr) buffered
160 return $ arr {aEnd = end}
164 writeArr dst Array{..} =
165 liftIO $ withForeignPtr aStart $ \src -> do
166 let count = aEnd `minusPtr` src
167 A.memcpy (castPtr dst) (castPtr src) count
168 return $ dst `plusPtr` count
170{-# INLINE _spliceArraysBuffered #-}
171_spliceArraysBuffered :: (MonadIO m, Storable a)
172 => SerialT m (Array a) -> m (Array a)
173_spliceArraysBuffered s = do
174 buffered <- P.foldr S.cons S.nil s
175 len <- S.sum (S.map length buffered)
176 spliceArraysLenUnsafe len s
178{-# INLINE spliceArraysRealloced #-}
179spliceArraysRealloced :: forall m a. (MonadIO m, Storable a)
180 => SerialT m (Array a) -> m (Array a)
181spliceArraysRealloced s = do
182 idst <- liftIO $ A.newArray (A.bytesToElemCount (undefined :: a)
185 arr <- S.foldlM' A.spliceWithDoubling idst s
186 liftIO $ A.shrinkToFit arr
188-- | Given a stream of arrays, splice them all together to generate a single
189-- array. The stream must be /finite/.
192{-# INLINE toArray #-}
193toArray :: (MonadIO m, Storable a) => SerialT m (Array a) -> m (Array a)
194toArray = spliceArraysRealloced
195-- spliceArrays = _spliceArraysBuffered
197-- exponentially increasing sizes of the chunks upto the max limit.
198-- XXX this will be easier to implement with parsers/terminating folds
199-- With this we should be able to reduce the number of chunks/allocations.
200-- The reallocation/copy based toArray can also be implemented using this.
203{-# INLINE toArraysInRange #-}
204toArraysInRange :: (IsStream t, MonadIO m, Storable a)
205 => Int -> Int -> Fold m (Array a) b -> Fold m a b
206toArraysInRange low high (Fold step initial extract) =
210-- | Fold the input to a pure buffered stream (List) of arrays.
211{-# INLINE _toArraysOf #-}
212_toArraysOf :: (MonadIO m, Storable a)
213 => Int -> Fold m a (SerialT Identity (Array a))
214_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