Jump to content

Any Performance Benefits to clearWorld = false


JTronLabs
 Share

Recommended Posts

When I start my 'Play' state from the 'Menu' state, the game is laggy for a short while before smoothing out. I was thinking that maybe if I initialized my sprite pools after Preload instead of the beginning of Play it would solve the initial lagginess. I reorganized all my states to be reusable (revive components in create() instead of creating new ones, set clearWorld = false in state.start() ), but don't think I see much of a performance gain. Might it be there but just not extremely noticeable? Even when switching to my 'Statistics' State, there is a few 100s of ms lag when starting, which I assumed would go away after the change but has not. Any ideas on why this might be? Or was this a foolhardy change to make to begin with? I'm using a Samsung Galaxy S4 with Cordova Crosswalk.

 

Edit: I take it back. By paying a bit more attention I would say Statistics screen loads about 2x as fast, just by eyeballing/guesstimating it. Not sure why the game isn't experiencing similar gains. Has anyone else done this and found similar improvements/lack thereof?

Edit: By setting visible to true/false instead of killing and reviving I further chopped off loading time for my Statistics State. Sprites in pools still have to be killed though.

Edit: Now I've noticed that if I've opened Statistics, or other background states, and leave them in memory while playing the game then the game is much slower. This seems to only happen if turning off the visibility on state change, instead of killing/reviving. So I'll stick with killing/reviving, which does not seem to impact game performance (as far as I can tell).

 

Edit: Overall I'd say this approach gave me some solid (though subtle) performance improvements. It also made the code a mess. I just discovered my performance issues were due to the lack of pooling on some random text object. I fixed that and it runs MUCH smoother now. I will probably keep the states as they are though, or maybe revert it just to test how perf would stack up.

Edit: I reverted back to clearing state and have not noticed any performance hits. Turns out the Stats loading time decrease was a red herring, it happens either way. My original performance issues were due to not pooling a commonly spawned text resource. Keep reading thread for more perf ideas.

Link to comment
Share on other sites

Okay, so not sure if this will help to answer your question.. but it depends on how well you've coded your game. If you are adding objects to the world, then yes.. clearing the world when you switch state would have a performance increase. I'm not sure if this would help with the loading time of a state, but certainly with playable performance. Here's why..

Pixi.js is a forward renderer, meaning on the RAF (request animation frame) it requests a list of display objects. The top most, being the game is a container. Inside the game, you have states.. each of these is a container. Inside each state, you may have groups, sprites, menu buttons, bitmap texts etc etc etc.. all of these are display objects. Upon an RAF, there is a recursive function that will traverse the entire game, and get a display list of objects for Pixi to render. Why this matters? 

If you were creating objects in your game like...

var sprite = this.game.add.sprite(x, y, frame);

var group = this.game.add.group();

var tween = this.game.add.tween().to();

var bitmap = this.game.add.bitmapText(text, font);

The problem with this approach, is you are not adding each display object to the current running state, but rather to the entire game object. As a consequence, when you change state, the references to the object still exist, and is not flagged for garbage collection. When you add objects to the state, by using "state.add.sprite.." you are creating a reference inside the state. Then, as you change the state, the state gets destroyed, and any and all objects within that state are also destroyed. If done properly, a clear world shouldn't really make any difference to how the game performs. 

If you are interested to see how the display list is rendered, check out the pixi.js docs https://pixijs.github.io/docs/PIXI.Container.html#children. Disclaimer: The new pixi docs are way ahead of where the version phaser is still running, but it should give you a good indication of what's going on under the hood.

Hope this helps in any way!

Link to comment
Share on other sites

Not exactly what I was looking for. Originally was wondering if clearWorld =true would allow you to essentially "pool" sprites across state changes. I'm finally done testing it, and found no noticeable improvements (and it made the code real messy). So I've removed it.

 

However, what you're talking about is very interesting. Not being flagged for GC is kind of a big deal. I've been led to believe (from the docs - http://phaser.io/examples/v2/sprites/add-a-sprite) that game.add.XXX is the way to go. But you're saying it'll decrease perf and lead to memory leaks? If true, that is a big help and I'm going to change this ASAP.

Edit: I guess not being flagged isn't a huge deal. If you keep a reference to it it'll be overwritten next time you come to the state, and original freed for GC. If not then that would be a problem.

Link to comment
Share on other sites

I tested your post and I think it is incorrect. I used this.add.text and this.game.add.text, then switched between a few states. When the state began on the second time, I printed out this.game.world.children and it was empty either way. I also tried setting the texts to an instance variable "this.text = this.game.add.text", "this.text2 = this.add.text". When I return to the state the second time they still existed (soley because I save these instance refs I believe), but both had been properly .destroy()'d by state.start("XXX") where clearWorld=false by default. Thus I think I'm going to revert to using this.game.add as the code is much cleaner. In case my tests are wrong (don't think so but it's certainly possible), I didn't have any noticeable improvement gains in-game by using currentState.add.

Link to comment
Share on other sites

Hmm okay, what language are you using? (vanilla, coffee, ES6, TS). When I ran some performance tests, I found it made an impact, if not huge, certainly noticeable and reliable. 

Could you try to console log the text object out in a separate state from the one it was created. I'm really interested to see how it's keeping state persistence without removing the object. If you added 10,000 text objects to the game, and switch to state 2, are those 10k objects still being given a update transform? I will try and do some benchmarks tonight. 

I will get back to you on this :)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...