[Haskell-cafe] Efficient mutable arrays in STM
ben.franksen at online.de
Mon Oct 31 02:20:12 CET 2011
Benjamin Franksen wrote:
> David Barbour wrote:
>> Create an extra TVar Int for every `chunk` in the array (e.g every 256
>> elements, tuned to your update patterns). Read-write it (increment it, be
>> sure to force evaluation) just before every time you write an element or
>> slice it or slice the array element.
> Incrementing and forcing evaluation should not be necessary, a TVar ()
> should be enough.
I was wrong, though not completely.
> I would be very much surprised if the internal STM
> machinery compares the actual _values_ of what is inside a TVar, I guess
> it just notes that a read or a write happened.
According to the original STM paper the implementation does an equality
test, albeit only for pointer equality. So, one should make sure that
whatever is written to the TVar is a new object. An incremented integer
would probably be ok, (no need to evaluate it, since the closure is newly
allocated, thus a new object), a little more on the safe side is a new TVar
i.e. use TVar (TVar ()).
More information about the Haskell-Cafe