Thoughts about RichObjects, Implicit convertions and performance

View: New views
4 Messages — Rating Filter:   Alert me  
< Prev | 1 - 2 | Next >

Re: Thoughts about RichObjects, Implicit convertions and performance

by Ben Hutchison-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

As David Hall mentioned, a similar idea has been raised before (by David
MacIver, IIRC) and received a fair amount of support, including me.

A search of Nabble and my archives hasn't located it, unfortunately, but
it was worth reading. Can anyone post a ref to it?? As I recall, the
prev. proposal required little syntactic change to effect.

-Ben

Vladimir Kirichenko wrote:

> One of the typical uses of implicit conversions is to declare
> "additional" methods for the existing class. Compiler substitutes
> reference to object with invocation of implicit function that typically
> instantiates some kind of RichObject or anonymous object with additional
> functions. For example
>
> var i = 5;
> i = i max 6;
> i = i max 7;
>
> will be compiled to 2 instantiations of RichInt. The use of anonymous
> declaration in implicit will cause big performance degradation.
>
> Also in these cases usually impossible to reuse created instance.
>
> The idea is to create some kind of "implicit traits" for this kind of
> situations that would not cause performance issues and also eliminates
> the need of implicit def declarations:
>
> implicit[Int] trait RichInt {
> def max(x: Int) = this > x
> def min(x: Int) = this < x
> }
>
> and the
>
> i = i max 6;
>
> will actually be compiled to "static" method invocation
>
> i = RichInt.max(i, 6);
>
> The performance difference is significant
>
> object XT {
>
> implicit def int2x(i: Int) = new {
>     def sum(x: Int) = i + x
> }
>
> def sum(i:Int, x:Int) = i + x;
>
> def main(args : Array[String]) = {
>
>     //simple
>     val iterations = 10000000
>     var start = System.currentTimeMillis
>
>     var i=0;
>     var x=0;
>
>     while(i < iterations) {
>         x = x sum i sum i;
>         i = i + 1;
>     }
>
>     println(x + " in " + (System.currentTimeMillis - start) );
>
>     //fast
>     start = System.currentTimeMillis
>
>     i=0;
>     x=0;
>
>     while(i < iterations) {
>         x = sum(sum(x, i),i);
>         i = i + 1;
>     }
>
>     println(x + " in " + (System.currentTimeMillis - start) );
>
> }
> }
> =============
> 266447232 in 7557
> 266447232 in 20
>
>
>
>  


--

       

*Ben Hutchison
Senior Developer
* Level 2 476 St Kilda Road Melbourne VIC 3004
T 613 8807 5252 | F 613 8807 5203 | M 0423 879 534 |
www.ibsglobalweb.com <http://www.ibsglobalweb.com/>



Re: Thoughts about RichObjects, Implicit convertions and performance

by David Hall-15 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tue, Mar 24, 2009 at 11:50 PM, Ben Hutchison <benh@...> wrote:
> As David Hall mentioned, a similar idea has been raised before (by David
> MacIver, IIRC) and received a fair amount of support, including me.
>
> A search of Nabble and my archives hasn't located it, unfortunately, but it
> was worth reading. Can anyone post a ref to it?? As I recall, the prev.
> proposal required little syntactic change to effect.

Apparently it was Jorge, and apparently the syntax he proposed was the
same one I mentioned above... I could have sworn it was different...

http://www.nabble.com/Implicit-classes-(or-encouraging-people-to-pimp-hygienically)-p18192147.html

The main change I would make to his proposal is to allow implicit
classes to actually perform the conversion when necessary, but to
elide it out whenever possible.

-- David

