Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faster promesa.exec/wrap-bindings #156

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 48 additions & 46 deletions src/promesa/exec.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -179,56 +179,58 @@
@default-scheduler
(pu/maybe-deref scheduler))))

#?(:clj
(defn- binding-conveyor-inner
[f]
(let [frame (clojure.lang.Var/cloneThreadBindingFrame)]
(fn
([]
(clojure.lang.Var/resetThreadBindingFrame frame)
(cond
(instance? clojure.lang.IFn f)
(.invoke ^clojure.lang.IFn f)

(instance? java.lang.Runnable f)
(.run ^java.lang.Runnable f)

(instance? java.util.concurrent.Callable f)
(.call ^java.util.concurrent.Callable f)

:else
(throw (ex-info "Unsupported function type" {:f f :type (type f)}))))
([x]
(clojure.lang.Var/resetThreadBindingFrame frame)
(cond
(instance? clojure.lang.IFn f)
(.invoke ^clojure.lang.IFn f x)

(instance? java.util.function.Function f)
(.apply ^java.util.function.Function f x)

:else
(throw (ex-info "Unsupported function type" {:f f :type (type f)}))))
([x y]
(clojure.lang.Var/resetThreadBindingFrame frame)
(f x y))
([x y z]
(clojure.lang.Var/resetThreadBindingFrame frame)
(f x y z))
([x y z & args]
(clojure.lang.Var/resetThreadBindingFrame frame)
(apply f x y z args))))))

(defn wrap-bindings
{:no-doc true}
;; Passes on local bindings from one thread to another. Compatible with `clojure.lang.IFn`,
;; `java.lang.Runnable`, `java.util.concurrent.Callable`, and `java.util.function.Function`.
;; Adapted from `clojure.core/binding-conveyor-fn`."
[f]
#?(:cljs f
:clj
(let [bindings (get-thread-bindings)]
(fn
([]
(push-thread-bindings bindings)
(try
(f)
(finally
(pop-thread-bindings))))
([a]
(push-thread-bindings bindings)
(try
(f a)
(finally
(pop-thread-bindings))))
([a b]
(push-thread-bindings bindings)
(try
(f a b)
(finally
(pop-thread-bindings))))
([a b c]
(push-thread-bindings bindings)
(try
(f a b c)
(finally
(pop-thread-bindings))))
([a b c d]
(push-thread-bindings bindings)
(try
(f a b c d)
(finally
(pop-thread-bindings))))
([a b c d e]
(push-thread-bindings bindings)
(try
(f a b c d e)
(finally
(pop-thread-bindings))))
([a b c d e & args]
(push-thread-bindings bindings)
(try
(apply f a b c d e args)
(finally
(pop-thread-bindings))))))))

:clj (reify
clojure.lang.IFn
(invoke [_ f] (binding-conveyor-inner f))
java.util.function.Function
(apply [_ f] (binding-conveyor-inner f)))))

#?(:clj
(defn thread-factory?
Expand Down
4 changes: 2 additions & 2 deletions src/promesa/exec/csp.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
promise instance, has the same semantics as `go` macro."
[& body]
`(let [c# (chan :buf 1)
f# (px/wrap-bindings (fn [] ~@body))]
f# (px/wrap-bindings (^{:once true} fn* [] ~@body))]
(->> (p/thread-call *executor* f#)
(p/fnly (fn [v# e#]
(if e#
Expand All @@ -80,7 +80,7 @@
a promise instance."
[& body]
`(let [c# (chan :buf 1)
f# (px/wrap-bindings (fn [] ~@body))]
f# (px/wrap-bindings (^{:once true} fn* [] ~@body))]
(->> (p/thread-call :thread f#)
(p/fnly (fn [v# e#]
(if e#
Expand Down
Loading