scala - When is it appropriate to use a TrieMap? -
i have been reading posts , wondering if can present situation on when triemap preferable using hashmap.
so architecture decision should motivate use of triemap?
as per documentation. it's mutable collection can safely used in multithreading applications.
a concurrent hash-trie or triemap
concurrent thread-safe lock-free
implementation of hash array mapped trie. used implement concurrent map abstraction. hasparticularly scalable concurrent
insert , remove operations ,memory-efficient
. supports o(1), atomic, lock-free snapshots used implement linearizable lock-free size, iterator , clear operations. cost of evaluating (lazy) snapshot distributed across subsequent updates, making snapshot evaluation horizontally scalable.
for details, see: http://lampwww.epfl.ch/~prokopec/ctries-snapshot.pdf
also has nice api caching
. example have calculate factorials of different number , re-use results.
object o { val factorialscache = new triemap[int, int]() def factorial(num: int) = ??? // heavy operations def doworkwithfuctorial(num: int) = { val factres = factorialscache.getorelseupdate(num, { // not want invoke factorial(num) // function executed if there no records in map such key }) // start work `withfactres` factres } }
pay attention - function above use global state (cache) write operations, it's absolutely safe use in concurrent threads. you'll not loose data.
Comments
Post a Comment