ADR 0035: One descriptor per class, and the union-typed field¶
Status: Final
Type: Architecture
Created: 2026-08-02
Amended-By: 0065 (the header gains the instance's class)
Related-ADRs: 0024 (the memory model this layout serves, where the container kinds this ADR reserved are now spent), 0029 (the union box a u slot owns). This ADR replaced the per-kind bitmaps of 0034 and of the first Str ownership rule.
Context¶
The release walk needs to know which slots of a dying instance hold counted references. The first answer was a 64-bit mask in the header for Str slots. The second was another mask for instance slots, a union-typed field would have been the third, and the counted kinds already numbered six. A word per kind grows the header without bound, caps the field count at the word width, and adds a word with each new kind. At three masks the trade flips. One pointer answers for every kind that will ever exist; a new kind costs one character.
Decision¶
The instance header holds a reference count and a pointer to a static, per-class string; one character per slot says what releasing that slot owes:
- nothing to release s a Str y a bytes o another instance
u a union box l / d / e list, dict, set
w an int word that may hold a big int F a file's stream
w is 0075's and F is 0061's. A class whose fields are (int, str, Node) has the descriptor "-so". The header's third word is the instance's class number, which method dispatch reads (0065) and the release walk does not; the allocator takes the descriptor where it took the masks.
The descriptor is emitted through the Str literal table every backend already has. It is interned, emitted as the same immortal static object as any other literal, and crosses as the const char * those already cross as, so it cost no backend a line. NUL terminates it, so no kind is the zero byte and the release walk needs no slot count.
The descriptor is a string so that it stays legible. A reader of the generated C sees
and can read the layout off it. This project's C is meant to be read.
Containers answer the same question with one character in their header, since a container's elements are homogeneous where a class's fields are heterogeneous. One shared function answers both shapes, so a kind added once reaches both. The zero byte is outside the alphabet, so an unconverted caller aborts on the runtime's tripwire and never reads "scalar".
The union-typed field is what the descriptor was bought for first. A u slot owns a box. The setter is the Str/instance setter with one release function changed. POST has no generics, so a union's case list lives on the slot. For a field that means the class records it, as a channel's element type lives on the slot holding the channel and a list's on the slot holding the list. Widening applies at the field store as it does at a union-typed return.
Consequences¶
- A cheaper header and no field limit, from a change made for a third reason. The 64-field refusal existed because slot 65 had no bit and a silently dropped bit would be a use-after-free; the descriptor is as long as the class has slots, so the limit retired with the mechanism that needed it.
- The next kind is one character plus one accessor pair and one release-walk case. The claim has been tested three times since (list, dict, set fields) and held each time with zero backend lines.
- The object and union runtime units release each other's payloads, so neither links without the other; the counted-heap linkage closure in 0024 is where that dependency is decided once.
Alternatives considered¶
- A type tag: offered alongside the destructor and the bitmap when the debt was first written down. A tag answers what class is this?, which no release walk has asked; the class number 0065 added answers it for method dispatch.
- A per-class destructor function: workable, but it moves layout knowledge into generated per-class code on every backend; the descriptor keeps it in one runtime file and costs the backends nothing.
- Two bits per slot in a packed word: keeps the header at two words by halving the field limit; the descriptor removes the limit instead.