on d030361 help sign in
streamly/src/Streamly/Memory/ArrayStream.hs
196 lines 6.3 kB history raw
1{-# LANGUAGE BangPatterns #-}
2{-# LANGUAGE CPP #-}
3{-# LANGUAGE MagicHash #-}
4{-# LANGUAGE RecordWildCards #-}
5{-# LANGUAGE UnboxedTuples #-}
6{-# LANGUAGE ScopedTypeVariables #-}
8#include "inline.hs"
10-- |
11-- Module : Streamly.Memory.ArrayStream
12-- Copyright : (c) 2019 Composewell Technologies
13--
14-- License : BSD3
15-- Maintainer : harendra.kumar@gmail.com
16-- Stability : experimental
17-- Portability : GHC
18--
19-- Combinators to efficiently manipulate streams of arrays.
20--
21module Streamly.Memory.ArrayStream
22 (
23 -- * Creation
24 arraysOf
26 -- * Flattening to elements
27 , flatten
28 -- , _flattenArraysRev
29 , unlinesBy
31 -- * Transformation
32 , splitOn
33 , compact -- compact
35 -- * Elimination
36 , toArray
37 )
38where
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.
62--
63-- Same as the following but more efficient:
64--
65-- > flatten = S.concatMap A.read
66--
67-- @since 0.7.0
68{-# INLINE flatten #-}
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
73-- instead?
74--
75-- | Convert a stream of arrays into a stream of their elements reversing the
76-- contents of each array before flattening.
77--
78-- @since 0.7.0
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
84-- API for that?
85--
86-- | Flatten a stream of arrays appending the given element after each
87-- array.
88--
89-- @since 0.7.0
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.
97--
98-- @since 0.7.0
99{-# INLINE splitOn #-}
100splitOn
101 :: (IsStream t, MonadIO m)
102 => Word8
103 -> t m (Array Word8)
104 -> t m (Array Word8)
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.
109--
110-- @since 0.7.0
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
117-- @n@ elements each.
118--
119-- Same as the following but more efficient:
120--
121-- > arraysOf n = S.chunksOf n (A.writeN n)
122--
123-- @since 0.7.0
124{-# INLINE arraysOf #-}
125arraysOf :: (IsStream t, MonadIO m, Storable a)
126 => Int -> t m a -> t m (Array a)
127arraysOf n str =
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
134-- stream.
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}
143 where
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)
164 (A.mkChunkSizeKB 4))
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/.
171--
172-- @since 0.7.0
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.
182--
183{-
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) =
188-}
190{-
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
196-}