Safe Haskell | None |
---|---|
Language | GHC2021 |
Bytezap.Struct.TypeLits.Bytes
Contents
Description
Efficient type-level bytestring serialization via chunking.
I did a quick Core check and found that GHC seems to successfully generate
minimal code for this e.g. for an 8-byte magic, GHC will do one
writeWord64OffAddr#
of a constant. Great!
The only way I can think of to make this faster is to somehow obtain an Addr#
with a known length. With that, we could memcpy
. But that would be slower for
small magics, and maybe others. And I doubt we can conjure up an Addr#
at
compile time. So I'm fairly confident that this is the best you're gonna get.
Synopsis
- class ReifyBytesW64 (bs :: [Natural]) where
- reifyBytesW64 :: Poke s
- class ReifyBytesW32 (bs :: [Natural]) where
- reifyBytesW32 :: Poke s
- class ReifyBytesW16 (bs :: [Natural]) where
- reifyBytesW16 :: Poke s
- class ReifyBytesW8 (bs :: [Natural]) where
- reifyBytesW8 :: Poke s
Chunking design
[
s have a convenient syntax, and we can use them as a type-level
bytestring by asserting that each Natural
]Natural
is <=255 when reifying. This module
provides type classes which give you a serializer for a given [
.Natural
]
We maximize efficiency by grouping bytes into machine words. We have to be pretty verbose to achieve this. Each type class attempts to group bytes into its machine word type, and if it can't (i.e. not enough bytes remain), it hands off to the next type class which handles the next smaller machine word.
TODO rewrite :)
class ReifyBytesW64 (bs :: [Natural]) where Source #
Serialize a type-level bytestring, largest grouping Word64
.
Methods
reifyBytesW64 :: Poke s Source #
Instances
ReifyBytesW32 bs => ReifyBytesW64 bs Source # | Try to group |
Defined in Bytezap.Struct.TypeLits.Bytes Methods reifyBytesW64 :: Poke s Source # | |
(ReifyW8 b0, ReifyW8 b1, ReifyW8 b2, ReifyW8 b3, ReifyW8 b4, ReifyW8 b5, ReifyW8 b6, ReifyW8 b7, ReifyBytesW64 bs) => ReifyBytesW64 (b0 ': (b1 ': (b2 ': (b3 ': (b4 ': (b5 ': (b6 ': (b7 ': bs)))))))) Source # | Enough bytes to make a |
Defined in Bytezap.Struct.TypeLits.Bytes Methods reifyBytesW64 :: Poke s Source # |
class ReifyBytesW32 (bs :: [Natural]) where Source #
Serialize a type-level bytestring, largest grouping Word32
.
Methods
reifyBytesW32 :: Poke s Source #
Instances
ReifyBytesW16 bs => ReifyBytesW32 bs Source # | Try to group |
Defined in Bytezap.Struct.TypeLits.Bytes Methods reifyBytesW32 :: Poke s Source # | |
(ReifyW8 b0, ReifyW8 b1, ReifyW8 b2, ReifyW8 b3, ReifyBytesW32 bs) => ReifyBytesW32 (b0 ': (b1 ': (b2 ': (b3 ': bs)))) Source # | Enough bytes to make a |
Defined in Bytezap.Struct.TypeLits.Bytes Methods reifyBytesW32 :: Poke s Source # |
class ReifyBytesW16 (bs :: [Natural]) where Source #
Serialize a type-level bytestring, largest grouping Word16
.
Methods
reifyBytesW16 :: Poke s Source #
Instances
ReifyBytesW8 bs => ReifyBytesW16 bs Source # | Reify byte-by-byte next. |
Defined in Bytezap.Struct.TypeLits.Bytes Methods reifyBytesW16 :: Poke s Source # | |
(ReifyW8 b0, ReifyW8 b1, ReifyBytesW16 bs) => ReifyBytesW16 (b0 ': (b1 ': bs)) Source # | Enough bytes to make a |
Defined in Bytezap.Struct.TypeLits.Bytes Methods reifyBytesW16 :: Poke s Source # |
class ReifyBytesW8 (bs :: [Natural]) where Source #
Serialize a type-level bytestring, byte-by-byte.
Methods
reifyBytesW8 :: Poke s Source #
Instances
ReifyBytesW8 ('[] :: [Natural]) Source # | End of the line. |
Defined in Bytezap.Struct.TypeLits.Bytes Methods reifyBytesW8 :: Poke s Source # | |
(ReifyW8 b0, ReifyBytesW8 bs) => ReifyBytesW8 (b0 ': bs) Source # | Reify the next byte. |
Defined in Bytezap.Struct.TypeLits.Bytes Methods reifyBytesW8 :: Poke s Source # |