How does Clojure STM differ from Haskell STM?
Asked Answered
C

3

32

I am trying to find the differences between what Clojure calls an STM and what is implemented in Haskell as STM. Taking the actual language semantic differences aside I am a little confused as Rich Hickey says in his speech that Clojure's implementation of STM is very different from anything else out there, but I don't understand the differences apart from the language choice.

Cy answered 30/12, 2010 at 6:47 Comment(0)
M
25

Clojure STM has 3 big unique features:

  1. Implements MVCC snapshot avoiding transactions restarts on read invalidation.
  2. Ensures references on read-writes provides a kind of manual control over resource acquisition order.
  3. Has explicit commute which reduces retries on commutative writes.
Muriel answered 30/12, 2010 at 8:46 Comment(5)
might you also add zero overhead for non-transactional reads to this list?Overhand
It's true, when you use non-transactional context, but if you consider STM and retries on other transactions, I don't have sure if hole system suffer on performance caused by context swap.But JVM is very mature and STM is like a GC...very necessary, so you should think in that wayMuriel
Hi william. reading the Mark volkmann article and presentation , he said "reads only trigger a retry in Clojure when the history list of a Refdoesn’t contain a value committed before the txn began" . The first feature described above is correct ?Conjugated
@Conjugated correct, reducing is a better word than avoiding. But generally only in special cases (slow reads with fast frequent writes) will this even come in to play.Cornish
Clojure's STM suffers one particular problem that is well-known and unresolved, where long-running transactions may never complete if they are pre-empted by shorter transactions. I'm not sure if Haskell has a similar problem.Stroke
P
16

For Haskell STM, see SPJ's papers: http://research.microsoft.com/en-us/um/people/simonpj/papers/stm/

Of particular use are "Composable memory transactions" and "Transactional memory with data invariants". GHC's implementation of STM indeed isn't MVCC. I don't recall all the implementation details, but my understanding is that the description in the papers isn't all that different from what currently exists in GHC.

(note that MVCC, in clojure or elsewhere, makes possible write-skew -- see, e.g., here: http://en.wikipedia.org/wiki/Snapshot_isolation)

Philander answered 31/12, 2010 at 2:23 Comment(2)
Also noteworthy is that GHC's implementation provides compile-time guarantees of the safety of a transaction with respect to side effects; and the unique orElse combinator for atomically composing transactions.Mayenne
@DonStewart In addition Haskell STM is a MonadPlus, which allows you to specify additional failure conditions within the transaction context. i.e. if final account balance is negative, fail/retry the txn. It also allows you to share that logic with other MonadPlus classes easily; the same function would show the result of all potential valid transactions given a List of accounts and purchases. This can be useful if the logic is complex. I don't think either is possible in clojure.Cornish
K
12

Mark Volkmann did a very detailed presentation on STMs in general (and Clojure's STM in particular) at Strange Loop 2009 which you can find here (article and slides here). I don't really know of any other resource (other than the code) for understanding how Clojure's STM works.

Kriemhild answered 30/12, 2010 at 15:39 Comment(4)
The presentation link doesn't work. Anyone have an alternative to this talk?Recaption
The presentation slides and a detailed article on the topic are available here java.ociweb.com/mark/stm/article.htmlProthesis
Such a good read. Very accessible. Mr. Volkmann is a gifted writer.Turbojet
Is there a recording of Mr. Volkmann's talk available somewhere?Unbend

© 2022 - 2024 — McMap. All rights reserved.