3 ms·
> The main benefit of parking lot is that it makes locks very small, which then encourages the use of fine grained locking. For example, in JavaScriptCore (Park
by scottlamb 11mo ago
> The main benefit of parking lot is that it makes locks very small, which then encourages the use of fine grained locking. For example, in JavaScriptCore (ParkingLot’s first customer), we stuff a 2-bit lock into every object header - so if there is ever a need to do some locking for internal VM reasons on any object we can do that without increasing the size of the object
IMHO that's a very cool feature which is essentially wasted when using it as a `Mutex<InnerBlah>` because the mutex's size will get rounded up to the alignment of `InnerBlah`. And even when not doing that, afaict `parking_lot` doesn't expose a way to use the remaining six bits in `parking_lot::RawMutex`. I think the new std mutexes made the right choice to use a different design.
> I’m surprised that this only compared to std on one platform (Linux).
Can't speak for the author, but I suspect a lot of people really only care about performance under Linux. I write software that I often develop from a Mac but almost entirely deploy on Linux. (But speaking of Macs: std::mutex doesn't yet use futexes on macOS. Might happen soon. https://github.com/rust-lang/rust/pull/122408 https://github.com/rust-lang/rust/pull/122408)
- pizlonator 11mo ago> I suspect a lot of people really only care about performance under Linux Yeah this is true
- Rusky 11mo agoHypothetically Rust could make `Mutex<InnerBlah>` work with just two bits in the same way it makes `Option<&T>` the same size as `&T`. Annotate `InnerBlah` with the information about which bits are available and let `Mutex` use them.
- timClicks 11mo agoReferences only have a single bit available as a niche (the null byte), which Option makes use of for null pointer optimization (https://doc.rust-lang.org/std/option/index.html#representation https://doc.rust-lang.org/std/option/index.html#representati...). In principle, you Rust could create something like std::num::NonZero and its corresponding sealed trait ZeroablePrimitive to mark that two bits are unused. But that doesn't exist yet as far as I know.
- Rusky 11mo agoThere are also currently the unstable rustc_layout_scalar_valid_range_start and rustc_layout_scalar_valid_range_end attributes (which are used in the definition of NonNull, etc.) which could be used for some bit patterns. Also aspirations to use pattern types for this sort of thing: https://github.com/rust-lang/rust/issues/135996 https://github.com/rust-lang/rust/issues/135996
- scottlamb 11mo agoThere was talk of Rust allowing stride != alignment. [1] I think this would mean if say `InnerBlah` has size 15 and alignment 8, `parking_lot::Mutex<InnerBlah>` can be size 16 rather than the current 24. Same would be true for an `OuterBlah` the mutex is one field of. But I don't think it'll happen. [1] e.g. https://internals.rust-lang.org/t/pre-rfc-allow-array-stride-size/17933/35 https://internals.rust-lang.org/t/pre-rfc-allow-array-stride...
- torginus 11mo agoI think he meant 1 byte on the heap for the shared state, on the stack it's larger. Which is fine since in Rust we almost always have the mutex in function scope as long as we're using it.