HORIZON HASKELLDocslts/ghc-9.14.x7a6c33e2026-10-12Search packagesCtrl K
Theme: |

GHC 9.14.1 · lts/ghc-9.14.x · 7a6c33e · 2026-10-12

Modulebytebuild-0.3.17.0Haskell2010

Data.Bytes.Builder

Contents
  1. Bounded Primitives
  2. Evaluation
  3. Materialized Byte Sequences
  4. Byte Sequence Encodings
  5. Encode Integral Types
  6. Human-Readable
  7. Unsigned Words
  8. 64-bit
  9. 32-bit
  10. 16-bit
  11. 8-bit
  12. Machine-Readable
  13. One
  14. Big Endian
  15. Little Endian
  16. LEB128
  17. VLQ
  18. Many
  19. Big Endian
  20. Little Endian
  21. Prefixing with Length
  22. Encode Floating-Point Types
  23. Human-Readable
  24. Replication
  25. Control
  26. Rebuild
  27. IO
Synopsis

Bounded Primitives

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.17.0 · Data.Bytes.Builder.Unsafe
  • Semigroup BuilderDefined in bytebuild-0.3.17.0 · Data.Bytes.Builder.Unsafe
  • Monoid BuilderDefined in bytebuild-0.3.17.0 · Data.Bytes.Builder.Unsafe
  • ToBuilder BuilderDefined in bytebuild-0.3.17.0 · 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

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

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)

Byte Sequence Encodings

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

Human-Readable

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

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

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.

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.

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

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.

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.

Machine-Readable

One

Big Endian

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

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

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

Human-Readable

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

Control

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

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 ...

IO