xacrimon / dashmap

Blazing fast concurrent HashMap for Rust.
MIT License
2.84k stars 140 forks source link

RFC: support "soft" synchronization for Async Runtime #289

Open thorseraq opened 7 months ago

thorseraq commented 7 months ago

Background

Due to DashMap's usability and great work of dealing with racing condition, I have seen cases when it was used in production scenario, which also includes when async runtime are started (such as Tokio) and CRUD method of DashMap are called in Async Runtime.

Problem

The spin or else park behavior will block the current thread, if it was called in one of the async runtime's thread, the current async runtime thread will be blocked, which is not the best practice. (The task should yield the occupation of the current thread and let the current thread be able to handle other tasks).

image

In the worst case, when all threads of async runtime are blocked due to waiting for some lock, none of the left async tasks are able to be executed.

Recommendations

Add feature flag such as async_runtime, when it was opened, use RwLock struct which can return struct that implements the Future trait when read(), write() is called, such as tokio::sync::RwLock to replace the RwLock in current shards type:

https://github.com/xacrimon/dashmap/blob/febc45dc62eecf3415024bb819e1aec7e4a063a7/src/lib.rs#L90

zRegle commented 7 months ago

I've came across same problem, it would be nice that dashmap can be run in async runtime

esemeniuc commented 7 months ago

Would love to see this!

tomkarw commented 5 months ago

You should check if moka or quick-cache would fit your use-case. They are cache implementations, but you either a) should use cache in the first place, or b) can set the capacity to u64::MAX and let your RAM run out before any evictions would happen.

leontoeides commented 3 months ago

It's worth noting that I'm currently switching back from mini_moka to dashmap. I liked the idea of having my cache automatically managed, and mini-moka is apparently backed by dashmap, but it devastated my software's performance due to repeated calls to std::time::Instant.

xacrimon commented 2 weeks ago

I'm looking into this at the moment. Hopefully I will have a satisfactory solution relatively soon.