Minimizing/eliminating allocations during Update?
Posted: Wed Nov 30, 2011 11:33 pm
Hi, I'm looking for ways to minimize (preferably eliminate) allocations during BEPU update. Are the following things normal and am I approaching the solution correctly?
Heap memory is allocated on the following occasions:
Space.Add/Remove
Allocations occur on calls to space.Add/Remove. Taking a look at BEPU's code, there's object pooling going on, so I was expecting objects to be reused after a while; I'm not sure why it is allocating more each time. Is it normal and is it possible to avoid it?
My attempted solution is to create a pool for entities and add every entity to the space before-hand (on initialization). I haven't found an easy way to tell the space to "ignore" entities that aren't active (other than Removing it, is there such a property?) the best I've come up with is to zero-out all relevant properties (mass, velocity, momentum etc) and set it to not render on returning the entity to the pool, then re-initialize it when taking an entity from the pool.
This only partially works... allocations still occur for several space Updates for about a minute or two. After this (assuming no collisions) no more allocations occur and the heap size eventually settles on a number depending on how many objects I'm simulating. Is there a better way to pre-allocate objects in the space and/or avoid allocations during update?
Collisions
Every few collisions create allocations on the heap. From an earlier topic, I picked up the tip: pre-allocate NarrowPhaseFactory collision pairs. But even allocating about 500 ConvexConvex pairs ahead of time (plenty, in contrast to the ~20 collisions occurring every minute) hasn't prevented allocations from occurring every few collisions. Normal? can I prevent this?
Heap memory is allocated on the following occasions:
Space.Add/Remove
Allocations occur on calls to space.Add/Remove. Taking a look at BEPU's code, there's object pooling going on, so I was expecting objects to be reused after a while; I'm not sure why it is allocating more each time. Is it normal and is it possible to avoid it?
My attempted solution is to create a pool for entities and add every entity to the space before-hand (on initialization). I haven't found an easy way to tell the space to "ignore" entities that aren't active (other than Removing it, is there such a property?) the best I've come up with is to zero-out all relevant properties (mass, velocity, momentum etc) and set it to not render on returning the entity to the pool, then re-initialize it when taking an entity from the pool.
This only partially works... allocations still occur for several space Updates for about a minute or two. After this (assuming no collisions) no more allocations occur and the heap size eventually settles on a number depending on how many objects I'm simulating. Is there a better way to pre-allocate objects in the space and/or avoid allocations during update?
Collisions
Every few collisions create allocations on the heap. From an earlier topic, I picked up the tip: pre-allocate NarrowPhaseFactory collision pairs. But even allocating about 500 ConvexConvex pairs ahead of time (plenty, in contrast to the ~20 collisions occurring every minute) hasn't prevented allocations from occurring every few collisions. Normal? can I prevent this?