One of the rules I’ve lived by as a progressional developer was to “optimize last”.
I’ve followed that rule pretty closely as I near completion of a side-scrolling 3d space shooter.
Working fairly blind (without Unity Pro - and hence no profiler), I am working through my ‘optimization’ list - one item of which is object pooling.
Most of my enemy/terrain objects are already pooled by virtue of an asset I am using - but I generate a huge amount of ‘other’ game objects in the form of missiles, lasers, and most of all , explosions. Explosions mostly consists of particle systems - occasional components added to them to de-parent parts when appropriate for cool post-explosion effects.
I decided to start implementing an object pool provided by Jean Moreno as part of his Cartoon FX assets. It’s very well written package - and I’ve used it in the past without issue (it uses a dictionary to look up prefabs by instance id).
about 75% done with my ‘conversion’ away from instantiate/destroy, I ran into a couple of snags that would require me to further refactor some things - and before I put a lot of energy into it, I though I would give myself a wam and fuzzy with a performance test before and after.
I set up a test whereby I would randomly instantiate explosion prefabs - using both the object pool and the instantiate/destroy method… My performance metric would be based on the Update() timing I was able to measure - accumulating average information and noting ‘stutters’ (which I defined as any update call that was greater than 1/30 of a second from the last one) My whole goal was to reduce/eliminate any ‘stutters’ caused by GC or other nuances of the engine dealing with lots of instantiation and destruction of game objects.
Running on my mac, my results were mostly expected - very little (if any) stutter with either method - and no discernible difference between the two.
Without object pooling
———————————————
Run time: 300 seconds (5 minutes)
Objects created: 2833
Average Update() differential (time between calls to Update): .0169
Stutters counted: 24
With object pooling
—————————————————————
Run time: 300 seconds (5 minutes)
Objects created: 2796
Average Update() differential (time between calls to Update): .0168
Stutters counted: 23
I then moved the test to my android Galaxy S4
Without object pooling
———————————————
Run time: 300 seconds (5 minutes)
Objects created: 2760
Average Update() differential (time between calls to Update):.0530
Stutters counted: 4907
With object pooling
—————————————————————
Run time: 300 seconds (5 minutes)
Objects created: 2763
Average Update() differential (time between calls to Update): .0541
Stutters counted: 4860
I’m debating ripping out the work I’ve done thus far tonight and migrating these explosions BACK to simple instantiate/destroy techniques.
I guess I could run the tests longer…….but I’m tired now.
Any comments on my experiment (technique or results)?
1708249–107505–PerformanceTest.cs (1.5 KB)