Reduce the number of calls to List::reserve()

This commit is contained in:
Daniel Chappuis 2020-08-30 08:32:49 +02:00
parent 2b362b5098
commit 70d4aed3c1
6 changed files with 22 additions and 10 deletions

View File

@ -49,6 +49,9 @@ struct Islands {
/// Number of islands in the previous frame /// Number of islands in the previous frame
uint32 mNbIslandsPreviousFrame; uint32 mNbIslandsPreviousFrame;
/// Number of items in the bodyEntities array in the previous frame
uint32 mNbBodyEntitiesPreviousFrame;
/// Maximum number of bodies in a single island in the previous frame /// Maximum number of bodies in a single island in the previous frame
uint32 mNbMaxBodiesInIslandPreviousFrame; uint32 mNbMaxBodiesInIslandPreviousFrame;
@ -79,7 +82,7 @@ struct Islands {
/// Constructor /// Constructor
Islands(MemoryAllocator& allocator) Islands(MemoryAllocator& allocator)
:mNbIslandsPreviousFrame(10), mNbMaxBodiesInIslandPreviousFrame(0), mNbMaxBodiesInIslandCurrentFrame(0), :mNbIslandsPreviousFrame(16), mNbBodyEntitiesPreviousFrame(32), mNbMaxBodiesInIslandPreviousFrame(0), mNbMaxBodiesInIslandCurrentFrame(0),
contactManifoldsIndices(allocator), nbContactManifolds(allocator), contactManifoldsIndices(allocator), nbContactManifolds(allocator),
bodyEntities(allocator), startBodyEntitiesIndex(allocator), nbBodiesInIsland(allocator) { bodyEntities(allocator), startBodyEntitiesIndex(allocator), nbBodiesInIsland(allocator) {
@ -133,7 +136,7 @@ struct Islands {
startBodyEntitiesIndex.reserve(mNbIslandsPreviousFrame); startBodyEntitiesIndex.reserve(mNbIslandsPreviousFrame);
nbBodiesInIsland.reserve(mNbIslandsPreviousFrame); nbBodiesInIsland.reserve(mNbIslandsPreviousFrame);
bodyEntities.reserve(mNbMaxBodiesInIslandPreviousFrame); bodyEntities.reserve(mNbBodyEntitiesPreviousFrame);
} }
/// Clear all the islands /// Clear all the islands
@ -145,9 +148,10 @@ struct Islands {
mNbMaxBodiesInIslandCurrentFrame = nbBodiesInIsland[nbIslands-1]; mNbMaxBodiesInIslandCurrentFrame = nbBodiesInIsland[nbIslands-1];
} }
mNbIslandsPreviousFrame = nbContactManifolds.size(); mNbMaxBodiesInIslandPreviousFrame = mNbMaxBodiesInIslandCurrentFrame;
mNbIslandsPreviousFrame = mNbMaxBodiesInIslandCurrentFrame; mNbIslandsPreviousFrame = nbIslands;
mNbMaxBodiesInIslandCurrentFrame = 0; mNbMaxBodiesInIslandCurrentFrame = 0;
mNbBodyEntitiesPreviousFrame = bodyEntities.size();
contactManifoldsIndices.clear(true); contactManifoldsIndices.clear(true);
nbContactManifolds.clear(true); nbContactManifolds.clear(true);

View File

@ -98,6 +98,9 @@ class CollisionDetectionSystem {
/// Broad-phase overlapping pairs /// Broad-phase overlapping pairs
OverlappingPairs mOverlappingPairs; OverlappingPairs mOverlappingPairs;
/// Overlapping nodes during broad-phase computation
List<Pair<int32, int32>> mBroadPhaseOverlappingNodes;
/// Broad-phase system /// Broad-phase system
BroadPhaseSystem mBroadPhaseSystem; BroadPhaseSystem mBroadPhaseSystem;

View File

@ -79,7 +79,8 @@ CollisionCallback::ContactPair::EventType CollisionCallback::ContactPair::getEve
CollisionCallback::CallbackData::CallbackData(List<reactphysics3d::ContactPair>* contactPairs, List<ContactManifold>* manifolds, CollisionCallback::CallbackData::CallbackData(List<reactphysics3d::ContactPair>* contactPairs, List<ContactManifold>* manifolds,
List<reactphysics3d::ContactPoint>* contactPoints, List<reactphysics3d::ContactPair>& lostContactPairs, PhysicsWorld& world) List<reactphysics3d::ContactPoint>* contactPoints, List<reactphysics3d::ContactPair>& lostContactPairs, PhysicsWorld& world)
:mContactPairs(contactPairs), mContactManifolds(manifolds), mContactPoints(contactPoints), mLostContactPairs(lostContactPairs), :mContactPairs(contactPairs), mContactManifolds(manifolds), mContactPoints(contactPoints), mLostContactPairs(lostContactPairs),
mContactPairsIndices(world.mMemoryManager.getHeapAllocator()), mLostContactPairsIndices(world.mMemoryManager.getHeapAllocator()), mWorld(world) { mContactPairsIndices(world.mMemoryManager.getHeapAllocator(), contactPairs->size()), mLostContactPairsIndices(world.mMemoryManager.getHeapAllocator(), lostContactPairs.size()),
mWorld(world) {
// Filter the contact pairs to only keep the contact events (not the overlap/trigger events) // Filter the contact pairs to only keep the contact events (not the overlap/trigger events)
for (uint32 i=0; i < mContactPairs->size(); i++) { for (uint32 i=0; i < mContactPairs->size(); i++) {

View File

@ -50,7 +50,7 @@ bool SphereVsConvexPolyhedronAlgorithm::testCollision(NarrowPhaseInfoBatch& narr
#endif #endif
List<GJKAlgorithm::GJKResult> gjkResults(memoryAllocator); List<GJKAlgorithm::GJKResult> gjkResults(memoryAllocator, batchNbItems);
gjkAlgorithm.testCollision(narrowPhaseInfoBatch, batchStartIndex, batchNbItems, gjkResults); gjkAlgorithm.testCollision(narrowPhaseInfoBatch, batchStartIndex, batchNbItems, gjkResults);
assert(gjkResults.size() == batchNbItems); assert(gjkResults.size() == batchNbItems);

View File

@ -771,7 +771,7 @@ void PhysicsWorld::createIslands() {
Stack<Entity> bodyEntitiesToVisit(mMemoryManager.getSingleFrameAllocator(), mIslands.getNbMaxBodiesInIslandPreviousFrame()); Stack<Entity> bodyEntitiesToVisit(mMemoryManager.getSingleFrameAllocator(), mIslands.getNbMaxBodiesInIslandPreviousFrame());
// List of static bodies added to the current island (used to reset the isAlreadyInIsland variable of static bodies) // List of static bodies added to the current island (used to reset the isAlreadyInIsland variable of static bodies)
List<Entity> staticBodiesAddedToIsland(mMemoryManager.getSingleFrameAllocator()); List<Entity> staticBodiesAddedToIsland(mMemoryManager.getSingleFrameAllocator(), 16);
uint nbTotalManifolds = 0; uint nbTotalManifolds = 0;

View File

@ -57,6 +57,7 @@ CollisionDetectionSystem::CollisionDetectionSystem(PhysicsWorld* world, Collider
mNoCollisionPairs(mMemoryManager.getPoolAllocator()), mNoCollisionPairs(mMemoryManager.getPoolAllocator()),
mOverlappingPairs(mMemoryManager, mCollidersComponents, collisionBodyComponents, rigidBodyComponents, mOverlappingPairs(mMemoryManager, mCollidersComponents, collisionBodyComponents, rigidBodyComponents,
mNoCollisionPairs, mCollisionDispatch), mNoCollisionPairs, mCollisionDispatch),
mBroadPhaseOverlappingNodes(mMemoryManager.getHeapAllocator(), 32),
mBroadPhaseSystem(*this, mCollidersComponents, transformComponents, rigidBodyComponents), mBroadPhaseSystem(*this, mCollidersComponents, transformComponents, rigidBodyComponents),
mMapBroadPhaseIdToColliderEntity(memoryManager.getPoolAllocator()), mMapBroadPhaseIdToColliderEntity(memoryManager.getPoolAllocator()),
mNarrowPhaseInput(mMemoryManager.getSingleFrameAllocator(), mOverlappingPairs), mPotentialContactPoints(mMemoryManager.getSingleFrameAllocator()), mNarrowPhaseInput(mMemoryManager.getSingleFrameAllocator(), mOverlappingPairs), mPotentialContactPoints(mMemoryManager.getSingleFrameAllocator()),
@ -99,17 +100,20 @@ void CollisionDetectionSystem::computeBroadPhase() {
RP3D_PROFILE("CollisionDetectionSystem::computeBroadPhase()", mProfiler); RP3D_PROFILE("CollisionDetectionSystem::computeBroadPhase()", mProfiler);
assert(mBroadPhaseOverlappingNodes.size() == 0);
// Ask the broad-phase to compute all the shapes overlapping with the shapes that // Ask the broad-phase to compute all the shapes overlapping with the shapes that
// have moved or have been added in the last frame. This call can only add new // have moved or have been added in the last frame. This call can only add new
// overlapping pairs in the collision detection. // overlapping pairs in the collision detection.
List<Pair<int32, int32>> overlappingNodes(mMemoryManager.getHeapAllocator(), 32); mBroadPhaseSystem.computeOverlappingPairs(mMemoryManager, mBroadPhaseOverlappingNodes);
mBroadPhaseSystem.computeOverlappingPairs(mMemoryManager, overlappingNodes);
// Create new overlapping pairs if necessary // Create new overlapping pairs if necessary
updateOverlappingPairs(overlappingNodes); updateOverlappingPairs(mBroadPhaseOverlappingNodes);
// Remove non overlapping pairs // Remove non overlapping pairs
removeNonOverlappingPairs(); removeNonOverlappingPairs();
mBroadPhaseOverlappingNodes.clear();
} }
// Remove pairs that are not overlapping anymore // Remove pairs that are not overlapping anymore