3 ms·
I'm curious how folks in Rust implement striped-locking? Striped locking is where you chunk sections of data behind the same lock to improve scalability. So, 1
by gavinray 4y ago
I'm curious how folks in Rust implement striped-locking?
Striped locking is where you chunk sections of data behind the same lock to improve scalability. So, 1 lock might protect 1,000 items in an array, for example.
- lilyball 4y agoI've never done this but I imagine you could use something like [Mutex<[T; 1000]>; 10]
- remexre 4y agoFirst thing that'd come to mind for me would be replacing the unstriped Vec<Mutex<T>> with some wrapper like: struct Striped<T, const StripeSize: usize>(Vec<Mutex<[T; StripeSize]>>, Mutex<tinyvec::ArrayVec<[T; StripeSize]>>); I think I'd probably want a wrapper either way to ensure I'm always taking locks in a particular order to avoid deadlocks, anyway.
- tick_tock_tick 4y agoRealistically you don't unless you really really need it. If you really need it you drop to unsafe and build a "safe" interface in-front of it. Rust makes writing very high performance multi-threaded code incredibly hard because most of the tricks to get that last bit of performance out are basically asking to generated races and are near impossible to express in a way that is provably safe. You basically thinking of your data-layout from day one if you need that kind of performance your just going to have to deal with a bunch of unsafe in your codebase.