How to reduce GC GeneratePointsArray after geometry update?

I have to edit a mesh (updating geometry Normals, and Vertex) afterward the geometries cached position array is of course empty.

Is a better way to get Picking info? Or to not clear the position cache?

image

Update the mesh variant 1

const vertexData = new VertexData();
vertexData.positions = positions;
vertexData.indices = mesh.geometry.getIndices(); // did not change
vertexData.normals = normals;
vertexData.applyToMesh(mesh, true);

Variant 2:

// buffer marked for update
mesh.geometry.updateVerticesDataDirectly(VertexBuffer.PositionKind, positions, 0);
mesh.geometry.updateVerticesDataDirectly(VertexBuffer.NormalKind, normals, 0);

Edit:
I also found this: "Free" performance improvement: preallocate arrays (when possible) throughout Babylon.js's codebase but could not find the PR or progress.

Variant 2 should not trigger any GC as there is no array creation: it simply takes your data and update the buffer directly.

_generatePointsArray is called only if the internal _positions is not constructed yet, and it should normally be constructed once except if you change the vertex array (as in variant 1).

i do, sry. did not append that code. I copied it from the _gerneratePointsArray

Strange, _generatePointsArray is this for me:

public _generatePointsArray(): boolean {
    if (this._positions) {
        return true;
    }

    var data = this.getVerticesData(VertexBuffer.PositionKind);

    if (!data || data.length === 0) {
        return false;
    }

    this._positions = [];

    for (var index = 0; index < data.length; index += 3) {
        this._positions.push(Vector3.FromArray(data, index));
    }

    return true;
}

Anyway, you can use updateVerticesDataDirectly to avoid _generatePointsArray being called when updating the positions (of course, the collision code may not work as expected anymore as it will not used the updated position data…).

Let’s see if the post you linked to about the PR will get some attention.

Exactly, in fact I need the _positions to be able to pick on the mesh. Therefore I use VertexData.apply.. or have to add the _positions my self…

I think that was not clear in the first post: I have to Pick on the updated mesh.