Safe Haskell | Safe-Inferred |
---|---|
Language | GHC2021 |
Efficient type-level bytestring serialization.
[
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.
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 (ns :: [Natural]) where
- reifyBytesW64 :: Poke s
- class ReifyBytesW32 (ns :: [Natural]) where
- reifyBytesW32 :: Poke s
- class ReifyBytesW16 (ns :: [Natural]) where
- reifyBytesW16 :: Poke s
- class ReifyBytesW8 (ns :: [Natural]) where
- reifyBytesW8 :: Poke s
Documentation
class ReifyBytesW64 (ns :: [Natural]) where Source #
Serialize a type-level bytestring, largest grouping Word64
.
reifyBytesW64 :: Poke s Source #
Instances
ReifyBytesW32 ns => ReifyBytesW64 ns Source # | Try to group |
Defined in Bytezap.Struct.TypeLits.Bytes reifyBytesW64 :: Poke s Source # | |
(ReifyW8 n1, ReifyW8 n2, ReifyW8 n3, ReifyW8 n4, ReifyW8 n5, ReifyW8 n6, ReifyW8 n7, ReifyW8 n8, ReifyBytesW64 ns) => ReifyBytesW64 (n1 ': (n2 ': (n3 ': (n4 ': (n5 ': (n6 ': (n7 ': (n8 ': ns)))))))) Source # | Enough bytes to make a |
Defined in Bytezap.Struct.TypeLits.Bytes reifyBytesW64 :: Poke s Source # |
class ReifyBytesW32 (ns :: [Natural]) where Source #
Serialize a type-level bytestring, largest grouping Word32
.
reifyBytesW32 :: Poke s Source #
Instances
ReifyBytesW16 ns => ReifyBytesW32 ns Source # | Try to group |
Defined in Bytezap.Struct.TypeLits.Bytes reifyBytesW32 :: Poke s Source # | |
(ReifyW8 n1, ReifyW8 n2, ReifyW8 n3, ReifyW8 n4, ReifyBytesW32 ns) => ReifyBytesW32 (n1 ': (n2 ': (n3 ': (n4 ': ns)))) Source # | Enough bytes to make a |
Defined in Bytezap.Struct.TypeLits.Bytes reifyBytesW32 :: Poke s Source # |
class ReifyBytesW16 (ns :: [Natural]) where Source #
Serialize a type-level bytestring, largest grouping Word16
.
reifyBytesW16 :: Poke s Source #
Instances
ReifyBytesW8 ns => ReifyBytesW16 ns Source # | Reify byte-by-byte next. |
Defined in Bytezap.Struct.TypeLits.Bytes reifyBytesW16 :: Poke s Source # | |
(ReifyW8 n1, ReifyW8 n2, ReifyBytesW16 ns) => ReifyBytesW16 (n1 ': (n2 ': ns)) Source # | Enough bytes to make a |
Defined in Bytezap.Struct.TypeLits.Bytes reifyBytesW16 :: Poke s Source # |
class ReifyBytesW8 (ns :: [Natural]) where Source #
Serialize a type-level bytestring, byte-by-byte.
reifyBytesW8 :: Poke s Source #
Instances
ReifyBytesW8 ('[] :: [Natural]) Source # | End of the line. |
Defined in Bytezap.Struct.TypeLits.Bytes reifyBytesW8 :: Poke s Source # | |
(ReifyW8 n1, ReifyBytesW8 ns) => ReifyBytesW8 (n1 ': ns) Source # | Reify the next byte. |
Defined in Bytezap.Struct.TypeLits.Bytes reifyBytesW8 :: Poke s Source # |