simplify manifold allocation

This commit is contained in:
Erin Catto 2026-11-17 23:01:38 -08:00
commit f36bcdaccc
3 changed files with 15 additions and 33 deletions

View file

@ -1830,10 +1830,10 @@ bool b3ComputeMeshManifolds( b3World* world, int workerIndex, b3Contact* contact
// Make a temporary copy of previous manifolds
int oldManifoldCount = contact->manifoldCount;
b3Manifold* oldManifolds = NULL;
B3_ASSERT( oldManifoldCount <= B3_MAX_MANIFOLDS );
b3Manifold oldManifolds[B3_MAX_MANIFOLDS];
if ( oldManifoldCount > 0 )
{
oldManifolds = b3Bump( &arena, oldManifoldCount * sizeof( b3Manifold ) );
memcpy( oldManifolds, contact->manifolds, oldManifoldCount * sizeof( b3Manifold ) );
}
@ -1850,12 +1850,7 @@ bool b3ComputeMeshManifolds( b3World* world, int workerIndex, b3Contact* contact
memset( contact->manifolds, 0, contact->manifoldCount * sizeof( b3Manifold ) );
}
bool* consumed = NULL;
if ( oldManifoldCount > 0 )
{
consumed = b3Bump( &arena, oldManifoldCount * sizeof( bool ) );
memset( consumed, 0, oldManifoldCount * sizeof( bool ) );
}
bool consumed[B3_MAX_MANIFOLDS] = { 0 };
b3Matrix3 matrixB = b3MakeMatrixFromQuat( xfB.q );
b3Vec3 offsetA = b3SubPos( xfB.p, xfA.p );

View file

@ -253,7 +253,10 @@ b3WorldId b3CreateWorld( const b3WorldDef* def )
world->stack = b3CreateStack( 2048 );
b3Array_Reserve( world->manifoldAllocators, 16 );
for ( int i = 0; i < B3_MAX_MANIFOLDS; ++i )
{
world->manifoldAllocators[i] = b3CreateBlockAllocator( ( i + 1 ) * (int)sizeof( b3Manifold ), 0 );
}
world->manifoldAllocatorMutex = b3CreateMutex();
b3CreateBroadPhase( &world->broadPhase, &def->capacity );
@ -540,11 +543,10 @@ void b3DestroyWorld( b3WorldId worldId )
b3DestroyIdPool( &world->islandIdPool );
b3DestroyIdPool( &world->solverSetIdPool );
for ( int i = 0; i < world->manifoldAllocators.count; ++i )
for ( int i = 0; i < B3_MAX_MANIFOLDS; ++i )
{
b3DestroyBlockAllocator( world->manifoldAllocators.data + i );
b3DestroyBlockAllocator( world->manifoldAllocators + i );
}
b3Array_Destroy( world->manifoldAllocators );
b3DestroyMutex( world->manifoldAllocatorMutex );
b3DestroyStack( &world->stack );
@ -2846,20 +2848,18 @@ static uint64_t b3GetWorldByteCount( b3World* world, bool log )
}
// Manifold block allocators, one per manifold point count
int manifoldArrayBytes = b3Array_ByteCount( world->manifoldAllocators );
int manifoldBlockBytes = 0;
for ( int i = 0; i < world->manifoldAllocators.count; ++i )
for ( int i = 0; i < B3_MAX_MANIFOLDS; ++i )
{
b3BlockAllocator* allocator = world->manifoldAllocators.data + i;
b3BlockAllocator* allocator = world->manifoldAllocators + i;
manifoldBlockBytes += b3Array_ByteCount( allocator->blocks );
manifoldBlockBytes += allocator->blocks.count * B3_BLOCK_SIZE * allocator->elementSize;
}
total += (uint64_t)manifoldArrayBytes + manifoldBlockBytes;
total += (uint64_t)manifoldBlockBytes;
if ( log )
{
b3Log( "manifold allocators" );
b3Log( "allocator array: %d", manifoldArrayBytes );
b3Log( "blocks: %d", manifoldBlockBytes );
}

View file

@ -131,7 +131,7 @@ typedef struct b3World
b3ConstraintGraph constraintGraph;
// Manifold allocators have one allocator for each manifold count.
b3Array( b3BlockAllocator ) manifoldAllocators;
b3BlockAllocator manifoldAllocators[B3_MAX_MANIFOLDS];
b3Mutex* manifoldAllocatorMutex;
// The body id pool is used to allocate and recycle body ids. Body ids
@ -353,20 +353,9 @@ static inline b3Manifold* b3AllocateManifolds( b3World* world, int count )
B3_ASSERT( count <= B3_MAX_MANIFOLDS );
int index = count - 1;
// Need lock because this is called from the parallel narrow phase.
// todo use atomic?
b3LockMutex( world->manifoldAllocatorMutex );
int currentCount = world->manifoldAllocators.count;
for ( int i = currentCount; i < count; ++i )
{
b3BlockAllocator allocator = b3CreateBlockAllocator( ( i + 1 ) * sizeof( b3Manifold ), 2 * B3_BLOCK_SIZE );
b3Array_Push( world->manifoldAllocators, allocator );
}
b3BlockAllocator* allocator = b3Array_Get( world->manifoldAllocators, index );
b3Manifold* manifolds = (b3Manifold*)b3AllocateElement( allocator );
b3Manifold* manifolds = b3AllocateElement( world->manifoldAllocators + count - 1 );
b3UnlockMutex( world->manifoldAllocatorMutex );
memset( manifolds, 0, count * sizeof( b3Manifold ) );
return manifolds;
@ -381,9 +370,7 @@ static inline void b3FreeManifolds( b3World* world, b3Manifold* manifolds, int c
B3_ASSERT( count <= B3_MAX_MANIFOLDS );
int index = count - 1;
b3LockMutex( world->manifoldAllocatorMutex );
b3BlockAllocator* allocator = b3Array_Get( world->manifoldAllocators, index );
b3FreeElement( allocator, manifolds );
b3FreeElement( world->manifoldAllocators + count - 1, manifolds );
b3UnlockMutex( world->manifoldAllocatorMutex );
}