13.8. Serialization

Serialization placement runs on each pool’s tree after segmentation (Segmentation and cross-language calls) and before ownership planning (Ownership). It decides where a value changes between its native form and its serialized form, and for each type and language it builds a serialization plan: the schema string, the native type, and the conversions a value needs at a crossing. Each pool writes the same voidstar layout for a type (Voidstar format and schema strings), so two pools agree on a value with no negotiation.

Two states. Inside a pool, every value is either native (an object of the pool’s language) or serialized (a data packet, Packet protocol). An argument arrives serialized; a result and an argument to a foreign call leave serialized. The pass gives every subexpression one of the two states and inserts a transition only where a use needs the other one. An argument is decoded at most once, at the entry of the function that receives it, and only if some use needs its native value. A value a pool only passes on to another pool stays a packet and is never decoded. This is also how a pool carries a value of a type it cannot hold, such as a record with no native form in a compiled language: the packet passes through unchanged.

The plan. For a type in a language, the plan is a tree with one node per type constructor. Each node records the wire shape (primitive, list, tuple, record, enum, variant, optional, table, stream handle, function) and the language’s native name for the constructor at that point. Three things are read off one plan, so they cannot disagree:

Output Use

Schema with hints

Given to the pool’s binding, which builds the named native container (<dict> in Python) while reading or writing the layout.

Schema without hints

Stored in packets written to disk and in the manifest. Hints never change the layout, so this form is the same in every language.

Native type and conversions

The type the generated code declares, and the Packable calls placed before writing and after reading.

Aliases are expanded to the type they name, which gives the layout; an alias’s own native form, if it has one, supplies the hint. A built-in width such as I64 stops the expansion, so an alias of Int keeps the width it names. A data type with no fields in any constructor is an enum; one with fields is a variant whose arms are built from the declaration, in declaration order, since the ordinal is the wire tag.

Newtypes and Packable. A newtype crosses in the form of its representation (Naming a type: type and newtype). Where the language’s native form for the newtype is the representation’s form, or a list or tuple container the binding can build directly, no conversion is needed. Otherwise the plan routes through a Packable instance (Serializing custom types with Packable): the instance Packable W N gives unpack :: N → W and pack :: W → N, where W is the newtype’s representation with its parameters substituted. A sender calls unpack and writes W; a receiver reads W and calls pack. An intrinsic that acts on the value as it crosses (@save, @show, @hash) also unpacks first, and one that yields a value read from data (@load, @read) packs its result. A record or data type has a wire form of its own and never takes a Packable instance.

Choosing and checking instances. In each language the plan takes the most specific instance whose head covers the use site; two equally specific candidates are an ambiguity error, and an instance with no implementation in the language that needs it is an error naming that language. Across the program, every pair of instances whose heads can be instantiated to a common type must give it the same wire form, or the program is rejected. Because the wire form is fixed by the newtype’s declaration, the instance chosen in one language and the instance chosen in another always write and read the same layout.

Function values. A function crosses by defunctionalization. Every function value that may cross is a generated function with a number in its home pool, applied to zero or more captured values. Its wire form does not depend on its signature: the tuple of the home language’s name, the function number, and the captured values as self-contained packets, schema t3sjaau1. Captured values are embedded in their packets rather than referenced in shared memory, because the closure may be applied after the dispatch that built it has released its blocks.

Diagram
Diagram

The receiving pool reflects the tuple into a native callable of the right arity, using the argument and result plans the closure’s type gives. Calling it sends a call to the home pool with the captured packets followed by the new arguments. A reflected closure remembers its origin, so passing it on or back sends the original tuple, never a chain of forwarding closures. A function value can be a field of a record or an element of a list; the enclosing value’s layout carries the tuple in that slot. A suspension <E> T crosses the same way, as a closure of no arguments, and is never run by the crossing (Effects). A callable that host code created has no number to call back into: it is refused where the compiler can see it and fails the call otherwise. A function or suspension has no file form, so the data-writing and data-reading intrinsics refuse types that contain one.

Recursive types. While building a plan the compiler keeps the set of type instantiations on the path from the root. Meeting one of them again ends the descent with a back-reference node. In the schema, the enclosing node is declared with & and a name, and the occurrence refers to it with ^ (Voidstar format and schema strings). The key is the whole instantiation, not the name: a Tree Int inside a Tree Str is a different type and is expanded on its own. A plan whose expansion does not terminate is rejected. The layout stays finite because a variant’s payload, an optional and an array are behind pointers.

Tables and handles. A table’s plan is a table node listing its declared columns, written T or T: followed by the columns. The value travels as an Arrow block rather than voidstar, and the declared columns are a lower bound checked when the block is received (Tables and tensors on the wire). A stream or file handle travels as the file’s path, and the receiving pool opens its own handle on it (Streaming and memory mapping).

Strings. Where the receiving language cannot hold a NUL byte in a string, the generated code checks received strings for one (NUL bytes in strings). The check is emitted only for types that contain a string, so other types pay nothing.