Question

I am creating a particle system in XNA4 and I've bumped into problem. My first particle system was a simple list of particles, whose instances are created when needed. But then I read about using pools.

My second system consists of a pool, filled with particles, and an emitter/controller. My pool is pretty basic, this is the code:

class Pool<T> where T: new ()
{
    public T[] pool;
    public int nextItem = 0;


    public Pool(int capacity)
    {
        pool = new T[capacity];

        for (int i = 0; i < capacity; i++)
        {
            pool[i] = new T();
        }
    }
    public T Create()
    {
       return pool[nextItem++];
    }
    public void Destroy(T particle)
    {
        pool[--nextItem] = particle;
    }
}

The problem is pool system is much more hungry for CPU. Every time I take out particle from pool to my emitter I'm forced to re-initialize and reset particles, due the constructor absence and this is a problem.

Is there any point at using pools, if I re-init those particles or I should leave pools for arrays of completely identical objects that never changes?

Was it helpful?

Solution

In an XNA application, typically object pooling isn't supposed to speed up initialization. It is to speed up garbage collection. The GCs on consoles are slower, and in a game where your entire update function has 16 milliseconds to run, a collection pause can be visibly noticeable. This may or may not a concern for your particular case, especially if this is a PC application, depending on the number and lifetimes of particles you are using.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top