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:
- Dynamically add objects.
- Reuse the oldest active object.
- 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?