on fb39bc3 help sign in
474 lines 17 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.Array
12-- Copyright : (c) 2019 Harendra Kumar
13--
14-- License : BSD3
15-- Maintainer : harendra.kumar@gmail.com
16-- Stability : experimental
17-- Portability : GHC
18--
19-- This module provides immutable arrays suitable for storage and random
20-- access. Once created an array cannot be mutated without copying. Arrays in
21-- this module are chunks of memory that hold a sequence of 'Storable' values
22-- of a given type. They are designed to store serializable data, they cannot
23-- store non-serializable data like functions, this is reflected by the
24-- 'Storable' constraint. For efficient buffering of data, arrays use pinned
25-- memory and therefore can hold arbitrary number of elements with a single
26-- movable pointer visible to GC, therefore, adding no pressure to GC.
27-- Moreover, pinned memory allows communication with foreign consumers and
28-- producers (e.g. file or network IO) without copying the data.
29--
30-- By design, there are no array transformation operations provided in this
31-- module, only conversion to and from stream is provided. Arrays can be
32-- operated upon /efficiently/ by converting them into a stream, applying the
33-- desired stream transformations from "Streamly.Prelude" and then converting
34-- it back to an array.
35--
36-- This module is designed to be imported qualified:
37--
38-- > import qualified Streamly.Array as A
40-- To summarize:
41--
42-- Arrays are finite and fixed in size
43-- provide /O(1)/ access to elements
44-- store only data and not functions
45-- provide efficient IO interfacing
46--
47-- 'Foldable' instance is not provided because the implementation would be much
48-- less efficient compared to folding via streams. 'Semigroup' and 'Monoid'
49-- instances are deliberately not provided to avoid misuse; concatenating
50-- arrays using binary operations can be highly inefficient. Instead, use
51-- 'Streamly.Memory.Array.Stream.toArray' to concatenate N arrays at once.
52--
53-- Each array is one pointer visible to the GC. Too many small arrays (e.g.
54-- single byte) are only as good as holding those elements in a Haskell list.
55-- However, small arrays can be compacted into large ones to reduce the
56-- overhead. To hold 32GB memory in 32k sized buffers we need 1 million arrays
57-- if we use one array for each chunk. This is still significant to add
58-- pressure to GC. However, we can create arrays of arrays (trees) to scale to
59-- arbitrarily large amounts of memory but still using small chunks of
60-- contiguous memory.
62module Streamly.Memory.Array
63 (
64 Array
66 -- , defaultChunkSize
68 -- * Arrays
69 -- ** Construction
70 -- | When performance matters, the fastest way to generate an array is
71 -- 'writeN'. For regular use, 'IsList' and 'IsString' instances can be
72 -- used to conveniently construct arrays from literal values.
73 -- 'OverloadedLists' extension or 'fromList' can be used to construct an
74 -- array from a list literal. Similarly, 'OverloadedStrings' extension or
75 -- 'fromList' can be used to construct an array from a string literal.
77 -- Pure List APIs
78 , A.fromListN
79 , A.fromList
81 -- Monadic APIs
82 -- , newArray
83 , A.writeN
84 , write
86 -- Stream Folds
87 -- , writeNS
88 -- , writeS
90 -- ** Elimination
91 -- 'GHC.Exts.toList' from "GHC.Exts" can be used to convert an array to a
92 -- list.
94 , A.toList
95 , A.read
96 , readRev
98 -- ** Random Access
99 , length
100 -- , (!!)
102 , readIndex
103 {-
104 , readSlice
105 , readSliceRev
106 -}
108 -- , writeIndex
109 {-
110 , writeSlice
111 , writeSliceRev
112 -}
114 {-
115 -- * Immutable Transformations
116 , runTransform
118 -- * Folding Arrays
119 -- , runStreamFold
120 , runFold
121 -}
123 -- * Streams of Arrays
124 -- ** Creation
125 , AS.arraysOf
127 -- ** Elimination
128 , AS.concat
129 -- , AS.concatRev
130 , AS.concatWithSuffix
131 , AS.toArray
133 -- ** Transformation
134 , AS.splitOn
135 , AS.compact -- compact
136 )
137where
139import Control.Monad (when)
140import Control.Monad.IO.Class (MonadIO(..))
141-- import Data.Functor.Identity (Identity)
142import Foreign.ForeignPtr (withForeignPtr)
143import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
144import Foreign.Ptr (minusPtr, plusPtr)
145import Foreign.Storable (Storable(..))
146import Prelude hiding (length, null, last, map, (!!), read, concat)
148import Streamly.Memory.Array.Types (Array(..), length)
149import Streamly.Streams.Serial (SerialT)
150import Streamly.Streams.StreamK.Type (IsStream)
151import Streamly.Fold.Types (Fold(..))
153import qualified Streamly.Memory.Array.Types as A
154import qualified Streamly.Memory.ArrayStream as AS
155import qualified Streamly.Streams.Prelude as P
156import qualified Streamly.Streams.Serial as Serial
157import qualified Streamly.Streams.StreamD as D
159-------------------------------------------------------------------------------
160-- Construction
161-------------------------------------------------------------------------------
163{-
164-- | Create a new uninitialized array of given length.
165--
166-- @since 0.7.0
167newArray :: (MonadIO m, Storable a) => Int -> m (Array a)
168newArray len = undefined
169-}
171-- | Create an 'Array' from the first N elements of a stream. The array is
172-- allocated to size N, if the stream terminates before N elements then the
173-- array may hold less than N elements.
174--
175-- @since 0.7.0
176{-# INLINE _writeNS #-}
177_writeNS :: (MonadIO m, Storable a) => Int -> SerialT m a -> m (Array a)
178_writeNS n m = do
179 if n < 0 then error "writeN: negative write count specified" else return ()
180 A.fromStreamDN n $ D.toStreamD m
182-- XXX The realloc based implementation needs to make one extra copy if we use
183-- shrinkToFit. On the other hand, the stream of arrays implementation may
184-- buffer the array chunk pointers in memory but it does not have to shrink as
185-- we know the exact size in the end. However, memory copying does not seems to
186-- be as expensive as the allocations. Therefore, we need to reduce the number
187-- of allocations instead. Also, the size of allocations matters, right sizing
188-- an allocation even at the cost of copying sems to help. Should be measured
189-- on a big stream with heavy calls to toArray to see the effect.
190--
191-- XXX check if GHC's memory allocator is efficient enough. We can try the C
192-- malloc to compare against.
194{-# INLINE_NORMAL toArrayMinChunk #-}
195toArrayMinChunk :: forall m a. (MonadIO m, Storable a)
196 => Int -> Fold m a (Array a)
197-- toArrayMinChunk n = FL.mapM spliceArrays $ toArraysOf n
198toArrayMinChunk elemCount = Fold step initial extract
200 where
202 insertElem (Array start end bound) x = do
203 liftIO $ poke end x
204 return $ Array start (end `plusPtr` sizeOf (undefined :: a)) bound
206 initial = do
207 when (elemCount < 0) $ error "toArrayMinChunk: elemCount is negative"
208 liftIO $ A.newArray elemCount
209 step arr@(Array start end bound) x | end == bound = do
210 let p = unsafeForeignPtrToPtr start
211 oldSize = end `minusPtr` p
212 newSize = max (oldSize * 2) 1
213 arr1 <- liftIO $ A.realloc newSize arr
214 insertElem arr1 x
215 step arr x = insertElem arr x
216 extract = liftIO . A.shrinkToFit
218-- | Fold the whole input to a single array.
219--
220-- /Caution! Do not use this on infinite streams./
221--
222-- @since 0.7.0
223{-# INLINE write #-}
224write :: forall m a. (MonadIO m, Storable a) => Fold m a (Array a)
225write = toArrayMinChunk (A.bytesToElemCount (undefined :: a) (A.mkChunkSize 1024))
227-- | Create an 'Array' from a stream. This is useful when we want to create a
228-- single array from a stream of unknown size. 'writeN' is at least twice
229-- as efficient when the size is already known.
230--
231-- Note that if the input stream is too large memory allocation for the array
232-- may fail. When the stream size is not known, `arraysOf` followed by
233-- processing of indvidual arrays in the resulting stream should be preferred.
234--
235{-# INLINE _writeS #-}
236_writeS :: (MonadIO m, Storable a) => SerialT m a -> m (Array a)
237_writeS = P.runFold write
238-- write m = A.fromStreamD $ D.toStreamD m
240-------------------------------------------------------------------------------
241-- Elimination
242-------------------------------------------------------------------------------
244-- | Convert an 'Array' into a stream in reverse order.
245--
246-- @since 0.7.0
247{-# INLINE_EARLY readRev #-}
248readRev :: (Monad m, IsStream t, Storable a) => Array a -> t m a
249readRev = D.fromStreamD . A.toStreamDRev
250-- XXX add fallback to StreamK rule
251-- {-# RULES "Streamly.Array.readRev fallback to StreamK" [1]
252-- forall a. S.toStreamK (readRev a) = K.revFromArray a #-}
254{-# INLINE _null #-}
255_null :: Storable a => Array a -> Bool
256_null arr = length arr == 0
258{-# INLINE _last #-}
259_last :: Storable a => Array a -> Maybe a
260_last arr = readIndex arr (length arr - 1)
262-------------------------------------------------------------------------------
263-- Random Access
264-------------------------------------------------------------------------------
266-------------------------------------------------------------------------------
267-- Searching
268-------------------------------------------------------------------------------
270{-
271-- | Perform a binary search in the array to find an element.
272bsearch :: a -> Array a -> Maybe Bool
273bsearch = undefined
275-- | Perform a binary search in the array to find an element index.
276{-# INLINE elemIndex #-}
277bsearchIndex :: a -> Array a -> Maybe Int
278bsearchIndex elem arr = undefined
280-- find/findIndex etc can potentially be implemented more efficiently on arrays
281-- compared to streams by using SIMD instructions.
283find :: (a -> Bool) -> Array a -> Bool
284find = undefined
286findIndex :: (a -> Bool) -> Array a -> Maybe Int
287findIndex = undefined
289findIndices :: (a -> Bool) -> Array a -> Array Int
290findIndices = undefined
291-}
293-------------------------------------------------------------------------------
294-- Folds
295-------------------------------------------------------------------------------
297-- XXX We can potentially use SIMD instructions on arrays to fold faster.
299-------------------------------------------------------------------------------
300-- Slice and splice
301-------------------------------------------------------------------------------
303{-
304slice :: Int -> Int -> Array a
305slice begin end arr = undefined
307splitAt :: Int -> Array a -> (Array a, Array a)
308splitAt i arr = undefined
310-- XXX This operation can be performed efficiently via streams.
311-- | Append two arrays together to create a single array.
312splice :: Array a -> Array a -> Array a
313splice arr1 arr2 = undefined
315-------------------------------------------------------------------------------
316-- In-place mutation APIs
317-------------------------------------------------------------------------------
319-- | Partition an array into two halves using a partitioning predicate. The
320-- first half retains values where the predicate is 'False' and the second half
321-- retains values where the predicate is 'True'.
322{-# INLINE partitionBy #-}
323partitionBy :: (a -> Bool) -> Array a -> (Array a, Array a)
324partitionBy f arr = undefined
326-- | Shuffle corresponding elements from two arrays using a shuffle function.
327-- If the shuffle function returns 'False' then do nothing otherwise swap the
328-- elements. This can be used in a bottom up fold to shuffle or reorder the
329-- elements.
330shuffleBy :: (a -> a -> m Bool) -> Array a -> Array a -> m (Array a)
331shuffleBy f arr1 arr2 = undefined
333-- XXX we can also make the folds partial by stopping at a certain level.
334--
335-- | Perform a top down hierarchical recursive partitioning fold of items in
336-- the container using the given function as the partition function.
337--
338-- This will perform a quick sort if the partition function is
339-- 'partitionBy (< pivot)'.
340--
341-- @since 0.7.0
342{-# INLINABLE foldtWith #-}
343foldtWith :: Int -> (Array a -> Array a -> m (Array a)) -> Array a -> m (Array a)
344foldtWith level f = undefined
346-- | Perform a pairwise bottom up fold recursively merging the pairs. Level
347-- indicates the level in the tree where the fold would stop.
348--
349-- This will perform a random shuffle if the shuffle function is random.
350-- If we stop at level 0 and repeatedly apply the function then we can do a
351-- bubble sort.
352foldbWith :: Int -> (Array a -> Array a -> m (Array a)) -> Array a -> m (Array a)
353foldbWith level f = undefined
354-}
356-- XXX consider the bulk update/accumulation/permutation APIs from vector.
358-------------------------------------------------------------------------------
359-- Random reads and writes
360-------------------------------------------------------------------------------
362-- | /O(1)/ Lookup the element at the given index, starting from 0.
363--
364-- @since 0.7.0
365{-# INLINE readIndex #-}
366readIndex :: Storable a => Array a -> Int -> Maybe a
367readIndex arr i =
368 if i < 0 || i > length arr - 1
369 then Nothing
370 else A.unsafeInlineIO $
371 withForeignPtr (aStart arr) $ \p -> fmap Just $ peekElemOff p i
373{-
374-- | @readSlice arr i count@ streams a slice of the array @arr@ starting
375-- at index @i@ and reading up to @count@ elements in the forward direction
376-- ending at the index @i + count - 1@.
377--
378-- @since 0.7.0
379{-# INLINE readSlice #-}
380readSlice :: (IsStream t, Monad m, Storable a)
381 => Array a -> Int -> Int -> t m a
382readSlice arr i len = undefined
384-- | @readSliceRev arr i count@ streams a slice of the array @arr@ starting at
385-- index @i@ and reading up to @count@ elements in the reverse direction ending
386-- at the index @i - count + 1@.
387--
388-- @since 0.7.0
389{-# INLINE readSliceRev #-}
390readSliceRev :: (IsStream t, Monad m, Storable a)
391 => Array a -> Int -> Int -> t m a
392readSliceRev arr i len = undefined
393-}
395-- | /O(1)/ Write the given element at the given index in the array.
396-- Performs in-place mutation of the array.
397--
398-- @since 0.7.0
399{-# INLINE _writeIndex #-}
400_writeIndex :: (MonadIO m, Storable a) => Array a -> Int -> a -> m ()
401_writeIndex arr i a = do
402 let maxIndex = length arr - 1
403 if i < 0
404 then error "writeIndex: negative array index"
405 else if i > maxIndex
406 then error $ "writeIndex: specified array index " ++ show i
407 ++ " is beyond the maximum index " ++ show maxIndex
408 else
409 liftIO $ withForeignPtr (aStart arr) $ \p ->
410 pokeElemOff p i a
412{-
413-- | @writeSlice arr i count stream@ writes a stream to the array @arr@
414-- starting at index @i@ and writing up to @count@ elements in the forward
415-- direction ending at the index @i + count - 1@.
416--
417-- @since 0.7.0
418{-# INLINE writeSlice #-}
419writeSlice :: (IsStream t, Monad m, Storable a)
420 => Array a -> Int -> Int -> t m a -> m ()
421writeSlice arr i len s = undefined
423-- | @writeSliceRev arr i count stream@ writes a stream to the array @arr@
424-- starting at index @i@ and writing up to @count@ elements in the reverse
425-- direction ending at the index @i - count + 1@.
426--
427-- @since 0.7.0
428{-# INLINE writeSliceRev #-}
429writeSliceRev :: (IsStream t, Monad m, Storable a)
430 => Array a -> Int -> Int -> t m a -> m ()
431writeSliceRev arr i len s = undefined
432-}
434-------------------------------------------------------------------------------
435-- Transform via stream operations
436-------------------------------------------------------------------------------
438-- for non-length changing operations we can use the original length for
439-- allocation. If we can predict the length then we can use the prediction for
440-- new allocation. Otherwise we can use a hint and adjust dynamically.
442{-
443-- | Transform an array into another array using a pipe transformation
444-- operation.
445--
446-- @since 0.7.0
447{-# INLINE runPipe #-}
448runPipe :: (MonadIO m, Storable a, Storable b)
449 => Pipe m a b -> Array a -> m (Array b)
450runPipe f arr = P.runPipe (toArrayMinChunk (length arr)) $ f (A.read arr)
451-}
453-- | Transform an array into another array using a stream transformation
454-- operation.
455--
456-- @since 0.7.0
457{-# INLINE _runTransform #-}
458_runTransform :: (MonadIO m, Storable a, Storable b)
459 => (SerialT m a -> SerialT m b) -> Array a -> m (Array b)
460_runTransform f arr = P.runFold (toArrayMinChunk (length arr)) $ f (A.read arr)
462-- | Fold an array using a 'Fold'.
463--
464-- @since 0.7.0
465{-# INLINE _runFold #-}
466_runFold :: forall m a b. (MonadIO m, Storable a) => Fold m a b -> Array a -> m b
467_runFold f arr = P.runFold f $ (A.read arr :: Serial.SerialT m a)
469-- | Fold an array using a stream fold operation.
470--
471-- @since 0.7.0
472{-# INLINE _runStreamFold #-}
473_runStreamFold :: (MonadIO m, Storable a) => (SerialT m a -> m b) -> Array a -> m b
474_runStreamFold f arr = f (A.read arr)