Object Pool Performance

I’m creating a simple object pool, and I notice that there a a few different ways to go about this.

The first thing that came to mind was having a single List, and tracking used/unused objects by activeSelf.
However, this will require iterating through the list to find an unused object. Additionally, I can see some scenarios where a disabled object might not be ready for reuse.

Next, is the idea of having two Lists (or Stacks), one for available objects and the other for unavailable objects, moving them between Lists as needed.
(It should be noted that there doesn’t seem to be much performance difference between Stack and List, if only Adding/Removing from the end of List: http://stackoverflow.com/questions/13211277/performance-differences-so-dramatic )
So this saves having to iterate through Lists looking for free objects, but removing and adding twice for each object may negate any performance gains. I’m not sure here.

Now, I’m wondering if if might just be better to keep a single List, remove objects from the end when being used, and let the object keep track of itself. When it’s ready to be reused, it can just tell the List to add it back on. This seems to save operations over the dual List. But maybe there’s another downside here?

Maybe there’s a better way?

I know this is all just theorycrafting, but I’m just looking for some general rules-of-thumb as to the best way to do this.

What are some of your lessons-learned?

No pool is perfect. I used pools only in few cases.

  • Used in my custom coroutine code to avoid Garbage collection
  • Used to recycle fired bullets.

The biggest lesson is to not optimize if not needed. A pool add some requirements to your code (objects have to be returned to the pool in some way), and there’s Always the risk to add unnecessary data that may cause problems later (what if another code add a component to your pooled object?).

If you have performance problems on some target platforms, then you may need a pool, or more simply you may need to remove unnecessary GameObjects and replace them with classes.

Whatever your implementation of pool you use, do not use

  • Array
  • LinkedList
  • Queue.

Use a Stack! it is faster because it has better locality (it uses an array inside, but prevent your from rewriting already existing code. Another advice do not reinvent the wheel).

Any pool is a thousand times better then no pool. And the difference between pool implementations is not huge, and will depend heavily on your use scenario.

If you must squeeze the maximum performance out of your pool, it’s difficult to beat a static sized array. But this approach requires more leg work then the others, so I wouldn’t reccomend it as a first choice.

Fair enough. However, this IS an excellent learning opportunity!

Good to know. I’m more interested in versatility, and then good performance within that idea. This is an asset that would be re-used in many different projects.

And by that, are you referring to a built-in array? I’m under the impression that Lists and Stacks are actually backed by such arrays, with the added benefit of being able to handle re-sizing if necessary (among other things).

Ultimately, however, the intrinsic performance difference between a basic built-in and and List or Stack is probably negligible for my purposes, and I think I’m asking more about the differences in how they are used in the pool implementation. Of course, let me know about any hidden “gotchas” you’ve come across.

As an example, say we’re talking about List. I’ve seen some examples where items are inserted and removed from index 0. This is much slower than adding adding and removing items from the end of the array, and I can see no good reason for not doing it that way. It seems wasteful and unnecessary. As far as I can tell, It doesn’t make the code more complex, it doesn’t make it less readable, and it improves the performance. It doesn’t take any extra time to make or test, so why wouldn’t I do that instead?

At any rate, here’s my thinking:

As to versatility, there are three ways for the pool to handle being empty:

  1. Dynamically add objects.
  2. Reuse the oldest active object.
  3. Simply cause the spawn to fail.

In the first example of my original post, a single collection is used and objects are differentiated by the active state as to whether or not they are available.
On the plus side, this doesn’t require an extra script on the objects, and can handle all three use cases.
The downside, is that it requires iteration to find inactive objects, and this can get expensive with large collections. Also, the objects would have to reserve their inactive state for only being used to indicate pool availability.
I could use some other indication, such as bool attached to the collection entries, but then the object needs to know where to reference it, and that will require the object have another script. Is this a big deal? Probably not, but it’s a complication.

As I write this, I now realize the example with two collections would require the less efficient RemoveAt() since in the collection of active objects, there’s no guarantee of which object is to go back to the pool. In fact, it also requires finding the correct object, and now we are getting into territory that seems to be losing any advantages over the first example.

The last example looks to have a lot of promise, but the downside, is that every object will need a specific script to record which pool the object belongs so it knows how to go back, and you lose the ‘reuse functionality’ since there’s no tracking of currently used objects.
I could have a another collection solely to record that information, though. And which would be simply ignored if it’s not needed.

However, the addition of scripts to pool objects may constitute development complexity that isn’t altogether desirable.
I wonder if adding the necessary scripts at runtime might be the way to go here. This might be a nice way to keep everything convenient as possible, but I suspect adding scripts on-the-fly like this might open up a can of worms for reasons I don’t know about. EDIT: Yeah, I looked into it, and it’s a terrible idea.

EDIT: Ok, I tried a simple implementation of the last idea (without reuse functionality) and I benchmarked 1000 primitive cubes. Total time to spawn and then despawn them with List was between 15-16 milliseconds, and Stack was 15 milliseconds. Compared to the iteration approach which took 65 milliseconds. I think this is good?