Monday, December 15, 2008

a point in haxe: inline ftw!

A while back, I talked about how delineating between structures and objects could have some performance implications.

Since then I have periodically doubted the conclusions I drew from my observations. My results still indicate that for Java with the JVM I used, structuring the code into a point structure and implementing the operations as a collection of static operators does make for a significant performance increase.

What I came to doubt was the universality of this observation.

Specifically, I wanted to see if this design methodology also payed off with haxe or if it was more of a JVM implementation artifact.

To test it, I wrote 3 different implementation of a "point" class:


typedef TypedPoint = {
public var x : Float;
public var y : Float;
public var z : Float;
}
class TypedPointOps {
public inline static function add( dest:TypedPoint, a:TypedPoint, b:TypedPoint ) : TypedPoint {
dest.x = a.x + b.x;
dest.y = a.y + b.y;
dest.z = a.z + b.z;
return dest;
}
}



class Point {
public var x : Float;
public var y : Float;
public var z : Float;

public function new( ?x:Float, ?y:Float, ?z:Float ) {
this.assign( x, y, z );
}

public function assign( ?x:Float, ?y:Float, ?z:Float ) : Point {
this.x = x;
this.y = y;
this.z = z;
return this;
}

public function add( a:Point, b:Point ) : Point {
this.x = a.x + b.x;
this.y = a.y + b.y;
this.z = a.z + b.z;
return this;
}
}



I wrote another version called "InlinePoint" which is just like "Point", but used the "inline" keyword.

Here is the result:




It basicly "adds" two points together 1 million times.

Here is the breakdown:

point : 0.019s
inline : 0.010s
typed : 1.355s


As you can see, the object version using the "inline" keyword is extremely fast compared to the alternatives.

In testing, I used the neato flashplayer command line tool that came as part of the Adobe Flex Builder Linux Public Alpha


% flashplayer pointsDemo.swf


For some reason it shows the "point" time as "0.149s"... weird... The "typed" time was also pretty different... eh...

So... why did I bother? Now I know I can port my JS 3D library straight to haxe without any major refactoring! :-D

No comments:

Post a Comment