What object Pooling methods does coocs2d-x support?

Hello, I have seen many sample projects and all are extremely inefficient being that they create new objects all the time without considering re-use (an object pool). In my game I’m going to be creating many sprites and I wan’t to re-use them is there any standard way in cocos2d-x to implement because otherwise I can do it myself?

There are a couple pool containers that exist in the engine source and code base and there was even a new set of experimental allocators that are still work in progress, but no I don’t believe there’s any official pool class for c++ (there was one for JS, possibly still works).

One option is to use Vector<Enemy*> where Enemy is derived from Sprite (or Ref*). You can also checkout ccArray, and see usage in CCParallaxNode.cpp or CCScheduler.cpp. Add a flag for whether object is ‘active’ (hide inactive objects or remove them from the scene) as well as a reset or re-init method as needed.

Here’s a discussion around how to pool Sprites. Also discusses the engine experimental allocator templates.
Can anybody provide a simple c++ pool of sprites?.

Thread about the experimental allocators:
http://discuss.cocos2d-x.org/t/work-to-improve-memory-allocation-and-management-in-cocos2d-x/17442?u=stevetranby.

One solution from that thread:
Can anybody provide a simple c++ pool of sprites?.

Also from thread:
http://gameprogrammingpatterns.com/object-pool.html

3 Likes

Is it possible to use custom allocators without subclassing any cocos2d-x object?

First off you may want to concern yourself with pooling as a concept and focus on implementing that concept instead of worrying about allocators.

Allocators should be a standard requirement of any good high-performance game engine, but at this time cocos2d-x has relegated custom allocators to the libraries that really need them such as particles, physics, and parsing among others.


If you want to further discuss allocators …

Yes, and for non-Ref objects you can go search for any c/c++ allocator you like whether in a libraries source (e.g. Box2D, Bullet, RapidJSON, etc), or a video series (like HandmadeHero), or on someone’s blog. Or in this case you might as well search with “pool” to refine it toward your need.

I do believe the experimental allocators in the source at cocos2d-x/base/allocator work at a lower-level than the reference counting and thus will work for all objects. The allocators override the new/delete operators with a different implementation from the system OS standard library version to acquire the underlying data storage.

Retain/Release is at an abstraction level above the allocator so I believe you can use with any object, type, or struct, but I’m no expert.

I’ve only tried the experimental allocator a year+ ago and it didn’t quite work out-of-box and didn’t bother trying to get it to work because we already had written our own simple custom pooling implementations using c-array (or std::array<>) and Vector<> depending on whether we had known reasonable max counts at compile time (e.g. particles can choose to not emit any when no inactive remaining or could destroy in first in/out manner).


Allocation in concept is relatively simple (give me data, here’s the data back), but the implementations can differ in complexity depending on whether you just use standard OS malloc/free/new/delete or whether you just allocate a large chunk of memory that fits all objects your game will ever create, or whether you want re-use some portion of storage that you allocate (pooling).


RapidJSON has its own allocator as another reference.
https://github.com/miloyip/rapidjson/blob/67d8a99477b4e1b638b920cc9b02f8910dfb05e7/include/rapidjson/allocators.h.

Discussion of a simple block allocator, likely you won’t use, but maybe learn from?
CCNode and block allocator.
And the latest fork of the proejct mentioned:
https://github.com/billyquith/MemoryPool (forked from original, see network)

(anyone feel free to correct me if I’m wrong or have bad advice)

1 Like

Might be of interest … obviously sitting down with google for an afternoon would be smarter :smiley:

std::allocators and why they’re terrible, heh
https://www.youtube.com/watch?v=LIb3L4vKZ7U.
https://www.youtube.com/watch?v=LIb3L4vKZ7U

Jai’s allocators
https://www.youtube.com/watch?v=ciGQCP6HgqI.
https://www.youtube.com/watch?v=ciGQCP6HgqI

HandmadeHero’s Intro to Allocators

(there’s more info in other videos in the series)

157 - Introduction to General Purpose Allocation
https://www.youtube.com/watch?v=MvDUe2evkHg.
https://www.youtube.com/watch?v=MvDUe2evkHg

160 - Basic General Purpose Allocation
https://www.youtube.com/watch?v=MyGsWY6dezE.
https://www.youtube.com/watch?v=MyGsWY6dezE

161 - Finishing the General Purpose Allocator
https://www.youtube.com/watch?v=1LyHQVYlClw.
https://www.youtube.com/watch?v=1LyHQVYlClw

1 Like