>> Polyfns are exactly as fast as protocol functions (I am using the same
>> caching and dispatch code), but they do not generate a Java interface,
>> and they are slightly simpler to define.

I think it might be a good idea to discuss why the Java interfaces are
created for protocols.

Before we had protocols, there were only multimethods. Multimethods are
more flexible than protocols, but are naturally fairly slower due to the
fact that every single call requires a lookup in a hashmap. The fastest way
to do single-dispatch on the JVM is via interfaces. Protocols are a mixture
of the two. So the invoke of a protocol fn looks something like this (for
the protocol IFoo):

(defprotocol IFoo
  (bar [obj arg1 arg2]))

(defn bar [obj arg1 arg2]
  (if (instance? IFoo obj)
      (.bar obj arg1 arg2)
      ((get extends (type obj)) arg1 arg 2)))

So if the object implements IFoo then we get the "fast path" dispatch that
the JVM offers. Otherwise we're about as fast as multimethods. This means:


(extend-type String
   IFoo
   (bar [obj arg1 arg2] "string")) ; <--- uses slow path via the hashmap

(deftype Bar []
   IFoo
   (bar [obj arg1 arg2] "Bar")) ; <--- uses fast path via the interface


So I guess I have to ask the question again...what is the true use case of
polyfns? Are they faster than multimethods? If I can get a dramatic speed
up by using interfaces, why would I throw that away?

Timothy

>
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to