Thanks Tassilo,

your suggestion of fully qualifying the protocol worked like a charm! I managed to eliminate my last 2 reflective calls on the whole namespace... this is good stuff!

Jim


On 12/06/12 12:57, Jim - FooBar(); wrote:
aaa ok I see...

however I only want to do this to avoid a specific reflection call...you see my move function take either any type that satisfies IPiece and so at each call reflection is needed to decide which one to use...I'd like to say in the argument that what is coming is a IPiece so stop doing reflection and dispatch directly to IPiece who will then decide between the correct implementation based on the first argument only...

am I missing something? Is this not correct? I want to keep 'move' as tight as possible - that's all!

Jim

ps: thanks for the detailed answer :-)


On 12/06/12 12:50, Tassilo Horn wrote:
"Jim - FooBar();"<jimpil1...@gmail.com>  writes:

why can't I use the interface as function argument instead of the
concrete class (record)?

example: (defprotocol IPiece
                      blah blah blah)

(defrecord ChessPiece
IPiece
  blah blah blah)

(defrecord CheckersPiece
IPiece
  blah blah blah)

(defn move [^IPiece p] ;will complain that it can't resolve type IPiece
blah blah blah)
The interface will reside in the current namespace, so you need to
qualify it.

--8<---------------cut here---------------start------------->8---
user>  (defprotocol IPiece
      (frobnicate [this]))
IPiece
user>  IPiece
{:on-interface user.IPiece, :on user.IPiece, :sigs {:frobnicate {:doc nil, :arglists ([this]), :name frobnicate}}, :var #'user/IPiece, :method-map {:frobnicate :frobnicate}, :method-builders {#'user/frobnicate #<user$eval2292$fn__2293 user$eval2292$fn__2293@62e6615f>}}
user>  user.IPiece
user.IPiece
user>  (class user.IPiece)
java.lang.Class
user>  (.isInterface user.IPiece)
true
--8<---------------cut here---------------end--------------->8---

However, that there is an interface is more or less an implementation
detail.  Protocol methods are dispatched dynamically and quickly enough,
so there's no benefit in type-hinting with generated interfaces.  Too
prove that:

--8<---------------cut here---------------start------------->8---
user>  (defrecord Frob [x]
    IPiece
    (frobnicate [this] x))
user.Frob
user>  (defn frob1 [p] (frobnicate p))
#'user/frob1
user>  (defn frob2 [^user.IPiece p] (frobnicate p))
#'user/frob2
user>  (use 'criterium.core)
nil
user>  (let [f (->Frob 10)]
       (bench (frob1 f) :verbose)
       (bench (frob2 f) :verbose))
amd64 Linux 3.4.0-gentoo 2 cpu(s)
OpenJDK 64-Bit Server VM 22.0-b10
Runtime arguments: -agentlib:jdwp=transport=dt_socket,server=y,suspend=n -XX:+TieredCompilation -Xmx1G -Dclojure.compile.path=/home/horn/Repos/clj/testi/target/classes -Dtesti.version=0.1.0-SNAPSHOT -Dclojure.debug=false
Evaluation count             : 8495040
Execution time mean : 6.474061 us 95.0% CI: (6.473442 us, 6.474699 us) Execution time std-deviation : 71.405519 ns 95.0% CI: (70.680926 ns, 72.202375 ns) Execution time lower ci : 6.390741 us 95.0% CI: (6.390741 us, 6.390741 us) Execution time upper ci : 6.595243 us 95.0% CI: (6.594962 us, 6.595243 us)

Found 1 outliers in 60 samples (1.6667 %)
    low-severe     1 (1.6667 %)
Variance from outliers : 1.6389 % Variance is slightly inflated by outliers
amd64 Linux 3.4.0-gentoo 2 cpu(s)
OpenJDK 64-Bit Server VM 22.0-b10
Runtime arguments: -agentlib:jdwp=transport=dt_socket,server=y,suspend=n -XX:+TieredCompilation -Xmx1G -Dclojure.compile.path=/home/horn/Repos/clj/testi/target/classes -Dtesti.version=0.1.0-SNAPSHOT -Dclojure.debug=false
Evaluation count             : 9339360
Execution time mean : 6.482568 us 95.0% CI: (6.481979 us, 6.483191 us) Execution time std-deviation : 82.858647 ns 95.0% CI: (82.277583 ns, 83.438706 ns) Execution time lower ci : 6.409230 us 95.0% CI: (6.409230 us, 6.409230 us) Execution time upper ci : 6.648313 us 95.0% CI: (6.646894 us, 6.648313 us)

Found 3 outliers in 60 samples (5.0000 %)
    low-severe     3 (5.0000 %)
Variance from outliers : 1.6389 % Variance is slightly inflated by outliers
nil
--8<---------------cut here---------------end--------------->8---

Here you can see that calls to the hinted `frob2` are not faster than
calls to the non-hinted `frob1`.  So don't do that.

Bye,
Tassilo



--
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