---
title: "Flat Values"
description: "Canonical offset-based layouts for values stored in a runtime arena."
---

> Documentation Index
> Fetch the complete documentation index at: https://docs.atoll.workreef.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Flat Values

“Flat” describes the canonical in-arena representation of an Atoll value. A
flat value uses fixed-width scalar fields and arena-relative handles rather than
native pointers. The complete object graph can therefore move with its
[arena](https://docs.atoll.workreef.dev/runtime/memory/arenas/) without rewriting internal references.

Flat values are runtime memory layouts. They are not the removed legacy
`FlatMessage` wire codec, and an arbitrary arena image is not automatically a
stable cross-version serialization format.

## Encoding rules

- multi-byte numbers are little-endian;
- primitive values use their natural canonical width, listed below;
- `Char` uses the four-byte `u32` Unicode scalar representation;
- structs use source field order with C-like alignment and padding;
- enums store an `i32` discriminant at offset `0`, followed by the selected
  payload layout;
- a stored pointer is **eight bytes on every target** — `AtollPtr` is a `u64`
  whose low 56 bits are the address and whose top byte is a tag, so one layout
  serves wasm32 and wasm64 alike;
- pointer `0` represents null or an empty niche where the type permits it.

**This page said "four-byte handle" throughout, and it dated from before the
pointer widened.** That prose was load-bearing: two miscompile reports were
filed during the pointer-width audit by readers deriving an extent from it.
Every width below is derived from `atoll_abi::ptr::ATOLL_PTR_BYTES` and
`atoll_sema::layouts`, and a length, a capacity, an index or a count is still
a `u32` — no single block reaches 4 GiB.

Padding belongs to the layout but does not carry language-level data.

The canonical collection-element widths on wasm32 are:

| Width | Kinds |
|---:|---|
| 1 byte | `i8`, `byte`, `bool` |
| 2 bytes | `i16`, `u16` |
| 4 bytes | `i32`, `u32`, `f32`, `char` |
| 8 bytes | `int`, `u64`, `f64`, a stored pointer |
| 16 bytes | `i128`, `u128` |

WebAssembly evaluation lanes can be wider than stored fields. For example, a
one-byte `bool` is carried in an `i32` Wasm lane.

## Resolving handles

Suppose a struct field at offset `20` contains the value `96`. `96` is not a
WebAssembly address. It resolves relative to the current body base:

```text
field address  = arena body base + 20
block address  = arena body base + 96
```

After growth, the body base may differ, but the values `20` and `96` still
describe the same relationship.

A handle is meaningful only with the arena that owns it. Passing a bare offset
between Unit instances, link groups, or restored arena generations without an
explicit transfer protocol is invalid.

## Strings and lists

**A collection's counts live in the value, not in its block.** A `string` and a
`List[T]` are the same sixteen-byte fat value:

| Offset | Field | Width |
|---:|---|---:|
| `0` | `data` — points at **element 0** | 8 |
| `8` | `len` | 4 |
| `12` | `cap` | 4 |

The two `u32`s land in what would otherwise be the pointer's tail padding, so
the value is sixteen bytes rather than twenty-four. `string` *is* `List[byte]`:
`len` is bytes, indexing is byte-indexed, and nothing in the type decodes UTF-8.

There is **no block header**. `data` addresses the first element directly, so
there is no `+ 8` to skip and no length in front of the elements.

The count a block does keep is at the **other end**, in the slack its size class
already leaves: four bytes past the last element, found from the pointer's own
tag with no memory read. It is written when a reference into the block is taken
and read when that reference is the last owner — the one case where a release
arrives with an address and no value to ask. See
[References](https://docs.atoll.workreef.dev/language/memory/references/) for what a count cannot answer for: a
destructor **body** owes one run per name, so the mutation under a live reference
is refused rather than the reference.

`data == 0` is the empty representation. A `cap` of zero with a non-null `data`
is the third state: a **window** into somebody else's block, which must never be
asked whether it is uniquely owned.

Elements are laid out at the element type's canonical size and alignment.
Reference-containing elements hold their own pointers and participate in the
generated release and collection walks.

## Maps and sets

`Map[K, V]` and `Set[T]` are a four-lane value over an Abseil-style Swiss table:

| Offset | Field | Width |
|---:|---|---:|
| `0` | `data` — points at **control byte 0** | 8 |
| `8` | `len` | 4 |
| `12` | `cap` | 4 |
| `16` | `tombstones` | 4 |

The block is `ctrl[cap + 16]`, then `cap` slots aligned to the wider of `K`
and `V`, each a key followed by its value — so a lookup that hits reads the
control byte and one slot, not a key region and a value region apart. The
trailing sixteen control bytes mirror
`ctrl[0..16]`, so a SIMD group load that straddles the wrap still sees the
wrapped slots. Control byte `0x80` marks an empty slot and `0xFE` a tombstone;
a slot is occupied iff its control byte is below `0x80`, and the remaining
values carry hash fragments used while probing.

There is no header here either, and it is the same change that moved a
collection's counts into its value. `Set[T]` is `Map[T, ()]`'s table, its
slots holding a key and a value of no size, sharing the control values, the
probing discipline and the ownership rules.

A table's internal layout is reached through generated code and the runtime's
own readers (`atoll::Map`, `atoll::Set`), whose geometry is pinned against a
compiled Atoll program. Do not assume it from application code.

**This page described a header that was deleted**, and so does
`atoll-abi`'s own `collections` module, which still exports `LIST_HEADER = 8`
and `TABLE_HEADER = 16` under a note saying not to build on them. The last
place that still modelled the header, an `arena.rs` beside the allocator, is
gone: its `ArenaMap::cap()` read a control byte as a count and answered `0x80808080` on a
live table, and it survived as long as it did because its tests wrote with the
matching writer and read back self-consistently — writer and reader sharing one
wrong constant, closing a round trip over a layout that does not exist.

## Function values

Compiled function identities are dense `u32` `FunctionId` values for one build.
They are not stable symbols across program versions. A Unit instance containing
stored function values is therefore not automatically safe to adopt during a
redeploy; the deployment compatibility check must prove that the relevant
mapping remains valid or reject migration.

## Checked views

Flat access has two trust levels:

- checked access validates offsets, lengths, alignment, discriminants, and
  UTF-8 before exposing typed data;
- trusted `Safe` views avoid repeating checks after a builder or runtime
  boundary has established the complete layout.

Trusted does not mean that bytes from an untrusted host or old program version
may skip validation. It describes a proof already obtained in the current
operation.

## Copying

Copying the complete `[FlatBuilderHeader | arena body]` preserves all
intra-arena relationships. Copying one referenced block alone generally does
not: its nested handles still point into the source arena. Cross-arena value
transfer therefore requires a generated deep clone or a typed marshal/unmarshal
operation.

Host calls use typed layouts rather than exposing raw native pointers. At
suspension boundaries, code retains handles and offsets or participates in the
generation-pin protocol described in [Host I/O](https://docs.atoll.workreef.dev/runtime/host-io/).

## Evolution

Flat layouts are shared by compiler-generated code and the runtime. Changes
must be coordinated through ABI/version metadata and migration compatibility
checks. In particular:

- never infer current sizes from old prose when generated constants disagree;
- never persist a build-local `FunctionId` as a cross-version identity;
- never reinterpret an arena from a different layout fingerprint;
- validate external or restored bytes before creating a trusted view.

Source: https://docs.atoll.workreef.dev/runtime/memory/flat/index.mdx
