-
Object Pooling
I'm trying to do this for a game I'm working on, I've been suggested it may be a problem to my games lag.
I'm new to object pooling so I'm not quite sure if this is right or not.
It's a simple test which creates squares (Things) at 0 y, and at a random point at X and places them into the pool array. They are then added 2 at a time to the display list and spliced from the pool array. They then move at 2 pixels per frame toward the bottom. Once they reach the bottom they are then removed from the display list and added back to the pool.
I've tested this things FPS and it's execution time against a normal one which does not use object pooling. I cannot see much difference at all however. So I'm wondering if the pooling used in this example is correct before I attempt to implement it into my game.
Code:
var thing:Thing;
var a:Array = new Array();
var pool:Array = new Array();
for(var i:int = 0;i < 400;i ++){
thing = new Thing();
thing.x = Math.random()*550;
thing.y = 0;
pool.push(thing);
}
stage.addEventListener(Event.ENTER_FRAME, onEnter);
function onEnter(e:Event):void {
for(var i:int = 0;i < 2; i++) {
if(pool[0] != undefined){
addChild(pool[0])
a.push(pool[0]);
pool.splice(0,1);
}
}
for(var j:int = 0;j < a.length;j++){
a[j].y += 2;
if(a[j].y >= 300){
a[j].x = Math.random()*550;
a[j].y = 0;
pool.push(a[j]);
removeChild(a[j]);
a.splice(j,1);
}
}
}
-
Hype over content...
With object pooling the speed increase isn't really noticeable at first as you're having to create the objects as normal.
You get the improvement after every possible object has been created, as the game doesn't have to make any more and the garbage collector doesn't have to kick in.
So say in the case of using pooling for particles, after a while you'll have a load of them in the pool and that's when you start getting your boost.
Also in real life, using particles as an example, you'll have code running in the constructor, where you get stage references; define vars etc.
With pooling these will only be run once, and then when you need a particle just pull it from the pool and give it it's x/y and speed coords ( ie, you skip the whole constructor stage ).
As to your code, looks fine, but to be honest I've not ripped it apart line by line.
Squize.
-
yeah, what squize said. also don't splice where you don't have to, always push and pop whenever possible. these operations are faster than operating on the beginning or middle of the array because the array doesn't need to be reindexed internally. also, cast to your type when accessing through the array Thing( a[ j ] ).whatever. Or use Vectors instead if you're working with Flash 10.
also, it looks odd that the logic of adding to your live list is based on whether the pool is populated at all, it's kinda backwards that way.
-
Alright. How would I correctly use the pop method? The above code will scan through the array and splice out whichever Thing is past 300 pixels Y. Is a say pop, won't that just remove the thing at the very end of the array? Which could and probably would be the Thing I wouldn't wish to remove?
-
yes, but for adding to your live list, you pop off the pool:
a.push( pool.pop() );
-
Hype over content...
If it's any help I use these sexy data structures:
http://lab.polygonal.de/ds/
For my pooling at they're quicker. Just the other night I set up the haXe version as it's now in a handy swc. Meant to be quicker again, although I've not tested it to death.
Squize.
-
Originally Posted by Squize
They are nice alright. Example: switching from an array to a 'priority queue' of paths to trawl instantly turns my current breadth-first pathfinding algorithm into the A* algorithm.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|