A request that fits nowhere reads every slot, so alloc is O(S)
A request that fits nowhere scans every slot, so alloc is O(S) for an arena of S units. free clears one slot per unit of the block.
Measured on arenas of S units holding 100 live blocks, each followed by a free gap as long as itself, with a request one unit bigger than any gap, so it fits nowhere:
Predict
The slot scan took 0.27 ms on an arena of 10,000 units. About how long on 10,000,000?
S, gap
Slot scan, ms
bytearray.find, ms
Free list walk, µs
10,000, 50
0.270
0.017
2.15
100,000, 500
2.822
0.188
2.83
1,000,000, 5,000
29.837
1.918
2.92
10,000,000, 50,000
300.211
19.250
2.83
The free list walk comes in the next section.
That's Python 3.12, best of 3 runs for the slot scan and 5 for the others, on the laptop these numbers come from.
Ten times the arena, ten times the scan: O(S).
Moving the scan into C helps by a constant. A bytearray with one byte per unit, searched with find(bytes(n)) for n zero bytes in a row, ran about 15 times faster and grew the same way.
The last column holds the same 100 gaps as 100 runs, and its time didn't move with S.
For an 80 GiB arena, one slot per unit is 80 GiB of bookkeeping or more
The memory is worse than the time. By tracemalloc, the list held 8.00 bytes per unit: 80,000,496 bytes for ten million units. The bytearray held 1 byte per unit.
The practice problem's arena is 80 GiB, one unit per byte: 85,899,345,920 units. One slot each is 80 GiB of bookkeeping as a bytearray and 640 GiB as a list, to manage 80 GiB. No speedup rescues that. So store the free space instead of the units.
runs_from_slots(slots) takes the slots of SlotAllocator and returns the free space as half-open runs. Write it, and the slots of the first figure after free(3) become two runs.
Pick an answer above to see the rest
Instructions
1
Write runs_from_slots(slots): every stretch of None slots in a row, as a half-open (start, end) where end is one past its last slot, in address order.