Using Object pools
Joa Ebert
Is right when he says that utilizing object pools can make your code
Perform a lot faster. An object pool is just a container for a bunch
Pre-constructed objects that are kept in memory ready for use, rather
Than being repeatedly allocated and destroyed on demand.
Object pooling makes sense if:
- You create dozens of short-lived objects in real-time applications like games
- You need to store and share temporary data throughout complex algorithms
- The objects are expensive to create (effecfields, complex inheritance chain, nested objects)
- The objects are expensive to remove (unregister listeners, nullify instances)
The only drawback is that memory consumption will raise, but
Ridiculously low memory prices this shouldn't be problem if used wisely
Implementation
So here is myObjectpool.
Manager class which is
Attempt to create a lightweight, fast and reusable solution.
Implementation is based on a circular list, and the API is very simple.
Download: objectpool_v1.0.zip
(Source, example, asdoc)
Edit
I have updated the class so it also accepts a factory for Object Construction.
Download: objectpool_v1.1.zip
First, we create the object pool:
var isDynamic:Boolean = true;
var size:int = 100;
var pool:ObjectPool = new ObjectPool(isDynamic);
pool.allocate(MyClass, size);
TheIsdynamic
Flag defines the behavior for an empty pool.
If true, the pool automatically creates a new bunch of objects for you.
If false, the class throws an error to indicate that the pool is empty.
TheSize
Value indicates the pool's Capacity-if the pool is
Dynamic, the pool grows by the initial size each time it becomes empty
So it actually never dries up.
By calling the allocate method the pool is filled with 100 instances
Of myclass. You can always reuse the pool for another class by invoking
This method again.
If you need to initialize the objects by passing arguments to it, you can do this with a little helper method called initialize:
pool.initialize("funcName", [arg0, arg1,...]);
This goes through every object and applies the function with
Given arguments upon each object. This can also be done by reading each
Object, calling the function and putting it back:
for (var i:int = 0; i < pool.size; i++)
{
var o:MyClass = pool.object;
o.init(arg0, arg1, ...);
pool.object = o;
}
Now to get an instance of myclass you access the pool like this:
myObjectArray[i] = pool.instance;
//instead of
myObjectArray[i] = new MyClass();
When you are done with your object, instead of throwing it into the garbage collector, you "recycle" It For The Next use:
pool.instance = myObjectArray[i];
This assumes that you are storing your instances in an array or
Something else because if you loose the reference, well it's lost and
Can't be reused anymore and be careful not to assign the object
Twice, since then your pool wocould contain duplicates of the same object!
That's all, pretty simple right?
Finally, there is the purge () method, which is only interesting
Pools that are dynamic. As the pool grows with the demands of
Application, it can get quite big. The Purge Methods scans the pool and
Removes all allocated but currently unused objects, leaving with
Compact representation.
Demo
Here is a little demo which demonstrations how the pool works
Internally. Actually it's very simple. Pressing the right arrow key
Reads an object from the pool (first row), which is then stored in
Second row beneath. Pressing the left arrow key gives the object back
The pool. Pressing the Enter key performs a purge () operation.
Purple circle points to the node where the next object is read, the blue
Circle to an empty node where the insertion is already med.
Performance
Benchmarking revealed that it's always faster to cache instances,
Even for the generic object class. All benchmarks were done with
Release player 9.0.124 on Vista, an object pool size of 100 and with 100
Iterations each to get an average value.
The purple bar indicates the time needed to access the pool:
for (var i:int = 0; i < k; i++) instances[i] = p.instance;
The blue bar measures the task of reading the objects, then putting them back:
for (i = 0; i < k; i++) instances[i] = p.instance;
for (i = 0; i < k; i++) p.instance = instances[i];
The gray bar shows the time needed for creating the objects on the fly:
for (i = 0; i < k; i++) instances[i] = new MyClass();
Here my result:
Caching a native flash object can be almost 5x faster instead of creating it on the fly.
Almost the same applies to a slightly more complex object like the Flash. Geom. Point class.
Creating complex objects, here from the flash. display. Sprite class, is extremely slow and up
To 80x faster when pooling.
Reprinted: http://lab.polygonal.de/2008/06/18/using-object-pools/