Memory management in Haxe


Hi, I am fairly new to Armory3D and the Haxe programming language and have a C#/C++ background.
I have been studying Haxe for a while and I can’t seem to find a feature that behaves like a C# struct. So for example, if I wanted a data structure to represent a point which contains x and y coordinates, that data structure must be instantiated via new, which allocates the object on the heap to be garbage collected later. This probably would not matter were objects allocated infrequently, but in one of the examples (armory_examples-master\material_baked), I found a source code:
function update() {
var mouse = Input.getMouse();
if (mouse.down()) {
object.transform.rotate(new Vec4(0, 0, 1), -mouse.movementX / 100);
which basically instantiates a new Vec4 object every frame for which the mouse button is held. In this case you could make a constant representing the point <0, 0, 1>, but in other cases it could be necessary or less messy to create a point object in the local scope but in a bigger game where there are hundreds of scripts running simultaneously, allocating new memory on the heap every frame for such trivial data structures could be a burden on the hardware. So is there a workaround for this kind of situation that I haven’t found out?
I’m curious because I know there are a few big game engines and frameworks are built with/for Haxe and I think value types are quite an important feature for a language used for game development.


There’s a thing called inline constructors in Haxe. If the rotate function and the Vec4 constructor are “inline” your sample code does not allocate.


It seems that the object’s rotate method isn’t inline according to the doc, and therefore the Vec4 isn’t inlined and is allocated for GC, as I have confirmed by compiling the project to C++ myself.
For performance I could write my own methods to have only primitive parameters, but there are classes such as RigidBody which contains useful–not necessarily inlined–methods that take in these “structs”, which would be hard to optimise unless I write custom methods myself. Anyway, thanks for the reply :slight_smile:


Just add some inlines and send a pull request. Alternatively use Krom - Chakra does escape analysis.


Wouldn’t inlining long methods just because they have class parameter may have negative consequences? I could instead overload methods that replace class parameters with its component parameters, but it seems really cumbersome. I will check out Krom though, thanks for the advice.


Yes, inlining has its downsides.