Object Pool class
8th October 2008
Two of the slow operations in the flash player are object creation and garbage collection. If we pool objects - save objects when they're no longer needed and reuse them later when another object of the same type is required - then object creation is kept to a minimum and garbage collection is reduced to zero.
Joa Ebert discussed this in his presentation at Flash on the Beach. I already use specific object pooling in the Flint particles library, but while he was talking I had an idea and wrote a class for generic object pooling of all object types. I finally had time to test the class, and it works as expected, so I've added it to my open code repository, where you can download it.
The object pool class manages pooling of all objects through two simple methods. You can obtain an object from it like this
var obj:SomeClass = ObjectPool.getObject( SomeClass );
If there are any such objects in the pool, one of them will be returned. If there are no such objects in the pool, a new one will be created and returned to you.
When you no longer need an object, you can add it to the pool for later reuse like this
ObjectPool.disposeObject( obj );
The object doesn't have to have been created through the pool - any object can be dropped into the pool with the disposeObject method.
Download the ObjectPool class.
Read more articles about Flash, Flex, Actionscript 3
5 Comments add your own
hi Rishard,
it is strange idea for me. if you can put an example for that it will be great.
regards
delizade | 14th October 2008 at 10:08 pm
Hmm, very interesting! Too bad I’m already 80% of the way through a project where this could be really useful; a little late to do too much with it now. In the future, though!
One question: I noticed that in the construct method, you’re doing a big switch to check how many arguments you have coming in. Is there any way to use apply with a constructor? Might be more flexible, but maybe that doesn’t work.
Dru Kepple | 17th October 2008 at 12:33 am
Unfortunately there’s no way to use apply with a constructor.
richard | 17th October 2008 at 8:26 am
I use Objects pools too, but I implemented it in a different way. I putted the pool in the created object. This way you can use the constructor. I use linked stack for the pool. This can be added to any class with little changes.
package{
public class Poolable{
public var userData:*;
private var next:Poolable;
private static var first:Poolable;
public function Poolable(){
trace(”object creation!”);
}
public function recycle():void{
if(first==null){
first = this;
}else{
this.next = first;
first = this;
}
}
public static function getObject(data:*):Poolable{
var poolable:Poolable;
if(first==null){
poolable = new Poolable();
}else{
poolable = first;
first = first.next;
}
poolable.userData = data;
return poolable;
}
}
}
Frédéric Gascon | 2nd December 2008 at 5:26 am
Bummer about apply not working with constructors, I wonder what they were thinking?
I decided to adapt your construct class and use the ..rest property instead.
e.g.
http://code.google.com/p/troyworks/source/browse/trunk/AS3/dev/src/com/troyworks/util/new2.as
TroyWorks | 5th December 2008 at 4:57 am
Leave a Comment
XHTML: you can use these tags - <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>Subscribe to the comments via RSS Feed