Generational Handle Pool
Prerequisites: Container Invalidation and Stable Handles, Dangling References, string_view, and span, Raw Pointers, unique_ptr, shared_ptr, and weak_ptr
Build the pool that holds the order book's resting AAPL orders. The engine refers to an order by handle, not pointer. When a filled or cancelled order's slot is reused, an old handle must report the order gone instead of reading the new one.
- Slot: one entry of the pool's slot array, numbered from 0.
- Generation: a per-slot counter. It is 0 when the slot is created and goes up by 1 each time the slot's object is destroyed.
- Handle: an object's slot plus that slot's generation when the object was created. Written
index:generation, two decimal integers joined by a colon with no spaces:3:2is slot 3 at generation 2. - Live handle: its index is less than the number of slots, that slot holds an object, and the slot's generation equals the handle's.
- Stale handle: any other handle. Its object was destroyed, or no object ever had that handle.
What to implement
process_commands(capacity, max_generation, commands)returns the printed lines in order. Each command is one line and prints exactly one line.capacityandmax_generationare the pool's limits (Part 2). Part 1's tests set them too high to matter.- An object's value is an integer from 0 to 109: the order's remaining quantity in shares.
- On stdin, the first two lines are
capacityandmax_generation. The third is the number of commands, and each remaining line is one command. - There are three parts, each adding to the previous ones. One solution must pass all ten tests.
| Command | Effect | Prints |
|---|---|---|
CREATE value | Store a new object | its handle, or FULL (Part 2) |
GET handle | Read an object | its value, or STALE |
SET handle value | Replace an object's value | OK, or STALE |
DESTROY handle | Remove an object | OK, RETIRED (Part 2), or STALE |
STATS | Report the pool's size | STATS live slots free |
SCAN | List the live objects (Part 3) | SCAN and one handle=value per object, or SCAN NONE |
Part 1: Create, read, destroy, and reuse
CREATE valueplaces a new object in a slot and prints its handle.- If a slot is free,
CREATEreuses the most recently freed one: the free slots form a free list used as a stack. A reused slot keeps the generation it received when freed. - If no slot is free, the pool appends a new slot. Its index is the number of slots before it, and its generation is 0.
GET handleprints the value.SET handle valuereplaces it and printsOK.DESTROY handleremoves the object, adds 1 to its slot's generation, pushes the slot onto the free list, and printsOK.GET,SET, andDESTROYwith a stale handle printSTALEand change nothing. So a second destroy of the same object printsSTALEand does not free its slot again.- The slot array never shrinks.
The new generation makes every copy of the old handle stale, so it can't reach the slot's next occupant. That is the fix for the ABA problem from Container Invalidation and Stable Handles.
A free slot's generation can equal a handle's. After DESTROY 1:0, slot 1 is free at generation 1, and 1:1 stays stale until a CREATE reuses slot 1, because the slot holds no object.
Output of STATS:
STATS live slots free
live: the number of objects in the pool.slots: the length of the slot array.free: the length of the free list.
Limits and guarantees
- At most 105 commands.
capacityis 1 to 105, andmax_generationis 0 to 109. - Every command is well formed. Every handle has the
index:generationform, both numbers from 0 to 109. - A handle need not be one the pool ever printed: a component can hold one long after its object is gone, and a corrupted message can carry one that never existed.
- The
SCANlines print at most 106 objects in total.
Tests 1–3 cover this part.
Part 2: Fixed capacity and retired slots
A real pool is sized once at startup, and its generation lives in a fixed number of bits. Wrapping back to 0 would make a long-ago handle live again and read a different object, so this pool never wraps.
- The slot array holds at most
capacityslots. ACREATEthat finds the free list empty and the slot array alreadycapacityslots long printsFULLand changes nothing. - A
DESTROYwhose slot's generation is alreadymax_generationretires the slot and printsRETIREDinstead ofOK. The slot stays empty at generationmax_generation, is not pushed onto the free list, and no laterCREATEreuses it. - A retired slot stays in the slot array and counts toward
capacity. A pool whose slots have all retired printsFULLeven when it holds no objects. STATScounts a retired slot inslotsbut not infree, soslots - live - freeis the number of retired slots.- With
max_generationequal to 0, every destroy retires its slot.
Tests 4–7 cover this part.
Part 3: Iterating the live objects
Repricing every resting order after a price move visits every live object, but the slot array also holds free and retired slots. So the pool keeps a dense array: the values of exactly the live objects, packed with no gaps, each with its owning slot.
CREATEappends the new object to the end of the dense array.DESTROY, whether it printsOKorRETIRED, does a swap-remove: the object at the end of the dense array moves into the destroyed object's position, and the array shrinks by one. If the destroyed object is already at the end, nothing moves.- The moved object's handle does not change; only its position does.
SETchanges a value in place.FULLand every command that printsSTALEleave the dense array unchanged.
Output of SCAN:
SCAN handle=value handle=value ...
- One
handle=valueentry per live object, in dense-array order, separated by single spaces. For example:SCAN 0:0=10 3:0=40 2:0=30. - With no live objects it prints
SCAN NONE.
Tests 8–10 cover this part.
1000 1000000 12 CREATE 300 CREATE 150 CREATE 500 GET 1:0 DESTROY 1:0 GET 1:0 CREATE 200 GET 1:0 GET 1:1 SET 0:0 250 GET 0:0 STATS
0:0 1:0 2:0 150 OK STALE 1:1 STALE 200 OK 250 STATS 3 3 0
The first two lines are capacity and max_generation, both too large to matter here. Three orders fill slots 0, 1, and 2 at generation 0.
Destroying 1:0 frees slot 1 and raises its generation to 1, so GET 1:0 is stale. The next CREATE reuses slot 1 and prints 1:1.
The old handle 1:0 still reads STALE, not the new order's 200, while 1:1 reads 200. At the end the pool holds three objects in three slots, and the free list is empty.