COMMUNITY

Structure impact on performance

The Haxe manual says that using strucure on a static target has an impact on performance.

So I tried this little example on HL/C to measure it:

typedef Point = {
    var x : Int;
	var y : Int;
}

class Main {
	static function main() {
		var p1:Point = {x:12, y:23};
		var p2:Point = {x:42, y:7};

		var dx = p1.x - p2.x;
		var dy = p1.y - p2.y;

		trace(dx*dx + dy*dy);
	}
}

The hashlink compiled code to C gives this result:

	int r0, r2, r3, r4, r5, r6, r7, r10;
	r0 = 12;
	r2 = 23;
	r3 = 42;
	r4 = 7;
	r5 = r0 - r3;
	r6 = r2 - r4;
	r9 = (haxe__$Log)g$_haxe_Log;
	r8 = r9->trace;
	if( r8 == NULL ) hl_null_access();
	r7 = r5 * r5;
	r10 = r6 * r6;
	r7 = r7 + r10;

I don’t see any performance penalty, or dynamic lookup as stated in the manual.

Did I miss something? Or does my example is too basic? Or does the performance impact is only related to the compilation and not the runtime?

Haxe inlines the object that don’t escape the scope into local variables, so this example will be fast everywhere, you have to pass the object somewhere outside to see the difference.

Generally the performance impact is there because you at least need an extra indirection on field access, which I believe is the case on Hashlink. But that depends on a target implementation, some of them implement anonymous objects similar to string maps which is definitely slower than simple field access on some structure.