Copyright | (c) 2019 Composewell Technologies |
---|---|
License | BSD3 |
Maintainer | streamly@composewell.com |
Stability | experimental |
Portability | GHC |
Safe Haskell | None |
Language | Haskell2010 |
To summarize:
- Arrays are finite and fixed in size
- provide O(1) access to elements
- store only data and not functions
- provide efficient IO interfacing
Foldable
instance is not provided because the implementation would be much
less efficient compared to folding via streams. Semigroup
and Monoid
instances should be used with care; concatenating arrays using binary
operations can be highly inefficient. Instead, use
toArray
to concatenate N
arrays at once.
Each array is one pointer visible to the GC. Too many small arrays (e.g. single byte) are only as good as holding those elements in a Haskell list. However, small arrays can be compacted into large ones to reduce the overhead. To hold 32GB memory in 32k sized buffers we need 1 million arrays if we use one array for each chunk. This is still significant to add pressure to GC.
Synopsis
- data Array a
- fromPtr :: Int -> Ptr a -> Array a
- fromAddr# :: Int -> Addr# -> Array a
- fromCString# :: Addr# -> Array Word8
- fromListN :: Storable a => Int -> [a] -> Array a
- fromList :: Storable a => [a] -> Array a
- fromStreamN :: (MonadIO m, Storable a) => Int -> SerialT m a -> m (Array a)
- fromStream :: (MonadIO m, Storable a) => SerialT m a -> m (Array a)
- writeN :: forall m a. (MonadIO m, Storable a) => Int -> Fold m a (Array a)
- writeNAligned :: forall m a. (MonadIO m, Storable a) => Int -> Int -> Fold m a (Array a)
- write :: forall m a. (MonadIO m, Storable a) => Fold m a (Array a)
- writeLastN :: (Storable a, MonadIO m) => Int -> Fold m a (Array a)
- toList :: Storable a => Array a -> [a]
- toStream :: (Monad m, IsStream t, Storable a) => Array a -> t m a
- toStreamRev :: (Monad m, IsStream t, Storable a) => Array a -> t m a
- read :: forall m a. (Monad m, Storable a) => Unfold m (Array a) a
- producer :: forall m a. (Monad m, Storable a) => Producer m (Array a) a
- unsafeRead :: forall m a. (Monad m, Storable a) => Unfold m (Array a) a
- readRev :: forall m a. (Monad m, Storable a) => Unfold m (Array a) a
- length :: forall a. Storable a => Array a -> Int
- null :: Storable a => Array a -> Bool
- last :: Storable a => Array a -> Maybe a
- getIndex :: Storable a => Array a -> Int -> Maybe a
- unsafeIndex :: forall a. Storable a => Array a -> Int -> a
- writeIndex :: (MonadIO m, Storable a) => Array a -> Int -> a -> m ()
- unsafeSlice :: forall a. Storable a => Int -> Int -> Array a -> Array a
- streamTransform :: forall m a b. (MonadIO m, Storable a, Storable b) => (SerialT m a -> SerialT m b) -> Array a -> m (Array b)
- cast :: forall a b. Storable b => Array a -> Maybe (Array b)
- unsafeCast :: Array a -> Array b
- unsafeAsPtr :: Array a -> (Ptr b -> IO c) -> IO c
- asBytes :: Array a -> Array Word8
- unsafeAsCString :: Array a -> (CString -> IO b) -> IO b
- streamFold :: (MonadIO m, Storable a) => (SerialT m a -> m b) -> Array a -> m b
- fold :: forall m a b. (MonadIO m, Storable a) => Fold m a b -> Array a -> m b
Documentation
Instances
Storable a => IsList (Array a) Source # | |
(Storable a, Eq a) => Eq (Array a) Source # | |
(Storable a, Ord a) => Ord (Array a) Source # | |
Defined in Streamly.Internal.Data.Array.Foreign.Type | |
(Storable a, Read a, Show a) => Read (Array a) Source # | |
(Show a, Storable a) => Show (Array a) Source # | |
a ~ Char => IsString (Array a) Source # | |
Defined in Streamly.Internal.Data.Array.Foreign.Type fromString :: String -> Array a # | |
Storable a => Semigroup (Array a) Source # | |
Storable a => Monoid (Array a) Source # | |
(Storable a, NFData a) => NFData (Array a) Source # | |
Defined in Streamly.Internal.Data.Array.Foreign.Type | |
type Item (Array a) Source # | |
Defined in Streamly.Internal.Data.Array.Foreign.Type |
Construction
fromPtr :: Int -> Ptr a -> Array a Source #
Create an Array
of the given number of elements of type a
from a read
only pointer Ptr a
. The pointer is not freed when the array is garbage
collected. This API is unsafe for the following reasons:
- The pointer must point to static pinned memory or foreign memory that does not require freeing..
- The pointer must be legally accessible upto the given length.
- To guarantee that the array is immutable, the contents of the address must be guaranteed to not change.
Unsafe
Pre-release
fromAddr# :: Int -> Addr# -> Array a Source #
Create an Array Word8
of the given length from a static, read only
machine address Addr#
. See fromPtr
for safety caveats.
A common use case for this API is to create an array from a static unboxed
string literal. GHC string literals are of type Addr#
, and must contain
characters that can be encoded in a byte i.e. characters or literal bytes in
the range from 0-255.
>>>
import Data.Word (Word8)
>>>
Array.fromAddr# 5 "hello world!"# :: Array Word8
[104,101,108,108,111]
>>>
Array.fromAddr# 3 "\255\NUL\255"# :: Array Word8
[255,0,255]
See also: fromString#
Unsafe
Time complexity: O(1)
Pre-release
fromCString# :: Addr# -> Array Word8 Source #
Generate a byte array from an Addr#
that contains a sequence of NUL
(0
) terminated bytes. The array would not include the NUL byte. The
address must be in static read-only memory and must be legally accessible up
to and including the first NUL byte.
An unboxed string literal (e.g. "hello"#
) is a common example of an
Addr#
in static read only memory. It represents the UTF8 encoded sequence
of bytes terminated by a NUL byte (a CString
) corresponding to the
given unicode string.
>>>
Array.fromCString# "hello world!"#
[104,101,108,108,111,32,119,111,114,108,100,33]
>>>
Array.fromCString# "\255\NUL\255"#
[255]
See also: fromAddr#
Unsafe
Time complexity: O(n) (computes the length of the string)
Pre-release
fromListN :: Storable a => Int -> [a] -> Array a Source #
Create an Array
from the first N elements of a list. The array is
allocated to size N, if the list terminates before N elements then the
array may hold less than N elements.
Since 0.7.0 (Streamly.Memory.Array)
Since: 0.8.0
fromList :: Storable a => [a] -> Array a Source #
Create an Array
from a list. The list must be of finite size.
Since 0.7.0 (Streamly.Memory.Array)
Since: 0.8.0
fromStreamN :: (MonadIO m, Storable a) => Int -> SerialT m a -> m (Array a) Source #
Create an Array
from the first N elements of a stream. The array is
allocated to size N, if the stream terminates before N elements then the
array may hold less than N elements.
Pre-release
fromStream :: (MonadIO m, Storable a) => SerialT m a -> m (Array a) Source #
Create an Array
from a stream. This is useful when we want to create a
single array from a stream of unknown size. writeN
is at least twice
as efficient when the size is already known.
Note that if the input stream is too large memory allocation for the array
may fail. When the stream size is not known, arraysOf
followed by
processing of indvidual arrays in the resulting stream should be preferred.
Pre-release
writeN :: forall m a. (MonadIO m, Storable a) => Int -> Fold m a (Array a) Source #
writeN n
folds a maximum of n
elements from the input stream to an
Array
.
Since 0.7.0 (Streamly.Memory.Array)
Since: 0.8.0
writeNAligned :: forall m a. (MonadIO m, Storable a) => Int -> Int -> Fold m a (Array a) Source #
writeNAligned alignment n
folds a maximum of n
elements from the input
stream to an Array
aligned to the given size.
Pre-release
write :: forall m a. (MonadIO m, Storable a) => Fold m a (Array a) Source #
Fold the whole input to a single array.
Caution! Do not use this on infinite streams.
Since 0.7.0 (Streamly.Memory.Array)
Since: 0.8.0
writeLastN :: (Storable a, MonadIO m) => Int -> Fold m a (Array a) Source #
writeLastN n
folds a maximum of n
elements from the end of the input
stream to an Array
.
Since: 0.8.0
Elimination
toStream :: (Monad m, IsStream t, Storable a) => Array a -> t m a Source #
Convert an Array
into a stream.
Pre-release
toStreamRev :: (Monad m, IsStream t, Storable a) => Array a -> t m a Source #
Convert an Array
into a stream in reverse order.
Pre-release
read :: forall m a. (Monad m, Storable a) => Unfold m (Array a) a Source #
Unfold an array into a stream.
Since 0.7.0 (Streamly.Memory.Array)
Since: 0.8.0
unsafeRead :: forall m a. (Monad m, Storable a) => Unfold m (Array a) a Source #
Unfold an array into a stream, does not check the end of the array, the user is responsible for terminating the stream within the array bounds. For high performance application where the end condition can be determined by a terminating fold.
Written in the hope that it may be faster than "read", however, in the case for which this was written, "read" proves to be faster even though the core generated with unsafeRead looks simpler.
Pre-release
readRev :: forall m a. (Monad m, Storable a) => Unfold m (Array a) a Source #
Unfold an array into a stream in reverse order.
Since: 0.8.0
Random Access
length :: forall a. Storable a => Array a -> Int Source #
O(1) Get the length of the array i.e. the number of elements in the array.
Since 0.7.0 (Streamly.Memory.Array)
Since: 0.8.0
last :: Storable a => Array a -> Maybe a Source #
last arr = getIndex arr (length arr - 1)
Pre-release
getIndex :: Storable a => Array a -> Int -> Maybe a Source #
O(1) Lookup the element at the given index, starting from 0.
Since: 0.8.0
unsafeIndex :: forall a. Storable a => Array a -> Int -> a Source #
Return element at the specified index without checking the bounds.
writeIndex :: (MonadIO m, Storable a) => Array a -> Int -> a -> m () Source #
O(1) Write the given element at the given index in the array. Performs in-place mutation of the array.
Pre-release
O(1) Slice an array in constant time.
Caution: The bounds of the slice are not checked.
Unsafe
Pre-release
Immutable Transformations
streamTransform :: forall m a b. (MonadIO m, Storable a, Storable b) => (SerialT m a -> SerialT m b) -> Array a -> m (Array b) Source #
Transform an array into another array using a stream transformation operation.
Pre-release
Casting
cast :: forall a b. Storable b => Array a -> Maybe (Array b) Source #
Cast an array having elements of type a
into an array having elements of
type b
. The length of the array should be a multiple of the size of the
target element otherwise Nothing
is returned.
Since: 0.8.0
unsafeCast :: Array a -> Array b Source #
Cast an array having elements of type a
into an array having elements of
type b
. The array size must be a multiple of the size of type b
otherwise accessing the last element of the array may result into a crash or
a random value.
Pre-release
unsafeAsCString :: Array a -> (CString -> IO b) -> IO b Source #
Convert an array of any type into a null terminated CString Ptr.
Unsafe
O(n) Time: (creates a copy of the array)
Pre-release