100s of thousands of RwLocks

I use a Map where the value needs to be protected on access. In particular I use crossbeam SkipMap. However I could have potentially 100s of thousands of items in the map. My question is would the following definition make sense:

let map : SkipMap<String, RwLock<MyDataType>> = SkipMap::new();

Things work but I am suspicious about using 100s of thousands of RwLocks. For instance in Java or in C++ I would have an abstraction that pools say N RwLocks, from which I could get one lock via a hash scheme to protect access. Is there any way to achieve that in Rust? Is it ok to have 100s of thousands of lock instances?

1 Like

The answer is, unfortunately, platform-specific.

On Linux passive RwLock is just a couple of integers and allocating even millions of them is cheap and fast.

On some other platforms they may be expensive and slow.

parking_lot's RwLock may be more suitable for you. The "parking lot" abstraction was designed specifically to make it relatively cheap to have a lot of locks without relying on platform specific implementation details.

2 Likes

implement now. optimize later :upside_down_face:

But if you want my opinion, concurrent access to a hashmap is not a good idea and you should design your system differently

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.