« Return to Thread: Thoughts about RichObjects, Implicit convertions and performance

Thoughts about RichObjects, Implicit convertions and performance

by Vladimir Kirichenko-2 :: Rate this Message:

Reply to Author | View in Thread

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



--
Best Regards,
Vladimir Kirichenko



signature.asc (262 bytes) Download Attachment

 « Return to Thread: Thoughts about RichObjects, Implicit convertions and performance