Skip to content

Instantly share code, notes, and snippets.

@bilus
Last active June 30, 2016 16:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bilus/bb2126d560721dadbfb8 to your computer and use it in GitHub Desktop.
Save bilus/bb2126d560721dadbfb8 to your computer and use it in GitHub Desktop.
Channel pool vs LinkedBlockingQueue
(require '[clojure.core.async :as a])
(defn fill-pool
[pool n]
(doseq [_ (range n)]
(a/>!! pool 6)))
(defmacro with-client-from-pool
[[client-binding pool] & body]
`(let [~client-binding (a/<!! ~pool)]
~@body
(a/>!! ~pool ~client-binding)))
(def my-pool (a/chan 10))
(fill-pool my-pool 10)
(defn run
[x]
(with-client-from-pool [x my-pool]
(Thread/sleep 10)))
(time (dotimes [_ 10]
(doall (pmap run (range 100))))) ; => ~ 1300 msecs
(time (dotimes [_ 2]
(doall (pmap run (range 1000))))) ; => ~2650 msecs
(def q-pool (java.util.concurrent.LinkedBlockingQueue. 10))
(dotimes [_ 10]
(.put q-pool _))
(defmacro with-client-from-q-pool
[[client-binding pool] & body]
`(when-let [~client-binding (.poll ~pool)]
~@body
(.put ~pool ~client-binding)))
(defn q-run
[x]
(with-client-from-q-pool [x q-pool]
(Thread/sleep 10)))
(time (dotimes [_ 10]
(doall (pmap q-run (range 100))))) ; => ~ 460 msecs
(time (dotimes [_ 2]
(doall (pmap q-run (range 1000))))) ; => ~ 7400 msecs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment