Skip to content

Commit 45c957f

Browse files
committed
fix performance of BatchedMesh removals and insertions
1 parent 4da81fd commit 45c957f

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

src/objects/BatchedMesh.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ class BatchedMesh extends Mesh {
262262
// flags
263263
this._visibilityChanged = true;
264264
this._geometryInitialized = false;
265+
this._availableInstanceIdsSorted = true;
266+
this._availableGeometryIdsSorted = true;
265267

266268
// cached user options
267269
this._maxInstanceCount = maxInstanceCount;
@@ -580,7 +582,12 @@ class BatchedMesh extends Mesh {
580582
// Prioritize using previously freed instance ids
581583
if ( this._availableInstanceIds.length > 0 ) {
582584

583-
this._availableInstanceIds.sort( ascIdSort );
585+
if ( ! this._availableInstanceIdsSorted ) {
586+
587+
this._availableInstanceIds.sort( ascIdSort );
588+
this._availableInstanceIdsSorted = true;
589+
590+
}
584591

585592
drawId = this._availableInstanceIds.shift();
586593
this._instanceInfo[ drawId ] = instanceInfo;
@@ -677,7 +684,12 @@ class BatchedMesh extends Mesh {
677684
let geometryId;
678685
if ( this._availableGeometryIds.length > 0 ) {
679686

680-
this._availableGeometryIds.sort( ascIdSort );
687+
if ( ! this._availableGeometryIdsSorted ) {
688+
689+
this._availableGeometryIds.sort( ascIdSort );
690+
this._availableGeometryIdsSorted = true;
691+
692+
}
681693

682694
geometryId = this._availableGeometryIds.shift();
683695
geometryInfoList[ geometryId ] = geometryInfo;
@@ -846,6 +858,7 @@ class BatchedMesh extends Mesh {
846858

847859
geometryInfoList[ geometryId ].active = false;
848860
this._availableGeometryIds.push( geometryId );
861+
this._availableGeometryIdsSorted = false;
849862
this._visibilityChanged = true;
850863

851864
return this;
@@ -864,6 +877,7 @@ class BatchedMesh extends Mesh {
864877

865878
this._instanceInfo[ instanceId ].active = false;
866879
this._availableInstanceIds.push( instanceId );
880+
this._availableGeometryIdsSorted = false;
867881
this._visibilityChanged = true;
868882

869883
return this;
@@ -1250,7 +1264,13 @@ class BatchedMesh extends Mesh {
12501264
// shrink the available instances as much as possible
12511265
const availableInstanceIds = this._availableInstanceIds;
12521266
const instanceInfo = this._instanceInfo;
1253-
availableInstanceIds.sort( ascIdSort );
1267+
if ( ! this._availableInstanceIdsSorted ) {
1268+
1269+
availableInstanceIds.sort( ascIdSort );
1270+
this._availableInstanceIdsSorted = true;
1271+
1272+
}
1273+
12541274
while ( availableInstanceIds[ availableInstanceIds.length - 1 ] === instanceInfo.length - 1 ) {
12551275

12561276
instanceInfo.pop();
@@ -1445,6 +1465,8 @@ class BatchedMesh extends Mesh {
14451465

14461466
this._availableInstanceIds = source._availableInstanceIds.slice();
14471467
this._availableGeometryIds = source._availableGeometryIds.slice();
1468+
this._availableInstanceIdsSorted = source._availableInstanceIdsSorted;
1469+
this._availableGeometryIdsSorted = source._availableGeometryIdsSorted;
14481470

14491471
this._nextIndexStart = source._nextIndexStart;
14501472
this._nextVertexStart = source._nextVertexStart;

0 commit comments

Comments
 (0)