values are "dynamic types" (trait objects, dyn Trait) stored on the heap (Box<>). They implement the trait / "virtual method" for as-bytes representation when taken by a reference (AsRef<[u8]>)
To allow being used concurrently from multiple threads the values are also marked Send and Sync meaning that can - and must be able to - safely shared between threads.
Additionally the Hash Table is wrapped into an atomically reference counted smart pointer (Arc<>) and a Mutex to allow concurrent usage of the whole object
I thought mutex meant Mutually exclusive and only allowed access to one thread at a time. It does allow multiple threads the access, but not concurrently, right?
That depends on your datastructures and how much you care about all readers seeing the most up to date version of the data, there are ways to ensure it either sees a past version or the correct one, but not an intermediate state.
No, we have closures. Memory safety is enforced with the borrow checker at compile time. Essentially you can have many immutable references OR one mutable reference at a time, not both. I haven't touched mutexes very much in rust but i believe that it's basically a pointer to the data you want (so it's cheap to clone) that blocks the thread until it can acquire a lock in the event you want to get at the data inside
Memory safety is enforced with the borrow checker at compile time. Essentially you can have many immutable references OR one mutable reference at a time, not both.
Ah, that's interesting and would indeed complicate the implementation of certain structures I was thinking of.
I haven't messed much with Rust beyond the most basic of things so far.
Yeah, the naming makes it sound a little paradoxical.
Fundamentally you can't (successfully) have two processes editing the same memory piece at the same time. Having a mutex enforcing only one process editing a piece of an object at a time means you can safely have multiple processes concurrently holding a reference to and occasionally interacting with a single object. (Because the mutex will make sure they don't actually collide)
I mean -- that's just handing the mutex work off to the hardware. It's going to emit something like a lock addl or whatever, which in turn is going to use a hardware signalling mechanism to ensure that nobody else will touch that shared memory region while the instruction completes.
It's certainly more efficient than emulating the functionality with a software mutex, but the underlying point is still to establish exclusive control over a memory region for the duration of the operation.
Because of the way rust works you can't have shared memory of the reference to the mutex itself. Every price of data must have only one owner wrapping the mutex in an arc is an exception since the borrow checking rules are enforced at runtime rather than compile time. That is to say while any thread with a reference to the arc may access that memory, they may only do so as long as there is not a lock on that memory
That gives you a vector of hashmaps, though, not one concurrent hashmap. This imposes extra requirements on ensuring that each shard in the Vec has a predefined and non-overlapping key space, not just an arbitrary String.
Also, having Arc<Vec<T>> means the vector itself is read-only, meaning you can't reorder, delete, or append buckets. You have to know exactly how many shards you want at construction time, no dynamic scaling.
Concurrent hashmaps are one of those things that are basically impossible to make performant in pure safe Rust. Luckily, there are already a number of good crates that already have good unsafe implementations. This benchmark suite suggests that Dashmap is good for write-heavy workloads but is outperformed by Flurry for read-heavy workloads.
Jon Gjengset gave a talk describing his implementation, EVMap, which is a great starting point for understanding where, when, and how to use unsafe code to build concurrency primitives.
sharded_slab is also a good choice if you don't have meaningful keys and just need a concurrent structure to dump objects into for shared thread access by index.
People suggested me, it would take at least 6 months (if you are working) - to build something good (like a db or compiler or something). Heck man, i'm stuck at changing lifetime of a stack variable in passing among multiple threads.
Use crossbeam::scope if you don’t want to promote the lifetime to ’static (e.g. with Arc or Box::leak).
Also I don’t think this guy is telling the truth. Maybe he mistook a sarcastic comment or something, but anyone who says that in r/rust is getting downvoted to hell.
198
u/grg994 Nov 20 '22
Source: Chainsaw Man
Explanation:
This type is basically a Hash Table where
To allow being used concurrently from multiple threads the values are also marked Send and Sync meaning that can - and must be able to - safely shared between threads.
Additionally the Hash Table is wrapped into an atomically reference counted smart pointer (Arc<>) and a Mutex to allow concurrent usage of the whole object
50
u/dthusian Nov 20 '22
C'mon, how are you going to get any performance if reading locks the whole DB? At least change Mutex to RwLock
19
u/ketalicious Nov 20 '22
man rust types are at it again..
9
u/delightful_aug_party Nov 20 '22
Damn
8
u/cchrobo Nov 20 '22
For a second I forgot where I was and thought this was talking about Rust the video game and I was thoroughly confused lmao
3
u/Aschentei Nov 20 '22
What the fuck am I reading, are rust types longer than Java class names now?
8
u/PeksyTiger Nov 20 '22
"Rust doesn't have much of a learning curve"
2
u/yorokobe__shounen Nov 20 '22
Isn't that the database type i am working on?...
2
u/tophatcoder Nov 20 '22
More foreshadowing of her true nature
2
u/[deleted] Nov 21 '22
A synchronized hashmap isn't a real database though.
2
u/berlin_priez Nov 20 '22
Maaaaaaaaaaybe /r/oddlyspecific ?
1
u/Dubmove Dec 23 '22
Arc and mutex?