GraphNode class and all methods create a lot of garbage

Hi play canvas team, i have a project where have a lot of simple entities(200 and more), they moving, rotating. And i notice sometimes they all freeze when run GC, for my game this is not acceptable, can you reduce places where you engine generate new objects in memory and i think GraphNode is good place for that. This code sample from you repository:

setPosition: function ()
{
// posible move to GraphNode temp variables, create once on class init,
var position = new pc.Vec3();
// posible move to GraphNode temp variables, create once on class init,
var invParentWtm = new pc.Mat4()

        // generate new function(produce garbage), i think this is bad too
        return function (x, y, z) {
            if (x instanceof pc.Vec3) {
                position.copy(x);
            } else {
                position.set(x, y, z);
            }

            if (this._parent === null) {
                this.localPosition.copy(position);
            } else {
                invParentWtm.copy(this._parent.getWorldTransform()).invert();
                invParentWtm.transformPoint(position, this.localPosition);
            }

            if (!this._dirtyLocal)
                this._dirtify(true);
        };

}

Also i see when i enable or disable rigidbody, it also generate garbage in memory, so it is not posible using Pool pattern for rigidbody, when i disable or enable entire Entity.

The setPosition function doesn’t generate any new objects (from the code you have posted). Can you create an example where the GC is called just by moving/rotating a lot of entities please?

var position = new pc.Vec3() and var invParentWtm = new pc.Mat4(); оn setPosition start

setPosition: function () {
var position = new pc.Vec3(); // posible move to GraphNode temp variables, create once on class init,
var invParentWtm = new pc.Mat4(); // posible move to GraphNode temp variables, create once on class init,

The setPosition function is using a closure so position and invParentWtm are only created once during the app lifetime, not every time the function is called.

1 Like

Thanks, you right! Just started learn JavaScript, and get used to new ‘language feature’.