Couldn't the pattern of interacting with a Mutex<HashMap<T>> be made much faster?
One of the most common patterns in my Axum webserver for my application is something like this:
let mut users = self.users.lock().unwrap();
if let Some(user) = users.get(&user_id) {
user.points += 5;
}
A trivial example, but the basic idea is that most of my route handlers involve locking some hashmap, interacting with it in some way (reading or updating some entries), and then releasing the lock.
But, couldn't this be faster?
Since you want to make the "critical section" where the lock is held as short as possible, wouldn't it be more efficient if we could hash the key beforehand? Something like this (pseudocode):
let key = HashMap::hash(user_id);
let mut users = self.users.lock().unwrap();
if let Some(user) = users.get_with_key(&key) {
user.points += 5;
}
That way you avoid having to do the hashing operation while the lock is held, so the critical region is shorter.
Is there anything like this in the ecosystem? I'm not sure how to make it ergonomic or how the API would work, but does the idea make sense at least?