2 ms·
Not a bad tradeoff and one I've made in the past. The tricky part here (which the author solved using rcu) is it's REALLY easy introduce a race condition here.
by cogman10 1y ago
Not a bad tradeoff and one I've made in the past.
The tricky part here (which the author solved using rcu) is it's REALLY easy introduce a race condition here.
Imagine thread A and thread B want to add a new value to the hashmap. They arrive at the same time. arc_swap used naively would ultimately have one of those threads win and the value from the other thread disappear. Not great.
But another thing to consider with this approach is that it's expensive under any sort of write contention. As soon as writes start to spike up the amount of allocations increases in a potentially really bad way. If you have 3 threads that want to write at the same time, one thread will create 1 full copy. 1 will create 2 full copies, and the 3rd thread will create 3 full copies. If your underlying data structure has any sort of memory allocated to it, this becomes a pretty bad trade off.
Funnily, one easy way to solve this problem is to introduce locks again, but only for the writer. Possibly a good idea if you are spending more than ~1000 cycles creating the data structure.
- webstrand 1y agoThis is effectively the solution I settled on this week for sharing and renewing an API token with multiple threads. ArcSwap protecting the (Token, Instant), a mutex to prevent multiple simultaneous writers, and an additional barrier readers could optionally wait on, if they need a fresh token before the write is finished.