HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Bounded Primitives

2 declarations
newtypenewtype Builder
#

An unmaterialized sequence of bytes that may be pasted into a mutable byte array.

Instances4IsString, Semigroup, Monoid, ToBuilder
  • IsString BuilderDefined in bytebuild-0.3.16.2 · Data.Bytes.Builder.Unsafe
  • Semigroup BuilderDefined in bytebuild-0.3.16.2 · Data.Bytes.Builder.Unsafe
  • Monoid BuilderDefined in bytebuild-0.3.16.2 · Data.Bytes.Builder.Unsafe
  • ToBuilder BuilderDefined in bytebuild-0.3.16.2 · Data.Bytes.Builder.Class

    Identity

valuefromBounded :: Nat n -> Builder n -> Builder
#

Convert a bounded builder to an unbounded one. If the size is a constant, use Arithmetic.Nat.constant as the first argument to let GHC conjure up this value for you.

Evaluation

6 declarations
valuerun
  1. :: Int

    Size of initial chunk (use 4080 if uncertain)

  2. -> Builder

    Builder

  3. -> Chunks
#

Run a builder.

valuerunOnto
  1. :: Int

    Size of initial chunk (use 4080 if uncertain)

  2. -> Builder

    Builder

  3. -> Chunks

    Suffix

  4. -> Chunks
#

Run a builder. The resulting chunks are consed onto the beginning of an existing sequence of chunks.

valueputMany
  1. :: Foldable f
  2. => Int

    Size of shared chunk (use 8176 if uncertain)

  3. -> (a -> Builder)

    Value builder

  4. -> f a

    Collection of values

  5. -> (MutableBytes RealWorld -> IO b)

    Consume chunks.

  6. -> IO ()
#

Run a builder against lots of elements. This fills the same underlying buffer over and over again. Do not let the argument to the callback escape from the callback (i.e. do not write it to an IORef). Also, do not unsafeFreezeByteArray any of the mutable byte arrays in the callback. The intent is that the callback will write the buffer out.

valueputManyConsLength
  1. :: (Foldable f, MonadIO m)
  2. => Nat n

    Number of bytes used by the serialization of the length

  3. -> (Int -> Builder n)

    Length serialization function

  4. -> Int

    Size of shared chunk (use 8176 if uncertain)

  5. -> (a -> Builder)

    Value builder

  6. -> f a

    Collection of values

  7. -> (MutableBytes RealWorld -> m b)

    Consume chunks.

  8. -> m ()
#

Variant of putMany that prefixes each pushed array of chunks with the number of bytes that the chunks in each batch required. (This excludes the bytes required to encode the length itself.) This is useful for chunked HTTP encoding.

Materialized Byte Sequences

16 declarations
valuebytes :: Bytes -> Builder
#

Create a builder from a sliced byte sequence. The variants copy and insert provide more control over whether or not the byte sequence is copied or aliased. This function is preferred when the user does not know the size of the byte sequence.

valuecopy :: Bytes -> Builder
#

Create a builder from a byte sequence. This always results in a call to memcpy. This is beneficial when the byte sequence is known to be small (less than 256 bytes).

valuecopy2 :: Bytes -> Bytes -> Builder
#

Create a builder from two byte sequences. This always results in two calls to memcpy. This is beneficial when the byte sequences are known to be small (less than 256 bytes).

valueinsert :: Bytes -> Builder
#

Create a builder from a byte sequence. This never calls memcpy. Instead, it pushes a chunk that references the argument byte sequence. This wastes the remaining space in the active chunk, so it may adversely affect performance if used carelessly. See flush for a way to mitigate this problem. This functions is most beneficial when the byte sequence is known to be large (more than 8192 bytes).

Create a builder from text. The text will be UTF-8 encoded, and JSON special characters will be escaped. Additionally, the result is surrounded by double quotes. For example:

  • foo ==> "foo" (no escape sequences)

  • \_"_/ ==> "\\_\"_/" (escapes backslashes and quotes)

  • hello<ESC>world ==> "hello\u001Bworld" (where <ESC> is code point 0x1B)

valuecstring :: CString -> Builder
#

Create a builder from a NUL-terminated CString. This ignores any textual encoding, copying bytes until NUL is reached.

Create a builder from a C string with explicit length. The builder must be executed before the C string is freed.

Byte Sequence Encodings

2 declarations

Encode seven bytes into eight so that the encoded form is eight-bit clean. Specifically segment the input bytes inot 7-bit groups (lowest-to-highest index byte, most-to-least significant bit within a byte), pads the last group with trailing zeros, and forms octects by prepending a zero to each group.

The name was chosen because this pads the input bits with zeros on the right, and also because this was likely the originally-indended behavior of the SMILE standard (see sevenEightSmile). Right padding the input bits to a multiple of seven, as in this variant, is consistent with base64 encodings (which encodes 3 bytes in 4) and base85 (which encodes 4 bytes in 5).

