Skip to content

Commit f961d78

Browse files
committed
Improve short-circuiting behavior of wrap-future
If the Netty future passed to `wrap-future` is already done, resolve the deferred result immediately. This short-circuits the Netty future listener indirection and all the overhead it involves in such a situation. Note that this optimization was already partially implemented, namely for the success case. This patch merely generelizes it to all "done" cases. Fixes #614.
1 parent be2006c commit f961d78

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

src/aleph/netty.clj

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -240,19 +240,19 @@
240240
(defn wrap-future
241241
[^Future f]
242242
(when f
243-
(if (.isSuccess f)
244-
(d/success-deferred (.getNow f) nil)
245-
(let [d (d/deferred nil)
246-
;; Ensure the same bindings are installed on the Netty thread (vars,
247-
;; classloader) than the thread registering the
248-
;; `operationComplete` callback.
249-
bound-operation-complete (bound-fn* operation-complete)]
250-
(.addListener f
251-
(reify GenericFutureListener
252-
(operationComplete [_ _]
253-
(ensure-dynamic-classloader)
254-
(bound-operation-complete f d))))
255-
d))))
243+
(let [d (d/deferred nil)]
244+
(if (.isDone f)
245+
(operation-complete f d)
246+
(let [;; Ensure the same bindings are installed on the Netty thread (vars,
247+
;; classloader) than the thread registering the
248+
;; `operationComplete` callback.
249+
bound-operation-complete (bound-fn* operation-complete)]
250+
(.addListener f
251+
(reify GenericFutureListener
252+
(operationComplete [_ _]
253+
(ensure-dynamic-classloader)
254+
(bound-operation-complete f d))))))
255+
d)))
256256

257257
(defn allocate [x]
258258
(if (instance? Channel x)

0 commit comments

Comments
 (0)