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
28 -- , _flattenArraysRev
40import Control.Monad.IO.Class (MonadIO(..))
41-- import Data.Functor.Identity (Identity)
42import Data.Word (Word8)
43import Foreign.ForeignPtr (withForeignPtr)
44import Foreign.Ptr (minusPtr, plusPtr, castPtr)
45import Foreign.Storable (Storable(..))
46import Prelude hiding (length, null, last, map, (!!), read)
48import Streamly.Memory.Array.Types (Array(..), length)
49import Streamly.Streams.Serial (SerialT)
50import Streamly.Streams.StreamK.Type (IsStream)
52import qualified Streamly.Memory.Array.Types as A
53import qualified Streamly.Prelude as S
54import qualified Streamly.Streams.StreamD as D
55import qualified Streamly.Streams.Prelude as P
57-- XXX efficiently compare two streams of arrays. Two streams can have chunks
58-- of different sizes, we can handle that in the stream comparison abstraction.
59-- This could be useful e.g. to fast compare whether two files differ.
61-- | Convert a stream of arrays into a stream of their elements.
63-- Same as the following but more efficient:
65-- > flatten = S.concatMap A.read
69flatten :: (IsStream t, MonadIO m, Storable a) => t m (Array a) -> t m a
70flatten m = D.fromStreamD $ A.flattenArrays (D.toStreamD m)
72-- XXX should we have a reverseArrays API to reverse the stream of arrays
75-- | Convert a stream of arrays into a stream of their elements reversing the
76-- contents of each array before flattening.
79{-# INLINE _flattenArraysRev #-}
80_flattenArraysRev :: (IsStream t, MonadIO m, Storable a) => t m (Array a) -> t m a
81_flattenArraysRev m = D.fromStreamD $ A.flattenArraysRev (D.toStreamD m)
83-- XXX use an Array instead as separator? Or use a separate unlinesArraysBySeq
86-- | Flatten a stream of arrays appending the given element after each
90{-# INLINE unlinesBy #-}
91unlinesBy :: (MonadIO m, IsStream t, Storable a)
92 => a -> t m (Array a) -> t m a
93unlinesBy x = D.fromStreamD . A.unlines x . D.toStreamD
95-- | Split a stream of arrays on a given separator byte, dropping the separator
96-- and coalescing all the arrays between two separators into a single array.
101 :: (IsStream t, MonadIO m)
105splitOn byte s = D.fromStreamD $ A.splitOn byte $ D.toStreamD s
107-- | Coalesce adjacent arrays in incoming stream to form bigger arrays of a
108-- maximum specified size in bytes.
111{-# INLINE compact #-}
112compact :: (MonadIO m, Storable a)
113 => Int -> SerialT m (Array a) -> SerialT m (Array a)
114compact n xs = D.fromStreamD $ A.packArraysChunksOf n (D.toStreamD xs)
116-- | @arraysOf n stream@ groups the elements in the input stream into arrays of
119-- Same as the following but more efficient:
121-- > arraysOf n = S.chunksOf n (A.writeN n)
124{-# INLINE arraysOf #-}
125arraysOf :: (IsStream t, MonadIO m, Storable a)
126 => Int -> t m a -> t m (Array a)
128 D.fromStreamD $ A.fromStreamDArraysOf n (D.toStreamD str)
130-- XXX Both of these implementations of splicing seem to perform equally well.
131-- We need to perform benchmarks over a range of sizes though.
133-- CAUTION! length must more than equal to lengths of all the arrays in the
135{-# INLINE spliceArraysLenUnsafe #-}
136spliceArraysLenUnsafe :: (MonadIO m, Storable a)
137 => Int -> SerialT m (Array a) -> m (Array a)
138spliceArraysLenUnsafe len buffered = do
139 arr <- liftIO $ A.newArray len
140 end <- S.foldlM' writeArr (aEnd arr) buffered
141 return $ arr {aEnd = end}
145 writeArr dst Array{..} =
146 liftIO $ withForeignPtr aStart $ \src -> do
147 let count = aEnd `minusPtr` src
148 A.memcpy (castPtr dst) (castPtr src) count
149 return $ dst `plusPtr` count
151{-# INLINE _spliceArraysBuffered #-}
152_spliceArraysBuffered :: (MonadIO m, Storable a)
153 => SerialT m (Array a) -> m (Array a)
154_spliceArraysBuffered s = do
155 buffered <- P.foldr S.cons S.nil s
156 len <- S.sum (S.map length buffered)
157 spliceArraysLenUnsafe len s
159{-# INLINE spliceArraysRealloced #-}
160spliceArraysRealloced :: forall m a. (MonadIO m, Storable a)
161 => SerialT m (Array a) -> m (Array a)
162spliceArraysRealloced s = do
163 idst <- liftIO $ A.newArray (A.bytesToElemCount (undefined :: a)
166 arr <- S.foldlM' A.spliceWithDoubling idst s
167 liftIO $ A.shrinkToFit arr
169-- | Given a stream of arrays, splice them all together to generate a single
170-- array. The stream must be /finite/.
173{-# INLINE toArray #-}
174toArray :: (MonadIO m, Storable a) => SerialT m (Array a) -> m (Array a)
175toArray = spliceArraysRealloced
176-- spliceArrays = _spliceArraysBuffered
178-- exponentially increasing sizes of the chunks upto the max limit.
179-- XXX this will be easier to implement with parsers/terminating folds
180-- With this we should be able to reduce the number of chunks/allocations.
181-- The reallocation/copy based toArray can also be implemented using this.
184{-# INLINE toArraysInRange #-}
185toArraysInRange :: (IsStream t, MonadIO m, Storable a)
186 => Int -> Int -> Fold m (Array a) b -> Fold m a b
187toArraysInRange low high (Fold step initial extract) =
191-- | Fold the input to a pure buffered stream (List) of arrays.
192{-# INLINE _toArraysOf #-}
193_toArraysOf :: (MonadIO m, Storable a)
194 => Int -> Fold m a (SerialT Identity (Array a))
195_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