Protocol callsites are already optimized in clojure.
Here's the emitted bytecode for a protocol invocation:
0: aload_0
1: aconst_null
2: astore_0
3: dup
4: invokestatic #36 // Method
clojure/lang/Util.classOf:(Ljava/lang/Object;)Ljava/lang/Class;
7: getstatic #38 // Field
__cached_class__0:Ljava/lang/Class;
10: if_acmpeq 27
13: dup
14: instanceof #40 // class test/p
17: ifne 42
20: dup
21: invokestatic #36 // Method
clojure/lang/Util.classOf:(Ljava/lang/Object;)Ljava/lang/Class;
24: putstatic #38 // Field
__cached_class__0:Ljava/lang/Class;
27: getstatic #23 // Field
const__0:Lclojure/lang/Var;
30: invokevirtual #44 // Method
clojure/lang/Var.getRawRoot:()Ljava/lang/Object;
33: swap
34: invokeinterface #49, 2 // InterfaceMethod
clojure/lang/IFn.invoke:(Ljava/lang/Object;)Ljava/lang/Object;
39: goto 47
42: invokeinterface #51, 1 // InterfaceMethod
test/p.f:()Ljava/lang/Object;
47: areturn
Ignoring the class cache, as you can see there's an instanceof check
at offset 14 which results in a direct interface method invocation (at
offset 42) if the type of the target implements directly the interface
which backs the protocol, which is always the case when a deftype
implements a protocol inline rather than via extend.
Nathan Marz writes:
> My understanding is that invocation of protocol methods incurs about 30%
> overhead due to the need to look up the appropriate function for the type.
> I also learned recently that Clojure does not use static type information
> to do the lookup at compile-time and avoid the overhead. Given that Clojure
> already does do some type analysis (to optimize Java method invocations),
> why not do it for protocol invocation as well? Just trying to further my
> understanding of the Clojure compiler.
--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.