4

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?

Servy
  • 202,030
  • 26
  • 332
  • 449
edwing
  • 81
  • 7
  • What types of objects are you using with this pool? For the majority of objects creating a new one is so fact that this will likely be slower; it's a rather special case pattern that should only apply to objects that are very expensive to create. – Servy Jan 17 '13 at 20:36
  • They are pretty basic: 10 variables and 4 of them changes every frame(scale,color, velocity and angle) and one draw process. – edwing Jan 17 '13 at 20:57
  • 1
    Something like that will take *very* little time to create, which makes this fall into the category of optimizing something that's just not a problem in the first place. "Premature optimization is the root of all evil." – Servy Jan 17 '13 at 21:00
  • Generally, when implementation of a feature (such as this) doesn't require a lot of work, recommendation is to try both and see if there is even any difference, but as it has already been said here, it doesn't help on Xbox much. Also premature optimization is evil >:) – user1306322 Jan 18 '13 at 00:08

1 Answers1

5

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.

Jimmy
  • 89,068
  • 17
  • 119
  • 137