stuhood a écrit :
> Hey gang,
>
> First time poster!
>
> I figured the best way to explore the interesting parts of Clojure
> (for me: STM, Java interop) would be to implement a simple locking
> problem in Java and Clojure, and pit them against eachother. The
> results are about what I expected, but I'm sure you all can tell me
> how I might make Clojure more competitive.
>
> http://github.com/stuhood/clojure-conc/tree/master
>
> The benchmark contains 4 bi-directional dictionary implementations:
>  * MDict - Java implementation using the synchronized keyword,
>  * RWDict - Java implementation using a ReadWriteLock,
>  * CLJDict - Clojure implementation using the (locking x) macro on
> Java HashMaps,
>  * STMDict - Clojure implementation using two refs on maps.
>   

I'm pretty sure that STMDict doesn't need two refs on maps but one ref 
on two maps (which would allow you to use commute in lieu of alter)
 and since 'this is not hinted in STM-put, STM-getValue and STM-getKey I 
think you have a reflective call for each (.state this).

(defn STM-init []
  ; returns the state of a new STMDict: a ref to two HashMaps
  ; see gen-class docs
  [[] (ref [{} {}])])
  (defn- bidi-assoc [[ktov vtok] k v] [(assoc ktov k v) (assoc vtok v k) ])
(defn STM-put [#^com.mailtrust.dict.STMDict this key value]
    (dosync
      (commute (.state this) bidi-assoc key value)))
 
(defn STM-getValue [#^com.mailtrust.dict.STMDict this key]
    (get (first @(.state this)) key))
 
(defn STM-getKey [#^com.mailtrust.dict.STMDict this value]
    (get (second @(.state this)) value))


(untested code)

Christophe

--~--~---------~--~----~------------~-------~--~----~
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
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to