hawkw / sharded-slab

a lock-free concurrent slab (experimental)
MIT License
269 stars 17 forks source link

out pointer/placement new-style API for insert #15

Closed hawkw closed 4 years ago

hawkw commented 4 years ago

it would be nice to not have to move data on the stack to insert into the slab, esp. since the intention is to provide preallocated storage. Currently, we use Option<T> for our slots, and move an item into the Option (making it Some) when inserting. This means copying data onto the stack, which is a shame if the slab is being used to store large objects:

We might want to consider using MaybeUninit instead, so we can have an API like

pub fn insert_ptr(&self) -> Option<(usize, &mut MaybeUninit<T>>)>

with the MaybeUninit reference being used as an out-param.

It might be better for this to be more like

pub fn insert_with(&self, f: impl FnOnce(&mut MaybeUninit<T>)) -> Option<usize>

since this would play nicer with loom's CausalCell::with, and because users wouldn't have to unpack the tuple.

This has the disadvantage of forcing users to deal with MaybeUninit, which would currently require unsafe code and is somewhat significantly less ergonomic. However, we can continue providing the current API as well, implemented on top of a MaybeUninit-based API.