From fa722c129d5b52806ce5c3a97f8d16abdb9e394d Mon Sep 17 00:00:00 2001 From: Daniel Chappuis Date: Sat, 26 Sep 2020 14:55:22 +0200 Subject: [PATCH] Better optimization to allocate correct amount of memory for potential manifolds and contact points --- .../reactphysics3d/systems/CollisionDetectionSystem.h | 6 ++++++ src/systems/CollisionDetectionSystem.cpp | 10 +++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/include/reactphysics3d/systems/CollisionDetectionSystem.h b/include/reactphysics3d/systems/CollisionDetectionSystem.h index 9431279b..29b21d73 100644 --- a/include/reactphysics3d/systems/CollisionDetectionSystem.h +++ b/include/reactphysics3d/systems/CollisionDetectionSystem.h @@ -162,6 +162,12 @@ class CollisionDetectionSystem { /// Array with the indices of all the contact pairs that have at least one CollisionBody Array mCollisionBodyContactPairsIndices; + /// Number of potential contact manifolds in the previous frame + uint32 mNbPreviousPotentialContactManifolds; + + /// Number of potential contact points in the previous frame + uint32 mNbPreviousPotentialContactPoints; + #ifdef IS_RP3D_PROFILING_ENABLED /// Pointer to the profiler diff --git a/src/systems/CollisionDetectionSystem.cpp b/src/systems/CollisionDetectionSystem.cpp index e80d07df..00e4d78c 100644 --- a/src/systems/CollisionDetectionSystem.cpp +++ b/src/systems/CollisionDetectionSystem.cpp @@ -67,7 +67,8 @@ CollisionDetectionSystem::CollisionDetectionSystem(PhysicsWorld* world, Collider mContactManifolds1(mMemoryManager.getPoolAllocator()), mContactManifolds2(mMemoryManager.getPoolAllocator()), mPreviousContactManifolds(&mContactManifolds1), mCurrentContactManifolds(&mContactManifolds2), mContactPoints1(mMemoryManager.getPoolAllocator()), mContactPoints2(mMemoryManager.getPoolAllocator()), - mPreviousContactPoints(&mContactPoints1), mCurrentContactPoints(&mContactPoints2), mCollisionBodyContactPairsIndices(mMemoryManager.getSingleFrameAllocator()) { + mPreviousContactPoints(&mContactPoints1), mCurrentContactPoints(&mContactPoints2), mCollisionBodyContactPairsIndices(mMemoryManager.getSingleFrameAllocator()), + mNbPreviousPotentialContactManifolds(0), mNbPreviousPotentialContactPoints(0) { #ifdef IS_RP3D_PROFILING_ENABLED @@ -595,8 +596,8 @@ void CollisionDetectionSystem::computeNarrowPhase() { // Swap the previous and current contacts arrays swapPreviousAndCurrentContacts(); - mPotentialContactManifolds.reserve(mPreviousContactManifolds->size()); - mPotentialContactPoints.reserve(mPreviousContactPoints->size()); + mPotentialContactManifolds.reserve(mNbPreviousPotentialContactManifolds); + mPotentialContactPoints.reserve(mNbPreviousPotentialContactPoints); // Test the narrow-phase collision detection on the batches to be tested testNarrowPhaseCollision(mNarrowPhaseInput, true, allocator); @@ -879,6 +880,9 @@ void CollisionDetectionSystem::createContacts() { mPreviousContactManifolds->clear(); mPreviousContactPairs->clear(); + mNbPreviousPotentialContactManifolds = mPotentialContactManifolds.capacity(); + mNbPreviousPotentialContactPoints = mPotentialContactPoints.capacity(); + // Reset the potential contacts mPotentialContactPoints.clear(true); mPotentialContactManifolds.clear(true);