1{-# LANGUAGE BangPatterns #-}
3{-# LANGUAGE MagicHash #-}
4{-# LANGUAGE RecordWildCards #-}
5{-# LANGUAGE UnboxedTuples #-}
6{-# LANGUAGE ScopedTypeVariables #-}
11-- Module : Streamly.Internal.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.Internal.Memory.ArrayStream
26 -- * Flattening to elements
43import Control.Monad.IO.Class (MonadIO(..))
44-- import Data.Functor.Identity (Identity)
45import Data.Word (Word8)
46import Foreign.ForeignPtr (withForeignPtr)
47import Foreign.Ptr (minusPtr, plusPtr, castPtr)
48import Foreign.Storable (Storable(..))
49import Prelude hiding (length, null, last, map, (!!), read, concat)
51import Streamly.Internal.Memory.Array.Types (Array(..), length)
52import Streamly.Streams.Serial (SerialT)
53import Streamly.Streams.StreamK.Type (IsStream)
55import qualified Streamly.Internal.Memory.Array as A
56import qualified Streamly.Internal.Memory.Array.Types as A
57import qualified Streamly.Internal.Prelude as S
58import qualified Streamly.Streams.StreamD as D
59import qualified Streamly.Streams.Prelude as P
61-- XXX efficiently compare two streams of arrays. Two streams can have chunks
62-- of different sizes, we can handle that in the stream comparison abstraction.
63-- This could be useful e.g. to fast compare whether two files differ.
65-- | Convert a stream of arrays into a stream of their elements.
67-- Same as the following but more efficient:
69-- > concat = S.concatMap A.read
73concat :: (IsStream t, MonadIO m, Storable a) => t m (Array a) -> t m a
74-- concat m = D.fromStreamD $ A.flattenArrays (D.toStreamD m)
75-- concat m = D.fromStreamD $ D.concatMap A.toStreamD (D.toStreamD m)
76concat m = D.fromStreamD $ D.concatMapU A.read (D.toStreamD m)
78-- XXX should we have a reverseArrays API to reverse the stream of arrays
81-- | Convert a stream of arrays into a stream of their elements reversing the
82-- contents of each array before flattening.
85{-# INLINE concatRev #-}
86concatRev :: (IsStream t, MonadIO m, Storable a) => t m (Array a) -> t m a
87concatRev m = D.fromStreamD $ A.flattenArraysRev (D.toStreamD m)
89-- | Flatten a stream of arrays after inserting the given element between
93{-# INLINE interpose #-}
94interpose :: (MonadIO m, IsStream t, Storable a) => a -> t m (Array a) -> t m a
95interpose x = S.interpose x A.read
97{-# INLINE intercalateSuffix #-}
98intercalateSuffix :: (MonadIO m, IsStream t, Storable a)
99 => Array a -> t m (Array a) -> t m a
100intercalateSuffix arr = S.intercalateSuffix arr A.read
102-- | Flatten a stream of arrays appending the given element after each
106{-# INLINE interposeSuffix #-}
107interposeSuffix :: (MonadIO m, IsStream t, Storable a)
108 => a -> t m (Array a) -> t m a
109-- interposeSuffix x = D.fromStreamD . A.unlines x . D.toStreamD
110interposeSuffix x = S.interposeSuffix x A.read
112-- | Split a stream of arrays on a given separator byte, dropping the separator
113-- and coalescing all the arrays between two separators into a single array.
116{-# INLINE splitOn #-}
118 :: (IsStream t, MonadIO m)
123 D.fromStreamD $ D.splitInnerBy (A.breakOn byte) A.spliceTwo $ D.toStreamD s
125{-# INLINE splitOnSuffix #-}
127 :: (IsStream t, MonadIO m)
131-- splitOn byte s = D.fromStreamD $ A.splitOn byte $ D.toStreamD s
132splitOnSuffix byte s =
133 D.fromStreamD $ D.splitInnerBySuffix (A.breakOn byte) A.spliceTwo $ D.toStreamD s
135-- | Coalesce adjacent arrays in incoming stream to form bigger arrays of a
136-- maximum specified size in bytes.
139{-# INLINE compact #-}
140compact :: (MonadIO m, Storable a)
141 => Int -> SerialT m (Array a) -> SerialT m (Array a)
142compact n xs = D.fromStreamD $ A.packArraysChunksOf n (D.toStreamD xs)
144-- | @arraysOf n stream@ groups the elements in the input stream into arrays of
147-- Same as the following but more efficient:
149-- > arraysOf n = S.chunksOf n (A.writeN n)
152{-# INLINE arraysOf #-}
153arraysOf :: (IsStream t, MonadIO m, Storable a)
154 => Int -> t m a -> t m (Array a)
156 D.fromStreamD $ A.fromStreamDArraysOf n (D.toStreamD str)
158-- XXX Both of these implementations of splicing seem to perform equally well.
159-- We need to perform benchmarks over a range of sizes though.
161-- CAUTION! length must more than equal to lengths of all the arrays in the
163{-# INLINE spliceArraysLenUnsafe #-}
164spliceArraysLenUnsafe :: (MonadIO m, Storable a)
165 => Int -> SerialT m (Array a) -> m (Array a)
166spliceArraysLenUnsafe len buffered = do
167 arr <- liftIO $ A.newArray len
168 end <- S.foldlM' writeArr (aEnd arr) buffered
169 return $ arr {aEnd = end}
173 writeArr dst Array{..} =
174 liftIO $ withForeignPtr aStart $ \src -> do
175 let count = aEnd `minusPtr` src
176 A.memcpy (castPtr dst) (castPtr src) count
177 return $ dst `plusPtr` count
179{-# INLINE _spliceArraysBuffered #-}
180_spliceArraysBuffered :: (MonadIO m, Storable a)
181 => SerialT m (Array a) -> m (Array a)
182_spliceArraysBuffered s = do
183 buffered <- P.foldr S.cons S.nil s
184 len <- S.sum (S.map length buffered)
185 spliceArraysLenUnsafe len s
187{-# INLINE spliceArraysRealloced #-}
188spliceArraysRealloced :: forall m a. (MonadIO m, Storable a)
189 => SerialT m (Array a) -> m (Array a)
190spliceArraysRealloced s = do
191 idst <- liftIO $ A.newArray (A.bytesToElemCount (undefined :: a)
194 arr <- S.foldlM' A.spliceWithDoubling idst s
195 liftIO $ A.shrinkToFit arr
197-- | Given a stream of arrays, splice them all together to generate a single
198-- array. The stream must be /finite/.
201{-# INLINE toArray #-}
202toArray :: (MonadIO m, Storable a) => SerialT m (Array a) -> m (Array a)
203toArray = spliceArraysRealloced
204-- spliceArrays = _spliceArraysBuffered
206-- exponentially increasing sizes of the chunks upto the max limit.
207-- XXX this will be easier to implement with parsers/terminating folds
208-- With this we should be able to reduce the number of chunks/allocations.
209-- The reallocation/copy based toArray can also be implemented using this.
212{-# INLINE toArraysInRange #-}
213toArraysInRange :: (IsStream t, MonadIO m, Storable a)
214 => Int -> Int -> Fold m (Array a) b -> Fold m a b
215toArraysInRange low high (Fold step initial extract) =
219-- | Fold the input to a pure buffered stream (List) of arrays.
220{-# INLINE _toArraysOf #-}
221_toArraysOf :: (MonadIO m, Storable a)
222 => Int -> Fold m a (SerialT Identity (Array a))
223_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