Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single atom vs multiple refs

What are tradeoffs of representing state using a single atom and a hashmap vs multiple refs?

For example:

(def start (atom {:location "Chicago" :employer "John"}))

vs

(def location (ref "Chicago"))
(def employer (ref "John"))

Many thanks

like image 303
user2936410 Avatar asked Mar 26 '26 23:03

user2936410


1 Answers

The single atom version is better and has less tradeoffs. Given that you don't want to change the employer and the location uncoordinated, your win is that you don't have to create a dosync block to change either location or employer or both. Using the atom, you can simply (swap! start assoc :location "baz").

A big tradeoff of using multiple refs is that all transactions to refs will be tried in parallel and the first one who is ready wins, the others will be restarted. While that is also true for atoms, having more refs for all entries requires more monitoring, grouping (for dosync blocks) etc. behind the scenes. To have less restarts, it makes sense to group the information in a hash-map. Depending on whether coordinated change is required, put it in a ref or an atom.

like image 51
Leon Grapenthin Avatar answered Mar 29 '26 16:03

Leon Grapenthin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!