Deprecated. Pin the array and then use unsafeAsPtr.
Modulestreamly-core-0.3.1Haskell2010
Streamly.Internal.Data.MutByteArray
- 11 types
- 5 classes
- 87 values
- Packagestreamly-core-0.3.1
- Exports103
- LanguageHaskell2010
- LicenceBSD-3-Clause
- SourceType.hs
MutByteArray
35 declarationsThe page or block size used by the GHC allocator. Allocator allocates at least a block and then allocates smaller allocations from within a block.
Deprecated. Please use unsafeCloneSlice instead.
unsafeCloneSlice offset len arr clones a slice of the supplied array
starting at the given offset and equal to the given length.
Deprecated. Please use unsafeCloneSliceAs instead.
Unsafe as it does not check whether the start offset and length supplied are valid inside the array.
Deprecated. Please use getMutByteArray# instead.
Return True if the array is allocated in pinned memory.
Allocations larger than largeObjectThreshold are in multiples of block size and are always pinned. The space beyond the end of a large object up to the end of the block is unused.
Return the size of the array in bytes.
Deprecated. Please use newAs instead.
Deprecated. Please use empty instead
Return a copy of the array in pinned memory if unpinned, else return the original array.
Deprecated. Please use unsafePinnedCloneSlice instead.
unsafePinnedCloneSlice offset len arrDeprecated. Please use new' instead.
Deprecated. Please use pinnedNew instead
Deprecated. Please use unsafePutSlice instead.
unsafePutSlice src srcOffset dst dstOffset len copies len bytes from
src at srcOffset to dst at dstOffset.
This is unsafe as it does not check the bounds of src or dst.
reallocSliceAs pinType newLen array offset len reallocates a slice
from array starting at offset and having length len to a new array of
length newLen copying the old data to the new. Note that if the newLen
is smaller than len it will truncate the old data.
Deprecated. Please use length instead.
Return the size of the array in bytes.
Return a copy of the array in unpinned memory if pinned, else return the original array.
Use a MutByteArray as Ptr a. This is useful when we want to pass
an array as a pointer to some operating system call or to a "safe" FFI call.
Unsafe WARNING:
Will lead to memory corruption if the array is not pinned. Use only if the array is known to be pinned already or pin it explicitly.
Ensure that the pointer is accessed within the legal bounds of the array. The size of the MutByteArray must be taken into account.
Pre-release
unsafeCloneSlice offset len arr clones a slice of the supplied array
starting at the given offset and equal to the given length.
Unsafe as it does not check whether the start offset and length supplied are valid inside the array.
Deprecated. Pin the array and then use unsafeAsPtr.
NOTE: this is deprecated because it can lead to accidental problems if the user tries to use it to mutate the array because it does not return the new array after pinning.
unsafePinnedCloneSlice offset len arrunsafePutPtrN srcPtr dst dstOffset len copies len bytes from srcPtr
to dst at dstOffset.
Unsafe:
The caller has to ensure that:
the MutByteArray
dstis valid up todstOffset + len.the
srcPtris alive and pinned during the call.the
srcPtris valid up to lengthlen.
unsafePutSlice src srcOffset dst dstOffset len copies len bytes from
src at srcOffset to dst at dstOffset.
This is unsafe as it does not check the bounds of src or dst.
A lifted mutable byte array type wrapping MutableByteArray# RealWorld.
This is a low level array used to back high level unboxed arrays and
serialized data.
Constructors
Deprecated. Please use MutByteArray instead
Instances2Eq, Show
Eq PinnedStateDefined in streamly-core-0.3.1 · Streamly.Internal.Data.MutByteArray.TypeShow PinnedStateDefined in streamly-core-0.3.1 · Streamly.Internal.Data.MutByteArray.Type
Unbox
19 declarationsA location inside a mutable byte array with the bound of the array. Is it cheaper to just get the bound using the size of the array whenever needed?
Constructors
Instances6PeekRep
PeekRep U1Defined in streamly-core-0.3.1 · Streamly.Internal.Data.UnboxPeekRep V1Defined in streamly-core-0.3.1 · Streamly.Internal.Data.UnboxUnbox a => PeekRep (K1 i a)Defined in streamly-core-0.3.1 · Streamly.Internal.Data.Unbox(MaxArity256 (SumArity (f :+: g)), KnownNat (SumArity (f :+: g)), PeekRepSumDefined in streamly-core-0.3.1 · Streamly.Internal.Data.Unbox0
(f :+: g)) => PeekRep (f :+: g)(PeekRep f, PeekRep g) => PeekRep (f :*: g)Defined in streamly-core-0.3.1 · Streamly.Internal.Data.UnboxPeekRep f => PeekRep (M1 i c f)Defined in streamly-core-0.3.1 · Streamly.Internal.Data.Unbox
Chains peek functions that pass the current position to the next function
Constructors
Peeker (Builder BoundedPtr IO a)
Methods
pokeRep :: f a -> BoundedPtr -> IO BoundedPtr
Instances6PokeRep
PokeRep U1Defined in streamly-core-0.3.1 · Streamly.Internal.Data.UnboxPokeRep V1Defined in streamly-core-0.3.1 · Streamly.Internal.Data.UnboxUnbox a => PokeRep (K1 i a)Defined in streamly-core-0.3.1 · Streamly.Internal.Data.Unbox(MaxArity256 (SumArity (f :+: g)), PokeRepSumDefined in streamly-core-0.3.1 · Streamly.Internal.Data.Unbox0
(f :+: g)) => PokeRep (f :+: g)(PokeRep f, PokeRep g) => PokeRep (f :*: g)Defined in streamly-core-0.3.1 · Streamly.Internal.Data.UnboxPokeRep f => PokeRep (M1 i c f)Defined in streamly-core-0.3.1 · Streamly.Internal.Data.Unbox
Implementation of sizeOf that works on the generic representation of an ADT.
Instances6SizeOfRep
SizeOfRep U1Defined in streamly-core-0.3.1 · Streamly.Internal.Data.UnboxSizeOfRep V1Defined in streamly-core-0.3.1 · Streamly.Internal.Data.UnboxUnbox a => SizeOfRep (K1 i a)Defined in streamly-core-0.3.1 · Streamly.Internal.Data.Unbox(MaxArity256 (SumArity (f :+: g)), SizeOfRepSum f, SizeOfRepSum g) => SizeOfRep (f :+: g)Defined in streamly-core-0.3.1 · Streamly.Internal.Data.Unbox(SizeOfRep f, SizeOfRep g) => SizeOfRep (f :*: g)Defined in streamly-core-0.3.1 · Streamly.Internal.Data.UnboxSizeOfRep f => SizeOfRep (M1 i c f)Defined in streamly-core-0.3.1 · Streamly.Internal.Data.Unbox
The Unbox type class provides operations for serialization (unboxing) and deserialization (boxing) of fixed-length, non-recursive Haskell data types to and from their byte stream representation.
Unbox uses fixed size encoding, therefore, size is independent of the value, it must be determined solely by the type. This restriction makes types with Unbox instances suitable for storing in arrays. Note that sum types may have multiple constructors of different sizes, the size of a sum type is computed as the maximum required by any constructor.
The peekAt operation reads as many bytes from the mutable byte
array as the size of the data type and builds a Haskell data type from
these bytes. pokeAt operation converts a Haskell data type to its
binary representation which consists of size bytes and then stores
these bytes into the mutable byte array. These operations do not check the
bounds of the array, the user of the type class is expected to check the
bounds before peeking or poking.
IMPORTANT: The serialized data's byte ordering remains the same as the host machine's byte order. Therefore, it can not be deserialized from host machines with a different byte ordering.
Instances can be derived via Generics, Template Haskell, or written manually. Note that the data type must be non-recursive. WARNING! Generic and Template Haskell deriving, both hang for recursive data types. Deriving via Generics is more convenient but Template Haskell should be preferred over Generics for the following reasons:
Instances derived via Template Haskell provide better and more reliable performance.
Generic deriving allows only 256 fields or constructor tags whereas template Haskell has no limit.
Here is an example, for deriving an instance of this type class using generics:
import GHC.Generics (Generic):{data Object = Object { _int0 :: Int , _int1 :: Int } deriving Generic:}
import Streamly.Data.MutByteArray (Unbox(..))instance Unbox Object
To derive the instance via Template Haskell:
import Streamly.Data.MutByteArray (deriveUnbox)
$(deriveUnbox [d|instance Unbox Object|])
See Streamly.Data.MutByteArray.deriveUnbox for more information on deriving
using Template Haskell.
If you want to write the instance manually:
:{instance Unbox Object where sizeOf _ = 16 peekAt i arr = do -- Check the array bounds x0 <- peekAt i arr x1 <- peekAt (i + 8) arr return $ Object x0 x1 pokeAt i arr (Object x0 x1) = do -- Check the array bounds pokeAt i arr x0 pokeAt (i + 8) arr x1:}
Methods
sizeOf :: Proxy a -> IntGet the size. Size cannot be zero, should be at least 1 byte.
peekAt :: Int -> MutByteArray -> IO apeekAt byte-offset arrayreads an element of typeafrom the given byte offset in the array.IMPORTANT: The implementation of this interface may not check the bounds of the array, the caller must not assume that.
peekByteIndex :: Int -> MutByteArray -> IO apokeAt :: Int -> MutByteArray -> a -> IO ()pokeAt byte-offset arraywrites an element of typeato the given byte offset in the array.IMPORTANT: The implementation of this interface may not check the bounds of the array, the caller must not assume that.
pokeByteIndex :: Int -> MutByteArray -> a -> IO ()
Instances32Unbox, …
Unbox FingerprintDefined in streamly-core-0.3.1 · Streamly.Internal.Data.UnboxUnbox CCharDefined in streamly-core-0.3.1 · Streamly.Internal.Data.UnboxUnbox CWcharDefined in streamly-core-0.3.1 · Streamly.Internal.Data.UnboxUnbox IntPtrDefined in streamly-core-0.3.1 · Streamly.Internal.Data.UnboxUnbox WordPtrDefined in streamly-core-0.3.1 · Streamly.Internal.Data.UnboxUnbox IoSubSystemDefined in streamly-core-0.3.1 · Streamly.Internal.Data.UnboxUnbox Int16Defined in streamly-core-0.3.1 · Streamly.Internal.Data.UnboxUnbox Int32Defined in streamly-core-0.3.1 · Streamly.Internal.Data.UnboxUnbox Int64Defined in streamly-core-0.3.1 · Streamly.Internal.Data.UnboxUnbox Int8Defined in streamly-core-0.3.1 · Streamly.Internal.Data.UnboxUnbox BoolDefined in streamly-core-0.3.1 · Streamly.Internal.Data.UnboxUnbox CharDefined in streamly-core-0.3.1 · Streamly.Internal.Data.UnboxUnbox DoubleDefined in streamly-core-0.3.1 · Streamly.Internal.Data.UnboxUnbox FloatDefined in streamly-core-0.3.1 · Streamly.Internal.Data.UnboxUnbox IntDefined in streamly-core-0.3.1 · Streamly.Internal.Data.UnboxUnbox WordDefined in streamly-core-0.3.1 · Streamly.Internal.Data.UnboxUnbox Word16Defined in streamly-core-0.3.1 · Streamly.Internal.Data.UnboxUnbox Word32Defined in streamly-core-0.3.1 · Streamly.Internal.Data.UnboxUnbox Word64Defined in streamly-core-0.3.1 · Streamly.Internal.Data.UnboxUnbox Word8Defined in streamly-core-0.3.1 · Streamly.Internal.Data.UnboxUnbox MicroSecond64Defined in streamly-core-0.3.1 · Streamly.Internal.Data.Time.UnitsUnbox MilliSecond64Defined in streamly-core-0.3.1 · Streamly.Internal.Data.Time.UnitsUnbox NanoSecond64Defined in streamly-core-0.3.1 · Streamly.Internal.Data.Time.UnitsUnbox ()Defined in streamly-core-0.3.1 · Streamly.Internal.Data.UnboxUnbox (FunPtr a)Defined in streamly-core-0.3.1 · Streamly.Internal.Data.UnboxUnbox (Ptr a)Defined in streamly-core-0.3.1 · Streamly.Internal.Data.UnboxUnbox (StablePtr a)Defined in streamly-core-0.3.1 · Streamly.Internal.Data.UnboxUnbox a => Unbox (Complex a)Defined in streamly-core-0.3.1 · Streamly.Internal.Data.UnboxUnbox a => Unbox (Identity a)Defined in streamly-core-0.3.1 · Streamly.Internal.Data.UnboxUnbox a => Unbox (Down a)Defined in streamly-core-0.3.1 · Streamly.Internal.Data.UnboxUnbox a => Unbox (Ratio a)Defined in streamly-core-0.3.1 · Streamly.Internal.Data.UnboxUnbox a => Unbox (Const a b)Defined in streamly-core-0.3.1 · Streamly.Internal.Data.Unbox
Given an Unbox instance declaration splice without the methods (e.g.
[d|instance Unbox a => Unbox (Maybe a)|]), generate an instance
declaration including all the type class method implementations.
Usage:
$(deriveUnbox [d|instance Unbox a => Unbox (Maybe a)|])
Reify the given data or newtype declaration, and yields its DataType representation.
Simplified info about a DataD. Omits deriving, strictness,
kind info, and whether it's data or newtype.
Serialize
1 declarationThe Serialize type class provides operations for serialization and deserialization of general Haskell data types to and from their byte stream representation.
Unlike Unbox, Serialize uses variable length encoding, therefore, it can serialize recursive and variable length data types like lists, or variable length sum types where the length of the value may vary depending on a particular constructor. For variable length data types the length is encoded along with the data.
The deserializeAt operation reads bytes from the mutable byte array and
builds a Haskell data type from these bytes, the number of bytes it reads
depends on the type and the encoded value it is reading. serializeAt
operation converts a Haskell data type to its binary representation which
must consist of as many bytes as added by the addSizeTo operation for that
value and then stores these bytes into the mutable byte array. The
programmer is expected to use the addSizeTo operation and allocate an
array of sufficient length before calling serializeAt.
IMPORTANT: The serialized data's byte ordering remains the same as the host machine's byte order. Therefore, it can not be deserialized from host machines with a different byte ordering.
Instances can be derived via Template Haskell, or written manually.
Here is an example, for deriving an instance of this type class using template Haskell:
:{data Object = Object { _obj1 :: [Int] , _obj2 :: Int }:}
import Streamly.Data.MutByteArray (deriveSerialize)
$(deriveSerialize [d|instance Serialize Object|])
See Streamly.Data.MutByteArray.deriveSerialize and
Streamly.Data.MutByteArray.deriveSerializeWith for more information on
deriving using Template Haskell.
Here is an example of a manual instance.
import Streamly.Data.MutByteArray (Serialize(..)):{instance Serialize Object where addSizeTo acc obj = addSizeTo (addSizeTo acc (_obj1 obj)) (_obj2 obj) deserializeAt i arr len = do -- Check the array bounds before reading (i1, x0) <- deserializeAt i arr len (i2, x1) <- deserializeAt i1 arr len pure (i2, Object x0 x1) serializeAt i arr (Object x0 x1) = do i1 <- serializeAt i arr x0 i2 <- serializeAt i1 arr x1 pure i2:}
Methods
addSizeTo :: Int -> a -> IntaddSizeTo accum valuereturnsaccumincremented by the size of the serialized representation ofvaluein bytes. Size cannot be zero. It should be at least 1 byte.deserializeAt :: Int -> MutByteArray -> Int -> IO (Int, a)deserializeAt byte-offset array arrayLendeserializes a value from the given byte-offset in the array. Returns a tuple consisting of the next byte-offset and the deserialized value.The arrayLen passed is the entire length of the input buffer. It is to be used to check if we would overflow the input buffer when deserializing.
Throws an exception if the operation would exceed the supplied arrayLen.
serializeAt :: Int -> MutByteArray -> a -> IO IntserializeAt byte-offset array valuewrites the serialized representation of thevaluein the array at the given byte-offset. Returns the next byte-offset.This is an unsafe operation, the programmer must ensure that the array has enough space available to serialize the value as determined by the
addSizeTooperation.
Instances28Serialize, …
Serialize IntegerDefined in streamly-core-0.3.1 · Streamly.Internal.Data.MutByteArray · orphanSerialize FingerprintDefined in streamly-core-0.3.1 · Streamly.Internal.Data.Serialize.TypeSerialize Int16Defined in streamly-core-0.3.1 · Streamly.Internal.Data.Serialize.TypeSerialize Int32Defined in streamly-core-0.3.1 · Streamly.Internal.Data.Serialize.TypeSerialize Int64Defined in streamly-core-0.3.1 · Streamly.Internal.Data.Serialize.TypeSerialize Int8Defined in streamly-core-0.3.1 · Streamly.Internal.Data.Serialize.TypeSerialize BoolDefined in streamly-core-0.3.1 · Streamly.Internal.Data.Serialize.TypeSerialize CharDefined in streamly-core-0.3.1 · Streamly.Internal.Data.Serialize.TypeSerialize DoubleDefined in streamly-core-0.3.1 · Streamly.Internal.Data.Serialize.TypeSerialize FloatDefined in streamly-core-0.3.1 · Streamly.Internal.Data.Serialize.TypeSerialize IntDefined in streamly-core-0.3.1 · Streamly.Internal.Data.Serialize.TypeSerialize WordDefined in streamly-core-0.3.1 · Streamly.Internal.Data.Serialize.TypeSerialize Word16Defined in streamly-core-0.3.1 · Streamly.Internal.Data.Serialize.TypeSerialize Word32Defined in streamly-core-0.3.1 · Streamly.Internal.Data.Serialize.TypeSerialize Word64Defined in streamly-core-0.3.1 · Streamly.Internal.Data.Serialize.TypeSerialize Word8Defined in streamly-core-0.3.1 · Streamly.Internal.Data.Serialize.TypeSerialize LiftedIntegerDefined in streamly-core-0.3.1 · Streamly.Internal.Data.MutByteArraySerialize ()Defined in streamly-core-0.3.1 · Streamly.Internal.Data.Serialize.TypeSerialize (FunPtr a)Defined in streamly-core-0.3.1 · Streamly.Internal.Data.Serialize.TypeSerialize (Ptr a)Defined in streamly-core-0.3.1 · Streamly.Internal.Data.Serialize.TypeSerialize (StablePtr a)Defined in streamly-core-0.3.1 · Streamly.Internal.Data.Serialize.TypeSerialize (Array a)Defined in streamly-core-0.3.1 · Streamly.Internal.Data.Serialize.TypeSerialize a => Serialize (Maybe a)Defined in streamly-core-0.3.1 · Streamly.Internal.Data.MutByteArray · orphanSerialize a => Serialize (CompactList a)Defined in streamly-core-0.3.1 · Streamly.Internal.Data.Serialize.TH.RecHeaderSerialize a => Serialize [a]Defined in streamly-core-0.3.1 · Streamly.Internal.Data.Serialize.TypeSerialize (Proxy a)Defined in streamly-core-0.3.1 · Streamly.Internal.Data.MutByteArray · orphan(Serialize a, Serialize b) => Serialize (Either a b)Defined in streamly-core-0.3.1 · Streamly.Internal.Data.MutByteArray · orphan(Serialize a, Serialize b) => Serialize (a, b)Defined in streamly-core-0.3.1 · Streamly.Internal.Data.Serialize.Type
Serialize TH
48 declarationsGiven an Serialize instance declaration splice without the methods (e.g.
[d|instance Serialize a => Serialize (Maybe a)|]), generate an instance
declaration including all the type class method implementations.
deriveSerialize = deriveSerializeWith idUsage:
$(deriveSerialize
[d|instance Serialize a => Serialize (Maybe a)|])
deriveSerializeWith config-modifier instance-dec generates a template
Haskell splice consisting of a declaration of a Serialize instance.
instance-dec is a template Haskell declaration splice consisting of a
standard Haskell instance declaration without the type class methods (e.g.
[d|instance Serialize a => Serialize (Maybe a)|]).
The type class methods for the given instance are generated according to the
supplied config-modifier parameter. See SerializeConfig for default
configuration settings.
Usage:
$(deriveSerializeWith
( inlineSerializeAt (Just NoInline)
. inlineDeserializeAt (Just NoInline)
)
[d|instance Serialize a => Serialize (Maybe a)|])
Experimental
In sum types, use Latin-1 encoded original constructor names rather than binary values to identify constructors. This option is not applicable to product types.
This option enables the following behavior:
Reordering: Order of the fields can be changed without affecting serialization.
Addition: If a field is added in the new version, the old version of the data type can still be deserialized by the new version. The new value would never occur in the old one.
Deletion: If a field is deleted in the new version, deserialization of the old version will result in an error. TBD: We can possibly designate a catch-all case to handle this scenario.
Note that if you change a type, change the semantics of a type, or delete a field and add a new field with the same name, deserialization of old data may result in silent unexpected behavior.
This option has to be the same on both encoding and decoding side.
The default is False.
Experimental
In explicit record types, use Latin-1 encoded record field names rather than binary values to identify the record fields. Note that this option is not applicable to sum types. Also, it does not work on a product type which is not a record, because there are no field names to begin with.
This option enables the following behavior:
Reordering: Order of the fields can be changed without affecting serialization.
Addition: If a Maybe type field is added in the new version, the old version of the data type can still be deserialized by the new version, the field value in the older version is assumed to be Nothing. If any other type of field is added, deserialization of the older version results in an error but only when that field is actually accessed in the deserialized record.
Deletion: If a field is deleted in the new version and it is encountered in a previously serialized version then the field is discarded.
This option has to be the same on both encoding and decoding side.
There is a constant performance overhead proportional to the total length of the record field names and the number of record fields.
The default is False.
How should we inline the deserialize function? See guidelines in
inlineSerializeAt.
How should we inline the serialize function? The default 'Just Inline'.
However, aggressive inlining can bloat the code and increase in compilation
times when there are big functions and too many nesting levels so you can
change it accordingly. A Nothing value leaves the decision to the
compiler.
Configuration to control how the Serialize instance is generated. The configuration is opaque and is modified by composing config modifier functions, for example:
import Language.Haskell.TH (Inline(..))configOpts = (inlineDeserializeAt (Just NoInline)) . (inlineSerializeAt (Just Inlinable))
The default configuration settings are:
inlineAddSizeTo Nothing
inlineSerializeAt (Just Inline)
inlineDeserializeAt (Just Inline)
The following experimental options are also available:
encodeConstrNames False
encodeRecordFields False
Constructors
Instances1Eq
Eq SimpleDataConDefined in streamly-core-0.3.1 · Streamly.Internal.Data.Serialize.TH.Bottom
Constructors
Instances1Eq
Eq TypeOfTypeDefined in streamly-core-0.3.1 · Streamly.Internal.Data.Serialize.TH.Bottom