Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Optimization Idea: Static Object Pooler

Discussion in 'General Discussion' started by jpthek9, May 24, 2015.

  1. jpthek9

    jpthek9

    Joined:
    Nov 28, 2013
    Posts:
    944
    For games that require a huge number of redundant objects (i.e. bullets), object poolers are a must. Current object poolers spawn X amount at the start of a scene and destroy them at the end. Is it possible to have an object pooler for game objects that statically holds its objects so they can survive through scene changes? With this ability, objects can even be pre-instantiated in the menu when not much is going on.
     
  2. Jodon

    Jodon

    Joined:
    Sep 12, 2010
    Posts:
    434
    Kiwasi and MD_Reptile like this.
  3. jpthek9

    jpthek9

    Joined:
    Nov 28, 2013
    Posts:
    944
    :O! WOW! That was easier than I expected xD.
     
  4. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,636
    Sure, but what problem does this solve? It gives you more to manage without improving performance where it matters, since the objects only need to be instantiated once at load time anyway.

    Also, in a large game you quite possibly don't know what objects you need to pool or how many of them until you get to the appropriate scene. There's no point instantiating 1000 lightning strikes if what you actually needed is 250 fireballs.

    Also consider that they can potentially introduce subtle and nasty bugs. (Notably, you're keeping a bunch of stuff around which might hold references to stuff in the previous scene.) There are strategies to effectively deal with these, of course, but my favorite and the easiest is re-creating them cleanly for each scene that needs them.
     
    Westland, jpthek9 and Ryiah like this.
  5. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    I agree with Penguin. There's only so much stuff I want to survive through a scene change. Most of the time, its nothing.

    I also only instantiate when an item is required and un-available (pool empty)
     
    landon912 likes this.
  6. jpthek9

    jpthek9

    Joined:
    Nov 28, 2013
    Posts:
    944
    Yup, I just tried a static object pooler. I was cowering from the swarm of bugs at one point. On the bright side, I gained an undetectable performance bonus.
     
    angrypenguin likes this.
  7. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,636
    If loading time is important and you know you'll be reusing the objects in most/all levels it could be worth it. You'll want to pre-plan your strategy to handle scene transitions, though.
     
  8. jpthek9

    jpthek9

    Joined:
    Nov 28, 2013
    Posts:
    944
    I think it can have a lot of use for match-based games like SC2. Actually, I got this idea from Starcraft 2 because they use a static object pooler which is why the Arcade Preloader works so well (it instantiates a bunch of units that get cached in the pool). I did it for this game's cubes because I figured that since it's going on mobile, I should squeeze every ounce of performance I can out of it.
     
  9. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,636
    Caching between scenes won't improve your frame rate, if that's what you're referring to by "performance". Pooling is in large part about avoiding garbage collection, which only happens at allocation time. It is also in part about avoiding the instantiation cost for high-frequency, short-lived objects. As far as your frame rate is concerned it doesn't care if you do these things at game load time or scene load time, and you will be doing some at both of those times anyway.
     
  10. jpthek9

    jpthek9

    Joined:
    Nov 28, 2013
    Posts:
    944
    Does it help to prevent lag spikes though?
     
  11. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,636
    It depends what's causing the "lag spike".

    Two possible causes are a high instantiation cost when creating a new object, or an instantiation triggering garbage collection. Those things don't matter at load time, regardless of whether it's initially loading the game or loading a new scene in the game. They can hurt if you're doing them during gameplay.

    If the "lag spike" is caused by something else - like network lag, or an overly expensive graphical effect - then this won't make any difference because it's a completely different part of the system.
     
  12. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860


    I believe these guy use the technique. For any given optimisation technique, you can find a game that will benefit from it. The trick is finding which techniques will best help your game.
     
    jpthek9 likes this.
  13. jpthek9

    jpthek9

    Joined:
    Nov 28, 2013
    Posts:
    944
    Thanks for that video! Very instructive. Still working my way through it but it's interesting to see how the industry works with pitches and whatnot.
     
  14. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Yeah, I strongly recommend browsing through all of the unite videos. Lots of little gems hidden around.