Using type hints to optimize protocol invocation

2015-09-08 Thread Nathan Marz
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 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
--- 
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 clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Using type hints to optimize protocol invocation

2015-09-08 Thread Gary Fredericks
I'm not an expert on this subject, but two thoughts come to mind:


   1. the point of protocols is polymorphism, and if I understand you 
   correctly, the case you're describing is narrowed enough that it is *not* 
   polymorphic -- i.e., if the compiler can statically determine what code to 
   run, it's not polymorphic anymore, and you as the programmer could have 
   just called that code directly (though perhaps for some reason you might 
   not want to write it that way)
   2. the previous point notwithstanding, I don't think static type 
   information is enough to pick an implementation, because you could still 
   conceivably encounter (at runtime) subclasses of the annotated type with 
   their own implementations, and have to check for that
   

On Tuesday, September 8, 2015 at 3:59:41 PM UTC-5, Nathan Marz wrote:
>
> 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 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
--- 
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 clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Using type hints to optimize protocol invocation

2015-09-08 Thread Timothy Baldridge
>> 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'd like to see some benchmarks on that. Protocols are actually quite fast,
and if you create the protocol first, then implement a type using that
protocol it should be as fast as any virtual method invocation in Java.

So I would first ask for proof of your first assertion.

Timothy

On Tue, Sep 8, 2015 at 6:11 PM, Gary Fredericks 
wrote:

> I'm not an expert on this subject, but two thoughts come to mind:
>
>
>1. the point of protocols is polymorphism, and if I understand you
>correctly, the case you're describing is narrowed enough that it is
>*not* polymorphic -- i.e., if the compiler can statically determine
>what code to run, it's not polymorphic anymore, and you as the programmer
>could have just called that code directly (though perhaps for some reason
>you might not want to write it that way)
>2. the previous point notwithstanding, I don't think static type
>information is enough to pick an implementation, because you could still
>conceivably encounter (at runtime) subclasses of the annotated type with
>their own implementations, and have to check for that
>
>
> On Tuesday, September 8, 2015 at 3:59:41 PM UTC-5, Nathan Marz wrote:
>>
>> 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 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
> ---
> 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 clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>



-- 
“One of the main causes of the fall of the Roman Empire was that–lacking
zero–they had no way to indicate successful termination of their C
programs.”
(Robert Firth)

-- 
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
--- 
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 clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Using type hints to optimize protocol invocation

2015-09-08 Thread Nicola Mometto

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 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
--- 
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 clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.