>
> -Ben
>
> Vladimir Kirichenko wrote:
>>
>> One of the typical uses of implicit conversions is to declare
>> "additional" methods for the existing class. Compiler substitutes
>> reference to object with invocation of implicit function that typically
>> instantiates some kind of RichObject or anonymous object with additional
>> functions. For example
>>
>> var i = 5;
>> i = i max 6;
>> i = i max 7;
>>
>> will be compiled to 2 instantiations of RichInt. The use of anonymous
>> declaration in implicit will cause big performance degradation.
>>
>> Also in these cases usually impossible to reuse created instance.
>>
>> The idea is to create some kind of "implicit traits" for this kind of
>> situations that would not cause performance issues and also eliminates
>> the need of implicit def declarations:
>>
>> implicit[Int] trait RichInt {
>>        def max(x: Int) = this > x
>>        def min(x: Int) = this < x
>> }
>>
>> and the
>>
>> i = i max 6;
>>
>> will actually be compiled to "static" method invocation
>>
>> i = RichInt.max(i, 6);
>>
>> The performance difference is significant
>>
>> object XT {
>>
>> implicit def int2x(i: Int) = new {
>>    def sum(x: Int) = i + x
>> }
>>
>> def sum(i:Int, x:Int) = i + x;
>>
>> def main(args : Array[String]) = {
>>
>>    //simple
>>    val iterations = 10000000
>>    var start = System.currentTimeMillis
>>
>>    var i=0;
>>    var x=0;
>>
>>    while(i < iterations) {
>>        x = x sum i sum i;
>>        i = i + 1;
>>    }
>>
>>    println(x + " in " + (System.currentTimeMillis - start) );
>>
>>    //fast
>>    start = System.currentTimeMillis
>>
>>    i=0;
>>    x=0;
>>
>>    while(i < iterations) {
>>        x = sum(sum(x, i),i);
>>        i = i + 1;
>>    }
>>
>>    println(x + " in " + (System.currentTimeMillis - start) );
>>
>> }
>> }
>> =============
>> 266447232 in 7557
>> 266447232 in 20
>>
>>
>>
>>
>
>
> --
>
>
>
> *Ben Hutchison
> Senior Developer
> * Level 2 476 St Kilda Road Melbourne VIC 3004
> T 613 8807 5252 | F 613 8807 5203 | M 0423 879 534 | www.ibsglobalweb.com
> <http://www.ibsglobalweb.com/>
>
>
>

Re: Thoughts about RichObjects, Implicit convertions and performance

by Jorge Ortiz-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I'd agree with that change, as long as there is an annotation (a la @tailrec) that ensures a conversion can be elided or emits a compiler error.

--j

On Wed, Mar 25, 2009 at 10:59 AM, David Hall <dlwh@...> wrote:
On Tue, Mar 24, 2009 at 11:50 PM, Ben Hutchison <benh@...> wrote:
> As David Hall mentioned, a similar idea has been raised before (by David
> MacIver, IIRC) and received a fair amount of support, including me.
>
> A search of Nabble and my archives hasn't located it, unfortunately, but it
> was worth reading. Can anyone post a ref to it?? As I recall, the prev.
> proposal required little syntactic change to effect.

Apparently it was Jorge, and apparently the syntax he proposed was the
same one I mentioned above... I could have sworn it was different...

http://www.nabble.com/Implicit-classes-(or-encouraging-people-to-pimp-hygienically)-p18192147.html

The main change I would make to his proposal is to allow implicit
classes to actually perform the conversion when necessary, but to
elide it out whenever possible.

-- David

>
> -Ben
>
> Vladimir Kirichenko wrote:
>>
>> One of the typical uses of implicit conversions is to declare
>> "additional" methods for the existing class. Compiler substitutes
>> reference to object with invocation of implicit function that typically
>> instantiates some kind of RichObject or anonymous object with additional
>> functions. For example
>>
>> var i = 5;
>> i = i max 6;
>> i = i max 7;
>>
>> will be compiled to 2 instantiations of RichInt. The use of anonymous
>> declaration in implicit will cause big performance degradation.
>>
>> Also in these cases usually impossible to reuse created instance.
>>
>> The idea is to create some kind of "implicit traits" for this kind of
>> situations that would not cause performance issues and also eliminates
>> the need of implicit def declarations:
>>
>> implicit[Int] trait RichInt {
>>        def max(x: Int) = this > x
>>        def min(x: Int) = this < x
>> }
>>
>> and the
>>
>> i = i max 6;
>>
>> will actually be compiled to "static" method invocation
>>
>> i = RichInt.max(i, 6);
>>
>> The performance difference is significant
>>
>> object XT {
>>
>> implicit def int2x(i: Int) = new {
>>    def sum(x: Int) = i + x
>> }
>>
>> def sum(i:Int, x:Int) = i + x;
>>
>> def main(args : Array[String]) = {
>>
>>    //simple
>>    val iterations = 10000000
>>    var start = System.currentTimeMillis
>>
>>    var i=0;
>>    var x=0;
>>
>>    while(i < iterations) {
>>        x = x sum i sum i;
>>        i = i + 1;
>>    }
>>
>>    println(x + " in " + (System.currentTimeMillis - start) );
>>
>>    //fast
>>    start = System.currentTimeMillis
>>
>>    i=0;
>>    x=0;
>>
>>    while(i < iterations) {
>>        x = sum(sum(x, i),i);
>>        i = i + 1;
>>    }
>>
>>    println(x + " in " + (System.currentTimeMillis - start) );
>>
>> }
>> }
>> =============
>> 266447232 in 7557
>> 266447232 in 20
>>
>>
>>
>>
>
>
> --
>
>
>
> *Ben Hutchison
> Senior Developer
> * Level 2 476 St Kilda Road Melbourne VIC 3004
> T 613 8807 5252 | F 613 8807 5203 | M 0423 879 534 | www.ibsglobalweb.com
> <http://www.ibsglobalweb.com/>
>
>
>


