The Subtle Footgun of TVar (Map _ _)

Great post!

There are no situations where going from StmMap.Map k v to TVar (Map k v) will make a significant improvement.

I think there are situations where TVar (Map _ _ ) will be better. If you want to atomically read all the values from the structure, then having a single TVar can lead to better performance.

Reading all the TVars in the StmMap.Map will lead to contention from touching many TVars , but also GHC’s current implementation of STM is quadratic in the amount of TVars used in a transaction. Execution time of reads/writes for TVars during transactions slows down with the number of TVars used during the Transaction. (#24410) · Issues · Glasgow Haskell Compiler / GHC · GitLab . If the map is large, this can be quite expensive!

12 Likes