Like most, I've noticed that the largest enemy to smooth Android game is the garbage collector. I've been looking at ways to cut down the amount of object allocations done during the average 'step' operation in JBox2D. I have a few ideas, and I was wondering what other people thought.
1) I've noticed the evaluate method in the Contact Objects ( like org.jbox2d.dynamics.contacts.CircleContact ) generates several objects every cycle.
Essentially a contact point and a manifold are generated and then passed to a ContactListener, (if it is defined). I think this area would still work if those objects where generated once along with the parent Contact object and used over and over again. The only problem with this would be that the user callback would be receiving a 'borrowed' object, that it would have to copy before playing with.
Also, change the
to
2) The World object creates two island objects (one in solve and one in solveTOI). Rather then re-creating them every step, create them once, keep them as private objects and add a method Update to Island.
This would only re-allocate the arrays when they needed to get bigger.
3) Create a 'pool manager' that keeps references to frequently generated objects, and recycles them rather then allowing the garbage collector to get to them:
1) I've noticed the evaluate method in the Contact Objects ( like org.jbox2d.dynamics.contacts.CircleContact ) generates several objects every cycle.
Essentially a contact point and a manifold are generated and then passed to a ContactListener, (if it is defined). I think this area would still work if those objects where generated once along with the parent Contact object and used over and over again. The only problem with this would be that the user callback would be receiving a 'borrowed' object, that it would have to copy before playing with.
Also, change the
Code: Select all
cp.normal = m_manifold.normal.clone();to
Code: Select all
cp.normal.set( m_manifold.normal )2) The World object creates two island objects (one in solve and one in solveTOI). Rather then re-creating them every step, create them once, keep them as private objects and add a method Update to Island.
Code: Select all
public Update(int bodyCapacity,
int contactCapacity,
int jointCapacity,
ContactListener listener) {
m_bodyCapacity = bodyCapacity;
m_contactCapacity = contactCapacity;
m_jointCapacity = jointCapacity;
m_bodyCount = 0;
m_contactCount = 0;
m_jointCount = 0;
m_listener = listener;
if ( m_bodies.length < bodyCapacity )
m_bodies = new Body[bodyCapacity];
if ( m_contacts.length < contactCapacity )
m_contacts = new Contact[contactCapacity];
if ( m_joints.length < jointCapacity )
m_joints = new Joint[jointCapacity];
m_positionIterationCount = 0;
}This would only re-allocate the arrays when they needed to get bigger.
3) Create a 'pool manager' that keeps references to frequently generated objects, and recycles them rather then allowing the garbage collector to get to them:
Code: Select all
import java.util.LinkedList;
public abstract class MemoryPool<T> {
private LinkedList<T> free_items = new LinkedList<T>();
public void freeItem(T item) {
free_items.add(item);
}
public abstract T Allocate();
public T newItem() {
T out = null;
if ( free_items.size() == 0 ) {
out = Allocate();
} else {
out = free_items.getFirst();
free_items.removeFirst();
}
return out;
}
}