The algorithm seems mainly memory lookup and copy operations, to futher optimize it, consider precompute length of arrays, instance property this._vertexPositions
, this._indices
, and local vars counters
, positions
, and allocate them as Float32Array, this should reduce allocations by reducing array.push, which can make memory reallocated as array grow.
Preallocate array optimization (~30ms → ~20ms):
Making positions and indices zero copy:
Making uvs preallocated, and eliminate side
, counters
, and some branches:
Making _previousAndSide
and _nextAndCounters
preallocated, ~12ms when after warmup:
Also, since there are not too much calculations here, the algorithm might not benefit too much from compute shaders, as the overhead of copying memory to gpu and the latency of invoking compute shader would also introduced by compute shaders.
BTW, if I understand correctly, this code pushes the same point to positions
twice, if this could be indexed, there might be some performance gains.