Skip to content
This repository has been archived by the owner on Apr 7, 2021. It is now read-only.

Optimize pair buffer in DefaultBroadPhaseBuffer #11

Merged
merged 2 commits into from
Sep 24, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions lib/src/collision/broadphase/default_broadphase_buffer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,24 @@ part of box2d;
/// client to consume the new pairs and to track subsequent overlap.
class DefaultBroadPhaseBuffer implements TreeCallback, BroadPhase {
final BroadPhaseStrategy _tree;
final int _mask32Bits = 4294967295; // 2^32-1

int _proxyCount = 0;

List<int> _moveBuffer;
int _moveCapacity = 16;
int _moveCount = 0;

List<Pair> _pairBuffer;
List<int> _pairBuffer;
int _pairCapacity = 16;
int _pairCount = 0;

int _queryProxyId = BroadPhase.NULL_PROXY;

DefaultBroadPhaseBuffer(BroadPhaseStrategy strategy) : _tree = strategy {
_pairBuffer = List<Pair>(_pairCapacity);
_pairBuffer = List<int>(_pairCapacity);
for (int i = 0; i < _pairCapacity; i++) {
_pairBuffer[i] = Pair();
_pairBuffer[i] = 0;
}
_moveBuffer = BufferUtils.intList(_moveCapacity);
}
Expand Down Expand Up @@ -110,18 +111,17 @@ class DefaultBroadPhaseBuffer implements TreeCallback, BroadPhase {
// Send the pairs back to the client.
int i = 0;
while (i < _pairCount) {
Pair primaryPair = _pairBuffer[i];
Object userDataA = _tree.getUserData(primaryPair.proxyIdA);
Object userDataB = _tree.getUserData(primaryPair.proxyIdB);
int primaryPair = _pairBuffer[i];
Object userDataA = _tree.getUserData(primaryPair >> 32);
Object userDataB = _tree.getUserData(primaryPair & _mask32Bits);

callback.addPair(userDataA, userDataB);
++i;

// Skip any duplicate pairs.
while (i < _pairCount) {
Pair pair = _pairBuffer[i];
if (pair.proxyIdA != primaryPair.proxyIdA ||
pair.proxyIdB != primaryPair.proxyIdB) {
int pair = _pairBuffer[i];
if (pair != primaryPair) {
break;
}
++i;
Expand Down Expand Up @@ -178,21 +178,19 @@ class DefaultBroadPhaseBuffer implements TreeCallback, BroadPhase {

// Grow the pair buffer as needed.
if (_pairCount == _pairCapacity) {
List<Pair> oldBuffer = _pairBuffer;
List<int> oldBuffer = _pairBuffer;
_pairCapacity *= 2;
_pairBuffer = List<Pair>(_pairCapacity);
_pairBuffer = List<int>(_pairCapacity);
BufferUtils.arrayCopy(oldBuffer, 0, _pairBuffer, 0, oldBuffer.length);
for (int i = oldBuffer.length; i < _pairCapacity; i++) {
_pairBuffer[i] = Pair();
_pairBuffer[i] = 0;
}
}

if (proxyId < _queryProxyId) {
_pairBuffer[_pairCount].proxyIdA = proxyId;
_pairBuffer[_pairCount].proxyIdB = _queryProxyId;
_pairBuffer[_pairCount] = (proxyId << 32) | _queryProxyId;
} else {
_pairBuffer[_pairCount].proxyIdA = _queryProxyId;
_pairBuffer[_pairCount].proxyIdB = proxyId;
_pairBuffer[_pairCount] = (_queryProxyId << 32) | proxyId;
}

++_pairCount;
Expand Down