From e526e48df837d282f4a59554750769b21bbc2104 Mon Sep 17 00:00:00 2001 From: Lunkhound Date: Tue, 13 Mar 2018 04:19:02 -0700 Subject: [PATCH] parallel solver: slightly overallocate to reduce how often allocation is needed --- .../ConstraintSolver/btBatchedConstraints.cpp | 6 ++++++ .../btSequentialImpulseConstraintSolverMt.cpp | 20 +++++++++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/BulletDynamics/ConstraintSolver/btBatchedConstraints.cpp b/src/BulletDynamics/ConstraintSolver/btBatchedConstraints.cpp index 310601659..84a00dc63 100644 --- a/src/BulletDynamics/ConstraintSolver/btBatchedConstraints.cpp +++ b/src/BulletDynamics/ConstraintSolver/btBatchedConstraints.cpp @@ -892,6 +892,12 @@ static void setupSpatialGridBatchesMt( memHelper.addChunk( (void**) &constraintBatchIds, sizeof( int ) * numConstraints ); memHelper.addChunk( (void**) &constraintRowBatchIds, sizeof( int ) * numConstraintRows ); size_t scratchSize = memHelper.getSizeToAllocate(); + // if we need to reallocate + if (scratchMemory->capacity() < scratchSize) + { + // allocate 6.25% extra to avoid repeated reallocs + scratchMemory->reserve( scratchSize + scratchSize/16 ); + } scratchMemory->resizeNoInitialize( scratchSize ); char* memPtr = &scratchMemory->at(0); memHelper.setChunkPointers( memPtr ); diff --git a/src/BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolverMt.cpp b/src/BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolverMt.cpp index b9ad17a03..4ccf7b247 100644 --- a/src/BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolverMt.cpp +++ b/src/BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolverMt.cpp @@ -568,10 +568,22 @@ void btSequentialImpulseConstraintSolverMt::allocAllContactConstraints(btPersist } } } - m_tmpSolverContactConstraintPool.resizeNoInitialize(numContacts); - m_rollingFrictionIndexTable.resizeNoInitialize(numContacts); - m_tmpSolverContactFrictionConstraintPool.resizeNoInitialize(numContacts*m_numFrictionDirections); - m_tmpSolverContactRollingFrictionConstraintPool.resizeNoInitialize(numRollingFrictionConstraints); + { + BT_PROFILE( "allocPools" ); + if ( m_tmpSolverContactConstraintPool.capacity() < numContacts ) + { + // if we need to reallocate, reserve some extra so we don't have to reallocate again next frame + int extraReserve = numContacts / 16; + m_tmpSolverContactConstraintPool.reserve( numContacts + extraReserve ); + m_rollingFrictionIndexTable.reserve( numContacts + extraReserve ); + m_tmpSolverContactFrictionConstraintPool.reserve( ( numContacts + extraReserve )*m_numFrictionDirections ); + m_tmpSolverContactRollingFrictionConstraintPool.reserve( numRollingFrictionConstraints + extraReserve ); + } + m_tmpSolverContactConstraintPool.resizeNoInitialize( numContacts ); + m_rollingFrictionIndexTable.resizeNoInitialize( numContacts ); + m_tmpSolverContactFrictionConstraintPool.resizeNoInitialize( numContacts*m_numFrictionDirections ); + m_tmpSolverContactRollingFrictionConstraintPool.resizeNoInitialize( numRollingFrictionConstraints ); + } } { AllocContactConstraintsLoop loop(this, &cachedInfoArray[0]);