Re: Thoughts about RichObjects, Implicit convertions and performance

by Martin Odersky :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I'm late to the debate...

I think the idea of allowing implicit for classes has a lot of merit.
I am not sure we need to spec
the generation of static methods, though. I see this rather as a
useful a optimization, if the normal scheme is too slow.

Cheers

 -- Martin

On Wed, Mar 25, 2009 at 7:59 PM, David Hall <dlwh@...> wrote:

> On Tue, Mar 24, 2009 at 11:50 PM, Ben Hutchison <benh@...> wrote:
>> As David Hall mentioned, a similar idea has been raised before (by David
>> MacIver, IIRC) and received a fair amount of support, including me.
>>
>> A search of Nabble and my archives hasn't located it, unfortunately, but it
>> was worth reading. Can anyone post a ref to it?? As I recall, the prev.
>> proposal required little syntactic change to effect.
>
> Apparently it was Jorge, and apparently the lot syntax he proposed was the
> same one I mentioned above... I could have sworn it was different...
>
> http://www.nabble.com/Implicit-classes-(or-encouraging-people-to-pimp-hygienically)-p18192147.html
>
> The main change I would make to his proposal is to allow implicit
> classes to actually perform the conversion when necessary, but to
> elide it out whenever possible.
>
> -- David
>
>>
>> -Ben
>>
>> Vladimir Kirichenko wrote:
>>>
>>> One of the typical uses of implicit conversions is to declare
>>> "additional" methods for the existing class. Compiler substitutes
>>> reference to object with invocation of implicit function that typically
>>> instantiates some kind of RichObject or anonymous object with additional
>>> functions. For example
>>>
>>> var i = 5;
>>> i = i max 6;
>>> i = i max 7;
>>>
>>> will be compiled to 2 instantiations of RichInt. The use of anonymous
>>> declaration in implicit will cause big performance degradation.
>>>
>>> Also in these cases usually impossible to reuse created instance.
>>>
>>> The idea is to create some kind of "implicit traits" for this kind of
>>> situations that would not cause performance issues and also eliminates
>>> the need of implicit def declarations:
>>>
>>> implicit[Int] trait RichInt {
>>>        def max(x: Int) = this > x
>>>        def min(x: Int) = this < x
>>> }
>>>
>>> and the
>>>
>>> i = i max 6;
>>>
>>> will actually be compiled to "static" method invocation
>>>
>>> i = RichInt.max(i, 6);
>>>
>>> The performance difference is significant
>>>
>>> object XT {
>>>
>>> implicit def int2x(i: Int) = new {
>>>    def sum(x: Int) = i + x
>>> }
>>>
>>> def sum(i:Int, x:Int) = i + x;
>>>
>>> def main(args : Array[String]) = {
>>>
>>>    //simple
>>>    val iterations = 10000000
>>>    var start = System.currentTimeMillis
>>>
>>>    var i=0;
>>>    var x=0;
>>>
>>>    while(i < iterations) {
>>>        x = x sum i sum i;
>>>        i = i + 1;
>>>    }
>>>
>>>    println(x + " in " + (System.currentTimeMillis - start) );
>>>
>>>    //fast
>>>    start = System.currentTimeMillis
>>>
>>>    i=0;
>>>    x=0;
>>>
>>>    while(i < iterations) {
>>>        x = sum(sum(x, i),i);
>>>        i = i + 1;
>>>    }
>>>
>>>    println(x + " in " + (System.currentTimeMillis - start) );
>>>
>>> }
>>> }
>>> =============
>>> 266447232 in 7557
>>> 266447232 in 20
>>>
>>>
>>>
>>>
>>
>>
>> --
>>
>>
>>
>> *Ben Hutchison
>> Senior Developer
>> * Level 2 476 St Kilda Road Melbourne VIC 3004
>> T 613 8807 5252 | F 613 8807 5203 | M 0423 879 534 | www.ibsglobalweb.com
>> <http://www.ibsglobalweb.com/>
>>
>>
>>
>
< Prev | 1 - 2 | Next >