optimistic-cell 0.1.0

a simple lock-like structure for low-overhead optimistic concurrency
Documentation
  • Coverage
  • 14.29%
    1 out of 7 items documented0 out of 6 items with examples
  • Size
  • Source code size: 6.72 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.51 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • komora-io/optimistic-cell
    21 2 1
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • spacejam

optimistic-cell

A highly cache-efficient lock-like container for working with concurrent data where reads may take place optimistically and without modifying any cachelines.

Due to the fact that reads may access data that races with writes and is only validated later, only items that are marked as Copy may be read optimistically.

The write guard is essentially just a plain spinlock.

let n: u32 = 128 * 1024 * 1024;
let concurrency = 4;

let cell = &OptimisticCell::new(0);
let barrier = &std::sync::Barrier::new(concurrency as _);

let before = std::time::Instant::now();
std::thread::scope(|s| {
    let mut threads = vec![];
    for _ in 0..concurrency {
        let thread = s.spawn(move || {
            barrier.wait();

            for _ in 0..n {
                let read_1 = cell.read();

                let mut lock = cell.lock();
                *lock += 1;
                drop(lock);

                let read_2 = cell.read();

                assert_ne!(read_1, read_2);
            }
        });

        threads.push(thread);
    }
    for thread in threads {
        thread.join().unwrap();
    }
});
dbg!(before.elapsed());

Workload scalability is highly dependent on the frequency of writes, and the duration that a writer may hold the cell locked for before dropping it.