10/34
Part 3 of 7 · MergingLearn and do

Free a block, and merge it with the free runs on each side

Handing memory out is half the job. Now take it back.

Freed runs that never merge break the arena into pieces

Three blocks of 4, all freed
01234567891011
three blocks
AAAABBBBCCCC
all freed, not merged
Three runs of 4: alloc(12) returns None.
all freed, merged
One run of 12: alloc(12) returns 0.

free puts the block's units back as a run. Putting it back in order isn't enough. Allocate three blocks of 4 in an arena of 12 and free all three. Put each back with insort, which inserts into a sorted list, and the free list is [(0, 4), (4, 8), (8, 12)]. The whole arena is free, and a request for 12 returns None, because no single run holds 12.

Over a longer run it gets much worse. Here's a workload on an arena of 10,000 units with the take you just wrote. It runs 20,000 steps. Each step allocates a random 1 to 100 units while fewer than 60% of the units are in use, and otherwise frees a random live block. random.Random(1) draws the sizes.

Predict

20,000 random steps on an arena of 10,000 units, with freed runs never merged. About what share of requests failed?

Pick an answer above to see the rest