Skip to content

Commit ed2d36a

Browse files
committed
perf: cache non-growable raytrace list
1 parent bca4df2 commit ed2d36a

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

lib/src/collisions/broadphase.dart

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -656,19 +656,19 @@ class SpatialGridBroadphase extends Broadphase<ShapeHitbox> {
656656

657657
bool get raytraceHitboxUpdated => _raytraceHitboxesUpdated;
658658
Rect? _activeCellRect;
659-
final _raytraceHitboxes = <ShapeHitbox>{};
659+
var _raytraceHitboxesList = <ShapeHitbox>[];
660660
RayTraceMode rayTraceMode = RayTraceMode.groupedHitboxes;
661661

662662
@override
663663
List<ShapeHitbox> get items {
664664
final activeCell = spatialGrid.currentCell;
665+
final raytraceHitboxes = <ShapeHitbox>[];
665666
if (activeCell == null) {
666667
return <ShapeHitbox>[];
667668
}
668669
if (_activeCellRect == activeCell.rect && _raytraceHitboxesUpdated) {
669-
return _raytraceHitboxes.toList(growable: false);
670+
return _raytraceHitboxesList;
670671
} else {
671-
_raytraceHitboxes.clear();
672672
_activeCellRect = activeCell.rect;
673673
final cells = spatialGrid.activeRadiusCells;
674674
for (final cell in cells) {
@@ -689,17 +689,18 @@ class SpatialGridBroadphase extends Broadphase<ShapeHitbox> {
689689
if (hitbox is BoundingHitbox &&
690690
hitbox.optimized &&
691691
hitbox.group != null) {
692-
_raytraceHitboxes.add(hitbox.group!);
692+
raytraceHitboxes.add(hitbox.group!);
693693
continue;
694694
}
695695
}
696-
_raytraceHitboxes.add(hitbox);
696+
raytraceHitboxes.add(hitbox);
697697
}
698698
}
699699
_raytraceHitboxesUpdated = true;
700+
_raytraceHitboxesList = raytraceHitboxes.toList(growable: false);
700701
}
701702

702-
return _raytraceHitboxes.toList(growable: false);
703+
return _raytraceHitboxesList;
703704
}
704705

705706
void dispose() {

0 commit comments

Comments
 (0)