oconnor663 / riddance

a reservable, retiring, recyclable slotmap/arena (WIP)
6 stars 1 forks source link

Reintroduce the linked list, use atomic pointer bumps? #5

Open oconnor663 opened 2 months ago

oconnor663 commented 2 months ago

I could use a free-linked-list (and a retired-linked-list) instead of the Vecs I'm using now. To support atomic reservations, the reservation iterator could do a compare_exchange loop, bumping the head of the free list forward. The iterators would know in advance how many free slots they "own" based on the result of the fetch_add that we did when we constructed each one. (And just like today, one of them might wind up with a mix of reused and newly allocated slots.) However, the specific reused slots that each iterator gets wouldn't be known until iterator, and different threads could race (safely, atomically) to claim individual slots.

Registry::allocate_empty_slots should not need to touch the linked lists. It only decrements the free list length (non-atomic) by the current value of the reservation cursor (atomic), with a saturating_sub. If all the reservation iterators ran to completion, then the list head is already bumped to where it should be. If some of the iterators were dropped before finishing, then the list head will be behind, and the saturating_sub will effectively leak some slots at the end of the free list.

However, it's interesting that we can detect this condition. We could provide a method like Registry::sanity_check_the_free_list, which could find the real length of the free list in O(n) time and compare that to the stored length. If the real length is greater, then some iterators were dropped before they were finished, and we can correct the stored length. (This method will take &mut self, so the lifetime restrictions on the reservation iterators will guarantee that they're all dead and gone by this point.)

oconnor663 commented 2 months ago

As part of this I should maybe go back to using u32 everywhere instead of usize everywhere.