TMap vs immutable Map in a Ref

43 views
Skip to first unread message

Kevin Esler

unread,
Jun 10, 2013, 11:14:27 AM6/10/13
to scala-stm-e...@googlegroups.com
I'm new to STM so go easy on the newbie. I have an Akka app in which various actors need to share a cache so STM looks like the thing to use.

The cache has the semantics of a Scala Map. I see I could use TMap, but it seems like I could also just store the cache as an immutable Map inside a Ref.

What are the pros and cons of these two alternatives?


Nathan Bronson

unread,
Jun 10, 2013, 1:15:38 PM6/10/13
to scala-stm-e...@googlegroups.com
Kevin,

Both approaches will have the right behavior, but TMap will give you more concurrency. The STM checks whether transactions can safely run in parallel at the granularity of a Ref. Ref[Map] has only a single ref, so if any thread writes to the map then all of the other transactions will have to wait to read. TMap a hash trie built of Ref-s, so transactions can simultaneously write different leaves without colliding.

 - Nathan


--
 
---
You received this message because you are subscribed to the Google Groups "Scala STM Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-stm-expert-...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Nathan Grasso Bronson
ngbr...@gmail.com

Kevin Esler

unread,
Jun 10, 2013, 3:34:15 PM6/10/13
to scala-stm-e...@googlegroups.com
Thanks. That makes perfect sense.


On Monday, June 10, 2013 1:15:38 PM UTC-4, Nathan Bronson wrote:
Kevin,

Both approaches will have the right behavior, but TMap will give you more concurrency. The STM checks whether transactions can safely run in parallel at the granularity of a Ref. Ref[Map] has only a single ref, so if any thread writes to the map then all of the other transactions will have to wait to read. TMap a hash trie built of Ref-s, so transactions can simultaneously write different leaves without colliding.

 - Nathan
On Mon, Jun 10, 2013 at 8:14 AM, Kevin Esler <kevin...@gmail.com> wrote:
I'm new to STM so go easy on the newbie. I have an Akka app in which various actors need to share a cache so STM looks like the thing to use.

The cache has the semantics of a Scala Map. I see I could use TMap, but it seems like I could also just store the cache as an immutable Map inside a Ref.

What are the pros and cons of these two alternatives?


--
 
---
You received this message because you are subscribed to the Google Groups "Scala STM Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-stm-expert-group+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 
Reply all
Reply to author
Forward
0 new messages