changeset 162:4aa8979938ee

Faster dynamic bindings.
author Mikhail Kryshen <mikhail@kryshen.net>
date Mon, 24 Nov 2014 02:09:13 +0300
parents acda6344bcb7
children dc3ed475c6d6
files src/indyvon/core.clj
diffstat 1 files changed, 30 insertions(+), 12 deletions(-) [+]
line diff
     1.1 --- a/src/indyvon/core.clj	Sat Nov 22 03:49:31 2014 +0300
     1.2 +++ b/src/indyvon/core.clj	Mon Nov 24 02:09:13 2014 +0300
     1.3 @@ -338,6 +338,24 @@
     1.4         (swap! (:next-state scene) assoc handle state)
     1.5         (update scene))))
     1.6  
     1.7 +(defmacro binding-fast
     1.8 +  "Faster alternative to core/binding. Works only with vars that are
     1.9 +  already thread-bound. Uses set! instead of push-thread-bindings and
    1.10 +  pop-thread-bindings."
    1.11 +  [bindings & body]
    1.12 +  {:pre [(vector? bindings)
    1.13 +         (even? (count bindings))]}
    1.14 +  (let [bindings (partition 2 bindings)
    1.15 +        var-syms (map first bindings)
    1.16 +        var-vals (map second bindings)
    1.17 +        syms (map (comp gensym name) var-syms)]
    1.18 +    `(let [~@(mapcat vector syms var-syms)]
    1.19 +       (try
    1.20 +         ~@(map #(list `set! %1 %2) var-syms var-vals)
    1.21 +         ~@body
    1.22 +         (finally
    1.23 +           ~@(map #(list `set! %1 %2) var-syms syms))))))
    1.24 +
    1.25  ;;
    1.26  ;; Rendering
    1.27  ;;
    1.28 @@ -446,10 +464,10 @@
    1.29    (let [graphics (create-graphics)]
    1.30      (try
    1.31        (.translate graphics (double x) (double y))
    1.32 -      (binding [*width* w
    1.33 -                *height* h
    1.34 -                *input-clip* (Rectangle2D$Double. 0.0 0.0 w h)
    1.35 -                *graphics* graphics]
    1.36 +      (binding-fast [*width* w
    1.37 +                     *height* h
    1.38 +                     *input-clip* (Rectangle2D$Double. 0.0 0.0 w h)
    1.39 +                     *graphics* graphics]
    1.40          (apply f args))
    1.41        (finally
    1.42         (.dispose graphics)))))
    1.43 @@ -464,11 +482,11 @@
    1.44          (try
    1.45            (.clip graphics bounds)
    1.46            (.translate graphics x y)
    1.47 -          (binding [*width* w
    1.48 -                    *height* h
    1.49 -                    *clip* clip
    1.50 -                    *input-clip* nil
    1.51 -                    *graphics* graphics]
    1.52 +          (binding-fast [*width* w
    1.53 +                         *height* h
    1.54 +                         *clip* clip
    1.55 +                         *input-clip* nil
    1.56 +                         *graphics* graphics]
    1.57              (apply f args))
    1.58            (finally
    1.59             (.dispose graphics)))))))
    1.60 @@ -565,7 +583,7 @@
    1.61    ([view]
    1.62       (let [graphics (create-graphics)]
    1.63         (try
    1.64 -         (binding [*graphics* graphics]
    1.65 +         (binding-fast [*graphics* graphics]
    1.66             (render! view))
    1.67           (finally
    1.68            (.dispose graphics)))))
    1.69 @@ -602,8 +620,8 @@
    1.70  
    1.71  (defn with-handlers*
    1.72    [handle handlers f & args]
    1.73 -  (binding [*event-dispatcher* (create-dispatcher
    1.74 -                                *event-dispatcher* handle handlers)]
    1.75 +  (binding-fast [*event-dispatcher* (create-dispatcher
    1.76 +                                     *event-dispatcher* handle handlers)]
    1.77      (apply f args)))
    1.78  
    1.79  (defmacro with-handlers