SubMesh: pre-allocate buffers for _getLinesIndexBuffer

Version: 7.42.0

Background

Currently in _getLinesIndexBuffer a linesIndices buffer is initialized as array, which size grows dynamically in the loop after it, which could lead to memory reallocations and more gc pressures. Also, uses array to store numbers would result in at least a 8-bytes double pre spec, and likely 24-bytes “heap number” in some chrome v8.

Code:

Proposal

Pre-allocate buffers as TypedArray, from the loop below we can know that the size of linesIndices should be exactly 2x the size of this.indexCount (aligned down to 3 elements).
This can also eliminate the overhead of scanning the buffer in engine._normalizeIndexData.

Feel free to make a PR for it

Hmmm, since iterating the indices buffer is a waste of performance, it it good to determine whether to use u16 or u32 by this.verticesStart + this.verticesCount?

1 Like
1 Like

Thank you !!!