hawkw / sharded-slab

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

[WIP]: Add an object pool backed by shards #19

Closed bIgBV closed 4 years ago

bIgBV commented 4 years ago

Add a new API for reusing heap allocation backed by Shards.

This PR adds new APIs to reusing heap allocations. As covered in #2 , when using the Slab, any heap allocated object stored in the slab would be dropped when the object was removed from it. This meant that each object stored in the Slab would have to be individually allocated. The Pool type aims to reuse these allocations by providing an in-place modification APIs.

In order to provide this, the internals of the crate have been significantly refactored in order to allow as much code reuse between the two types as possible. Pool specific APIs have the additional trait bounds of T: Clear + Default and methods specific for this type are in impl blocks bounded by these traits. Slab specific methods are implemented for types generic over Option<T>. This is a pattern which is followed down the abstractions to Slot.

bIgBV commented 4 years ago

I got iterators working. Took some persuasion to convince the compiler (and a lot of begging honestly) that the function pointer being passed was of the right type, but once that was done, it fit into place with existing iterator code perfectly.

I also updated the public API for Pool. There are now two main methods to get a new object, create and create_with.

One thing I want to address before getting started on tests is safety. We are calling the passed callback inside an unsafe block:

https://github.com/hawkw/sharded-slab/blob/4d8b405955276373d050a1b74cdd57d1741685bf/src/page/slot.rs#L332-L335

If the closure causes a panic, then we might be left in an inconsistent state. I think we need to use catch_unwind here, but I wanted to run this by before I went in that direction.