Bytes
Bytes module: raw byte manipulation.
Bytes wraps a String, which in the runtime is an array-backed buffer: march_string is {rc, tag, pad, len, data[]} — one allocation, an explicit length field, and a contiguous NUL-safe payload. Nothing here treats a String as text: string_byte_at, string_slice, string_chars and char_from_int are all byte-indexed in both backends.
This replaced Bytes(List(Int)), which cost one ~32-byte cons cell per byte and made length O(n), get O(i) and slice O(start+len). Measured on a 64 000-byte payload (interpreted): length 544 ms/call, get(last)
- See specs/plans/2026-08-10-array-backed-bytes-design.md.
The wrapper constructor is deliberately kept (a Bytes value is still a one-field boxed ADT cell with its payload at offset 16), because lib/tir/repr.ml, llvm_case.ml and drop.ml all encode assumptions about that shape, and because the C runtime builds these cells by hand.
to_list/from_list were O(1) under the old representation and are O(n) now. That is the one regression; see the design doc's audit of the callers.
This module is self-contained: it does not depend on List. or String. being loaded, so it can be tested or used in isolation.
Types
Functions
Concatenate two Bytes values. O(n).
Decode a Base64 string to Bytes. Returns Err on invalid input.
Decode UTF-8 bytes to a String. Returns Ok(s) always (same as to_string).
Encode Bytes as a Base64 string (with padding).
Encode a String as UTF-8 bytes. Equivalent to from_string for March strings.
Parse a hex string to Bytes. Returns Err if the input is not valid hex.
Wrap a List(Int) of byte values (0–255) as Bytes. O(n).
Convert a String to Bytes (raw UTF-8 byte values, each 0–255). O(1).
Copy [a]'s bytes into a fresh immutable Bytes. O(n) copy.
Byte at position i (0-based). Panics if out of bounds. O(1).
True if the Bytes is empty. O(1).
Number of bytes. O(1).
Sub-slice: bytes from position start with given length. O(len).
Encode Bytes as a lowercase hexadecimal string.
Unwrap Bytes to a List(Int). O(n).
Convert Bytes back to a String (bytes interpreted as UTF-8). O(1).
Copy [b]'s bytes into a fresh NativeU8Arr for SIMD/numeric work. O(n) copy — the layouts cannot be shared.