>> But consider swap! is already doing some kind of internal locking at commit >> time as I mentioned before >> I assume even with STM style atom, some kind of lock is happening >> internally, for the final commit, because when committing, >> you still need to coordinate the access to some state with other threads. >> Maybe it is not called a lock, but something similar. >> So that same mechanism can be used to lock the whole swap! and should not >> increase any overhead.
It's not. Locks are created by using CAS, not the other way around. On a x86 machine the swap basically compiles down to a single assembly code instruction: http://jsimlo.sk/docs/cpu/index.php/cmpxchg.html On a normal x86 machine, every lock in the system will boil down to using this single instruction. x86 has no concept of "locks". Locks are simply a construct created by the operating system that is implemented with a series of cmpxchg instructions. This is the reason this instruction exists in the first place. Every type of lock/semephore/mutex we need in a operating system can be built off of this single instruction. This is also why Clojure includes atoms. >> And that "apply" is slow too. Atom's apply uses apply as well: https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/Atom.java Plus, all these tests are worthless without introducing a few threads into the mix: user=> (time (doall (pmap (fn [x] (dotimes [x 1000000] (lock-swap! a (fn [o n] n) x))) (range 10)))) "Elapsed time: 5323.677006 msecs" (nil nil nil nil nil nil nil nil nil nil) user=> (time (doall (pmap (fn [x] (dotimes [x 1000000] (swap! a (fn [o n] n) x))) (range 10)))) "Elapsed time: 708.689155 msecs" (nil nil nil nil nil nil nil nil nil nil) Timothy -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en