[Haskell-cafe] Is there any experience using Software Transactional Memory in substantial applications?

Ketil Malde ketil at malde.org
Wed Aug 11 09:44:52 EDT 2010

Simon Peyton-Jones <simonpj at microsoft.com> writes:

> In contrast, in a pure functional language there are no reads and
> writes, so all the pure part has zero overhead.  Only when you do
> readTVar' and 'writeTVar' do you pay the overhead; these are a tiny
> fraction of all memory accesses.

I'm curious if there are any work done on the scalability of STM.  Or
rather - I expect the answer to be yes, but I'm curious what the results
are :-)

>From my small time experiments, there seems to be a noticeable but
liveable overhead to STM, even without collisions.  This is to be

But there also seem to be scalability issues, and in particular, the
time a transaction takes, appears to scale superlinearly (in fact, more
than O(n²)) with the number of TVars involved.  Is this correct?

(This is a killer for TArrays if you naïvely try to do:

   x <- atomically $ (newListArray (0,n-1) [0..n-1] :: STM (TArray Int Int))
   atomically $ unsafeFreeze x

Instead, I had to do:

   x <- atomically $ (newArray (0,n-1) 0 :: STM (TArray Int Int))
   sequence_ [atomically $ writeArray x i i | i <- [0..n-1]]


   a <- newArray (0,n-1) empty :: IO (IOArray Int Cluster)
   mapM_ (\i -> do v <- atomically $ readArray cmap i
                   writeArray a i v) [0..n-1]
   unsafeFreeze a

After doing this, I measure roughly a factor of two between
(single-threaded) operations on TArrays and STArrays, which I think is
pretty good.  Remains to be seen how it scales with multiple threads,

If I haven't seen further, it is by standing in the footprints of giants

More information about the Haskell-Cafe mailing list