Encode seven bytes into eight so that the encoded form is eight-bit clean. Specifically segment the input bytes inot 7-bit groups (lowest-to-highest index byte, most-to-least significant bit within a byte), then pad each group with zeros on the left until each group is an octet.

The name was chosen because this is the implementation that is used (probably unintentionally) in the reference SMILE implementation, and so is expected tp be accepted by existing SMILE consumers.

Encode Integral Types

0 declarations

Human-Readable

valueword64Dec :: Word64 -> Builder
#

Encodes an unsigned 64-bit integer as decimal. This encoding never starts with a zero unless the argument was zero.

valueword32Dec :: Word32 -> Builder
#

Encodes an unsigned 16-bit integer as decimal. This encoding never starts with a zero unless the argument was zero.

valueword16Dec :: Word16 -> Builder
#

Encodes an unsigned 16-bit integer as decimal. This encoding never starts with a zero unless the argument was zero.

valueword8Dec :: Word8 -> Builder
#

Encodes an unsigned 8-bit integer as decimal. This encoding never starts with a zero unless the argument was zero.

valuewordDec :: Word -> Builder
#

Encodes an unsigned machine-sized integer as decimal. This encoding never starts with a zero unless the argument was zero.

valuenaturalDec :: Natural -> Builder
#

Encodes an unsigned arbitrary-precision integer as decimal. This encoding never starts with a zero unless the argument was zero.

valueint64Dec :: Int64 -> Builder
#

Encodes a signed 64-bit integer as decimal. This encoding never starts with a zero unless the argument was zero. Negative numbers are preceded by a minus sign. Positive numbers are not preceded by anything.

valueint32Dec :: Int32 -> Builder
#

Encodes a signed 32-bit integer as decimal. This encoding never starts with a zero unless the argument was zero. Negative numbers are preceded by a minus sign. Positive numbers are not preceded by anything.

valueint16Dec :: Int16 -> Builder
#

Encodes a signed 16-bit integer as decimal. This encoding never starts with a zero unless the argument was zero. Negative numbers are preceded by a minus sign. Positive numbers are not preceded by anything.

valueint8Dec :: Int8 -> Builder
#

Encodes a signed 8-bit integer as decimal. This encoding never starts with a zero unless the argument was zero. Negative numbers are preceded by a minus sign. Positive numbers are not preceded by anything.

valueintDec :: Int -> Builder
#

Encodes a signed machine-sized integer as decimal. This encoding never starts with a zero unless the argument was zero. Negative numbers are preceded by a minus sign. Positive numbers are not preceded by anything.

valueintegerDec :: Integer -> Builder
#

Encode a signed arbitrary-precision integer as decimal. This encoding never starts with a zero unless the argument was zero. Negative numbers are preceded by a minus sign. Positive numbers are not preceded by anything.

Unsigned Words

0 declarations

64-bit

Encode a 64-bit unsigned integer as hexadecimal, zero-padding the encoding to 16 digits. This uses uppercase for the alphabetical digits. For example, this encodes the number 1022 as 00000000000003FE.

32-bit

Encode a 32-bit unsigned integer as hexadecimal, zero-padding the encoding to 8 digits. This uses uppercase for the alphabetical digits. For example, this encodes the number 1022 as 000003FE.

16-bit

Encode a 16-bit unsigned integer as hexadecimal, zero-padding the encoding to 4 digits. This uses uppercase for the alphabetical digits. For example, this encodes the number 1022 as 03FE.

Encode a 16-bit unsigned integer as hexadecimal, zero-padding the encoding to 4 digits. This uses lowercase for the alphabetical digits. For example, this encodes the number 1022 as 03fe.

Encode a 16-bit unsigned integer as hexadecimal without leading zeroes. This uses lowercase for the alphabetical digits. For example, this encodes the number 1022 as 3fe.

Encode a 16-bit unsigned integer as hexadecimal without leading zeroes. This uses uppercase for the alphabetical digits. For example, this encodes the number 1022 as 3FE.

8-bit

Encode a 8-bit unsigned integer as hexadecimal, zero-padding the encoding to 2 digits. This uses uppercase for the alphabetical digits. For example, this encodes the number 11 as 0B.

valueword8LowerHex :: Word8 -> Builder
#

Encode a 16-bit unsigned integer as hexadecimal without leading zeroes. This uses lowercase for the alphabetical digits. For example, this encodes the number 1022 as 3FE.

valueascii :: Char -> Builder
#

Encode an ASCII char. Precondition: Input must be an ASCII character. This is not checked.

valueascii2 :: Char -> Char -> Builder
#

Encode two ASCII characters. Precondition: Must be an ASCII characters. This is not checked.

valueascii3 :: Char -> Char -> Char -> Builder
#

Encode three ASCII characters. Precondition: Must be an ASCII characters. This is not checked.

valueascii4 :: Char -> Char -> Char -> Char -> Builder
#

Encode four ASCII characters. Precondition: Must be an ASCII characters. This is not checked.

