ADR 0068: A list element takes the width of its declared type¶
Status: Final
Type: Feature
Created: 2026-10-27 (as a draft for list[bool] alone)
Amends: 0027 (a list's elements were eight-byte slots), 0053 (the unchecked twins were for int, float and bool only)
Related-ADRs: 0067 (the measurement that asks for it), 0053 (the unchecked accessors, which gain a byte form), 0024 (the slot-descriptor alphabet an element kind is spelt in), 0027 (Array[T], which keeps a separate element width)
Context¶
ADR 0027 stores every list element in an eight-byte slot, and a bool crosses as an Int64 (notes/accept-then-wrong.md §11). On ADR 0067's corpus the sieve stayed well behind the C version after ADR 0053 removed the IndexError checks. The remaining gap was the element size alone: the same C program with int64_t flags in place of bool ran at postpile's speed (notes/measurements.md, 2026-10-27). Eight bytes per flag is eight times the memory traffic; a sieve, a visited set or a dynamic-programming table is that traffic. nqueens holds three boards in list[bool] too.
The draft asked for list[bool] alone, which reads as a special case. It is not one in kind: list[int] and list[float] already hold a raw int64_t or double, the C layout, and a str, bytes, instance or container element is a pointer, eight bytes on every target. Of the element types a list held, bool was the only one with a natural width under eight bytes. The general question is the declared narrow types, Int8 to UInt32 and Float32: list[Int32] was refused rather than widened, because widening would silently change the layout the annotation states.
Decision¶
- A list element takes the width of its declared type. The list header's element kind says what the elements are and fixes the width: one byte for a
bool, the declared width for a narrow number, eight for everything else. One table gives each narrow kind,boolincluded, a kind character (outside the slot-descriptor alphabet), an accessor suffix and a C type. The runtime's width switch, loads, stores and accessors, and each backend's accessor rows, are generated from that table, so no unit can disagree with another about a width. - Helpers that only move elements are width-generic. These move
n × widthbytes: copying, slicing, concatenation, repetition,extend,insert,pop,del,remove,reverse, the moves of a sort,random.shuffleand the buffer's growth. Their counted kinds (astr, an instance, a container) are always eight bytes wide, so the reference-taking paths are unchanged. - Moving an element is not reading its value. A helper that moves scalars one at a time (an extended slice, a sort,
reverse) holds them in an eight-byte form, an integer widened or a float's double bits, and narrows them back exactly. A helper that compares (in,count,index,==/<, the tuple-key hash,copy.deepcopy) reads an integer widened toint64_tor a float widened todouble, so the comparison logic exists once and anInt64needle is never narrowed (2**32 + 5 in xs32is False, as in CPython). A sort of aboollist counts theFalses: two equal bools are indistinguishable, so stability holds trivially. - The accessors that run in loops are per width, the unchecked twins of ADR 0053 included, and each backend's inline accesses gain the narrow loads and stores.
- A narrow element crosses at the width it widens to, an
Int64or aFloat64, as aboolcrosses as anInt640 or 1; the frontend casts a read back to the declared type, so arithmetic on it stays at that type's width and nothing outside the list changes. Narrow C parameters were the alternative and were declined: a sub-word argument is extended by the caller on some ABIs and the callee on others, the LLVM backend marks no extension on calls, and three backends could agree on the garbage in the upper bits. - A narrow element is stored through a type that cannot alias the list's header. A store through a C character type may alias any object, the list's header included, so the C compiler reloads the data pointer before every store in a loop. A
boolelement is a_Bool, as in C'sbool *sieve. A one-byte integer is stored through a one-field struct;int8_t,uint8_tand the other spellings tried all reload. Any later narrow kind is held to the same rule. - Only a list of its own. A narrow element is admitted where
list[T]is the whole annotation (a parameter, a return, a local, a field, a module constant). Inside another list, a union, a tuple or a dict it is refused with a sentence, since those positions hold eight-byte slots; an inferred list of a narrow list is refused the same way. - A
Float32element is single precision. AFloat32element holds whatFloat32arithmetic produced, single precision, where CPython's list would hold the double; the differential tests therefore hold only values postyp's cast produced, which both runs agree on. Literals adopt the element type inxs[i] = 0and[0] * nas they do in a list literal, under the scalar's rule (xs: list[Int8] = [300]is PP410).==and<compare lists of any two integer widths;sumof alist[Float32]adds the doubles.printof alist[Float32]stays refused asprintof a scalarFloat32is (ADR 0004).UInt64stays refused (values above 2**63 do not fit the readers'int64_t), as doesFloat16. - Not bit-packed. One bit per element would cut memory another eightfold, but every access would then need a shift and a mask, with a read-modify-write for each store. One byte matches C's
boolarray, the yardstick. list[int]keeps eight-byteintwords (0075). Narrowing a plainintfrom a value-range analysis would change what a program means behind the declaration; a narrow width is only ever declared.Array[T]stays a separate type (0027's "Two types"); the two may share the width table and nothing else.
Not now¶
dictandsetmembers at their natural width. A hash table's cost is hashing and probing, so narrower members save memory and little time, and no measured program asks. Avisitedset of small ints is faster as alist[bool]anyway.- Instance fields at their natural width, a C struct layout. It saves memory where there are many instances, but it changes the slot-descriptor scheme (ADR 0024). It waits for a program that asks.
- Tuples need nothing: a fixed tuple already holds one field per type (ADR 0058).
Consequences¶
- The cost is the breadth: every list helper is width-aware. The differential suite holds each narrow kind through each of them on three backends, ASan included. Nothing about what a program prints changes.
- With the element a
_Bool, the sieve on the C backend runs at C's time; the first build, through auint8_t, did not move at all, which is what decision 6 records (notes/measurements.md, 2026-10-31). - The LLVM backend's sieve did not gain: the loop there already kept the data pointer in a register and stored one byte, so the gap to C was never the element width. That is a separate item.