[Exploration] Specialize list storage for integers, floats, and booleans

JA
11d ago
+2876-378

A list keeps one heap identity while its storage adapts to the values it holds.

ContentsStorage
Integers from 0 through 255One byte per element
Other immediate integersSigned 64-bit slots
Floats64-bit float slots, preserving signed zero and NaN payloads
BooleansOne bit per element
Only NoneA length and a reserved bitmap counted against the memory budget
Mixed types or arbitrary-precision integersGeneric Value slots

Integer, float, and boolean lists can also hold None using a separate one-bit-per-element bitmap. An integer outside the byte range widens the buffer before replacing it. A drained list can reuse its capacity for compatible values or switch storage on refill. Dumps store the logical values in field I and the heap-reference flag in C. A regression test compares the CBOR bytes from each packed strategy with the pre-PR list encoding and checks that each reader accepts the other writer’s bytes. Nonempty lists restore into generic storage. The current dump version is 16 and the oldest supported version is 13.

Integer subscripts read the storage directly. Compatible numeric slice assignments snapshot the right-hand side in typed slots, including self-assignment. Lists constructed from bytes and ranges fill packed buffers directly. The benchmark suite covers reads, writes, copies, construction, and slicing across storage types.

Float measurements

Local release builds before and after adding float storage. Each timing is the median of three run medians, with 25 measured executions per run.

WorkloadGeneric float listPacked float list
Payload for one million floats16 MB8 MB
Build a 200,000-float list, then copy and slice it 16 times6.77 ms0.75 ms
Read 65,536 indexed floats1.35 ms1.36 ms

Earlier byte-storage measurements

These figures compare the original byte-storage commit with the same rebased tree using packed 64-bit integers. Each number is the median of six alternating runs on Apple Silicon.

WorkloadPacked 64-bit integersWith byte storage
Retained bytes for a one-million-item list8.00 MB1.00 MB
list(bytes_data) for one million bytes7.520 ms0.018 ms
Copy the resulting list0.151 ms0.019 ms
Write 256 into the resulting list0.002 ms0.095 ms
Fannkuch0.635 ms0.641 ms
Float workload3.741 ms3.774 ms
CSV writing5.204 ms5.192 ms

Local verification passed 1,218 interpreter tests with memory-model checks, 1,592 Python fixture runs across Monty and CPython, and 123 subprocess tests. The worker tests cover one million nullable floats under a 10 MiB limit and verify that a refused promotion preserves the list. Clippy passed for the interpreter and runtime test targets.