hawkw / sharded-slab

a lock-free concurrent slab (experimental)
MIT License
273 stars 19 forks source link

fix(Slab): invalid generation in case of custom config #80

Closed loyd closed 1 year ago

loyd commented 1 year ago

Hi, I used the following config and found the incorrect calculations of the generation and reference counter. After debugging, I discovered that sometimes Generation::pack is used instead of LifecycleGen::pack, which can be fixed by this PR.

My config:

struct SlabConfig;
impl sharded_slab::Config for SlabConfig {
    const INITIAL_PAGE_SIZE: usize = 32;
    const MAX_PAGES: usize = 15;
    const MAX_THREADS: usize = 256;
    const RESERVED_BITS: usize = 24;
}

The bug cannot be reproduced with the default config, because Generation and LifecycleGen have the same Pack::SHIFT.

It would be nice to remove impl Pack for Generation in favor of different impl Pack for KeyGen and impl Pack for LifecycleGen, but I decided not to do any extra refactoring, which you may not accept.

If the patch is ok for you, I can add a test to prevent regression in the future.

loyd commented 1 year ago

@hawkw, can you check this PR, please?

loyd commented 1 year ago

@hawkw, I've added two integrational tests (I consider them more reliable for preventing regression than the unit ones) that cover 3/5 changed places. I couldn't write a test in half an hour to reveal bugs in fn release2() and fn release() methods (both in integrational and loom tests), so let's merge these ones, it's better than nothing.

hawkw commented 1 year ago

@loyd Okay, that sounds good to me. Thanks for taking the time to add tests at all!

loyd commented 1 year ago

@hawkw, can you release a new release?

hawkw commented 1 year ago

@hawkw, can you release a new release?

@loyd https://crates.io/crates/sharded-slab/0.1.5 :tada: --- sorry to keep you waiting!

hawkw commented 1 year ago

@loyd I've just published a new crates.io release, v0.1.6 to crates.io, which should once again include your changes from this PR. Sorry we had to yank v0.1.5!