valuechar :: Char -> Builder
#

Encode a UTF-8 char. This only uses as much space as is required.

Machine-Readable

One

Big Endian

valueword256BE :: Word256 -> Builder
#

Requires exactly 32 bytes. Dump the octets of a 256-bit word in a big-endian fashion.

valueword128BE :: Word128 -> Builder
#

Requires exactly 16 bytes. Dump the octets of a 128-bit word in a big-endian fashion.

valueword64BE :: Word64 -> Builder
#

Requires exactly 8 bytes. Dump the octets of a 64-bit word in a big-endian fashion.

valueword32BE :: Word32 -> Builder
#

Requires exactly 4 bytes. Dump the octets of a 32-bit word in a big-endian fashion.

valueword16BE :: Word16 -> Builder
#

Requires exactly 2 bytes. Dump the octets of a 16-bit word in a big-endian fashion.

valueint64BE :: Int64 -> Builder
#

Requires exactly 8 bytes. Dump the octets of a 64-bit signed integer in a big-endian fashion.

valueint32BE :: Int32 -> Builder
#

Requires exactly 4 bytes. Dump the octets of a 32-bit signed integer in a big-endian fashion.

valueint16BE :: Int16 -> Builder
#

Requires exactly 2 bytes. Dump the octets of a 16-bit signed integer in a big-endian fashion.

Little Endian

valueword256LE :: Word256 -> Builder
#

Requires exactly 32 bytes. Dump the octets of a 256-bit word in a little-endian fashion.

valueword128LE :: Word128 -> Builder
#

Requires exactly 16 bytes. Dump the octets of a 128-bit word in a little-endian fashion.

valueword64LE :: Word64 -> Builder
#

Requires exactly 8 bytes. Dump the octets of a 64-bit word in a little-endian fashion.

valueword32LE :: Word32 -> Builder
#

Requires exactly 4 bytes. Dump the octets of a 32-bit word in a little-endian fashion.

valueword16LE :: Word16 -> Builder
#

Requires exactly 2 bytes. Dump the octets of a 16-bit word in a little-endian fashion.

valueint64LE :: Int64 -> Builder
#

Requires exactly 8 bytes. Dump the octets of a 64-bit signed integer in a little-endian fashion.

valueint32LE :: Int32 -> Builder
#

Requires exactly 4 bytes. Dump the octets of a 32-bit signed integer in a little-endian fashion.

valueint16LE :: Int16 -> Builder
#

Requires exactly 2 bytes. Dump the octets of a 16-bit signed integer in a little-endian fashion.

LEB128

valueintLEB128 :: Int -> Builder
#

Encode a signed machine-sized integer with LEB-128. This uses zig-zag encoding.

VLQ

Many

Big Endian

Little Endian

Prefixing with Length

valueconsLength
  1. :: Nat n

    Number of bytes used by the serialization of the length

  2. -> (Int -> Builder n)

    Length serialization function

  3. -> Builder

    Builder whose length is measured

  4. -> Builder
#

Prefix a builder with the number of bytes that it requires.

Prefix a builder with its size in bytes. This size is presented as a big-endian 32-bit word. The need to prefix a builder with its length shows up a numbers of wire protocols including those of PostgreSQL and Apache Kafka. Note the equivalence:

forall (n :: Int) (x :: Builder).
  let sz = sizeofByteArray (run n (consLength32BE x))
  consLength32BE x === word32BE (fromIntegral sz) <> x

However, using consLength32BE is much more efficient here since it only materializes the ByteArray once.

Encode Floating-Point Types

0 declarations

Human-Readable

valuedoubleDec :: Double -> Builder
#

Encode a double-floating-point number, using decimal notation or scientific notation depending on the magnitude. This has undefined behavior when representing +inf, -inf, and NaN. It will not crash, but the generated numbers will be nonsense.

Replication

1 declaration
valuereplicate
  1. :: Int

    Number of times to replicate the byte

  2. -> Word8

    Byte to replicate

  3. -> Builder
#

Replicate a byte the given number of times.

Control

1 declaration
valueflush :: Int -> Builder
#

Push the buffer currently being filled onto the chunk list, allocating a new active buffer of the requested size. This is helpful when a small builder is sandwhiched between two large zero-copy builders:

insert bigA <> flush 1 <> word8 0x42 <> insert bigB

Without flush 1, word8 0x42 would see the zero-byte active buffer that insert returned, decide that it needed more space, and allocate a 4080-byte buffer to which only a single byte would be written.

Rebuild

1 declaration
valuerebuild :: Builder -> Builder
#

This function and the documentation for it are copied from Takano Akio's fast-builder library.

rebuild b is equivalent to b, but it allows GHC to assume that b will be run at most once. This can enable various optimizations that greately improve performance.

There are two types of typical situations where a use of rebuild is often a win:

  • When constructing a builder using a recursive function. e.g. rebuild $ foldr ....

  • When constructing a builder using a conditional expression. e.g. rebuild $ case x of ...