Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Â
Dispatch Methods in Clojure
1. Dispatch in Clojure Carlo Sciolla, Product Lead @ Backbase
DISPATCH IN CLOJURE | August 8, 2012 | @skuro
2. (ns aot
(:gen-class))
(defn -main [& args]
(dorun
(map println (seq args))))
javap -c aot.class
A quick journey in function calling
We all learn to divide our code in functions, and invoke them when itâs
their time on the stage of data processing. We deïŹne units of
computations, ready to be executed.
DISPATCH IN CLOJURE | August 8, 2012 | @skuro
3. public static void main(java.lang.String[]);
[..]
4:! invokevirtual!#66;
[..]
26:!invokestatic!
#104;
29:!invokeinterface! #108, 2;
[..]
44:!invokespecial!#115; (defn -main [& args]
[..] (dorun
(map println (seq args))))
The JVM executes our code
Weâll leave it to the JVM to ïŹgure out which code to actually run upon
function call. Itâs not always a straightforward job, and there are several
ways to get to the code.
DISPATCH IN CLOJURE | August 8, 2012 | @skuro
4. public static void main(java.lang.String[]);
[..]
4:! invokevirtual!#66;
[..]
26:!invokestatic!
#104;
29:!invokeinterface! #108, 2;
[..]
44:!invokespecial!#115;
[..]
[clojure.lang.RT] static public ISeq seq(Object coll)
Static dispatch
When thereâs nothing to choose from, the compiler emits a static
dispatch bytecode. All calls will always result in the same code being
executed.
DISPATCH IN CLOJURE | August 8, 2012 | @skuro
5. public static void main(java.lang.String[]);
[..]
4:! invokevirtual!#66;
[..]
26:!invokestatic!
#104;
29:!invokeinterface! #108, 2;
[..]
44:!invokespecial!#115;
[..]
Dynamic dispatch
Most often the compiler canât ïŹgure out the proper method
implementation to call, and the runtime will get its chance to
dynamically dispatch the call.
DISPATCH IN CLOJURE | August 8, 2012 | @skuro
6. * dispatch by arity
* object oriented inheritance
* multimethods
* custom is-a hierarchies
* protocols
The options at hand
Clojure provides a rich interface to dynamic dispatch, allowing
programmers to have control over the dispatch logic at different
degrees to ïŹnd the optimal balance on the performance trade off scale.
DISPATCH IN CLOJURE | August 8, 2012 | @skuro
7. * dispatch by arity
* object oriented inheritance (defn sum-them
* multimethods ([x y] (+ x y))
* custom is-a hierarchies ([x y z] (+ x y z)))
* protocols
The good old arity
Being a dynamically typed language, Clojure only checks on the
number of arguments provided in the function call to ïŹnd the right
implementation to call.
DISPATCH IN CLOJURE | August 8, 2012 | @skuro
8. * dispatch by arity (defn stringify [x]
* object oriented inheritance (.toString x))
* multimethods
* custom is-a hierarchies (stringify (HashMap.)) ; â{}â
* protocols (stringify (HashSet.)) ; â[]â
More than Javaâą
Thanks to Clojure intimacy with Java, object inheritance is easily
achieved. Thanks to Clojure dynamic typing, it also allows functions to
traverse multiple inheritance trees.
DISPATCH IN CLOJURE | August 8, 2012 | @skuro
9. (defn stringify*
[^HashMap x]
(.toString x))
* dispatch by arity
* object oriented inheritance
(stringify* (HashMap.))
* multimethods
=> â{}â
* custom is-a hierarchies
(stringify* (TreeMap.))
* protocols
=> â{}â
(stringify* (HashSet.))
=> ClassCastException
Forcing virtual dispatch to improve performance
Being a dynamic language has a number of beneïŹts, but performance
isnât one of them. To avoid reïŹection calls needed by default by the
dynamic dispatch you can use type hints.
DISPATCH IN CLOJURE | August 8, 2012 | @skuro
10. (defn dispatch-fn
[{:keys [version]}]
* dispatch by arity
version)
* object oriented inheritance
* multimethods
(defmulti multi-call
* custom is-a hierarchies
dispatch-fn)
* protocols
http://bit.ly/multi-tests
Full power
Multimethods allow you to deïŹne your own dispatch strategy as a plain
Clojure function. Their limit is the sky: they perform quite bad, and your
dispatch fn canât be changed or extended in user code.
DISPATCH IN CLOJURE | August 8, 2012 | @skuro
11. (defmethod
multi-call ::custom-type
* dispatch by arity
[x] [...])
* object oriented inheritance
* multimethods
(derive java.util.HashSet
* custom is-a hierarchies
::custom-type)
* protocols
http://bit.ly/multi-tests
Inheritance Ă la carte
Java types hierarchies deïŹned outside your code can be âalteredâ by
custom is-a? relationships created as needed. You can either use the
default hierarchy or use make-hierarchy to restrict its scope.
DISPATCH IN CLOJURE | August 8, 2012 | @skuro
12. (defprotocol Registered
(register [this]))
* dispatch by arity
* object oriented inheritance
(extend-type String
* multimethods
Registered
* custom is-a hierarchies
(register [this] [...]))
* protocols
http://bit.ly/proto-tests
Inheritance Ă la carte
Java types hierarchies deïŹned outside your code can be âalteredâ by
custom is-a? relationships created as needed. You can either use the
default hierarchy or use make-hierarchy to restrict its scope.
DISPATCH IN CLOJURE | August 8, 2012 | @skuro
13. * never used when compiling pure Java
* moves type checking at run time
* the compiler doesnât resolve the method
* user code to perform the dispatch
* eligible for JIT optimizations
http://bit.ly/headius-invokedynamic
Bonus track: invokedynamic
Java7 introduced a new bytecode instruction to help JVM languages
designers: invokedynamic. Thereâs a long standing discussion as which
beneïŹts it can provide to Clojure.
DISPATCH IN CLOJURE | August 8, 2012 | @skuro
15. Thanks!
Amsterdam Clojurians
Carlo Sciolla
Product Lead
http://skuro.tk
@skuro
http://bit.ly/amsclojure
DISPATCH IN CLOJURE | August 8, 2012 | @skuro