January 10, 2014
To thread, or not to thread. That is the question.
With the arrival of multicore CPU’s for mobile devices, the talk about threading the Cocos2D API has steadily risen, until it has reached a level where it seems to be consensus, that without multi threading, we will never reach the Shangri La of 2D rendering. Let me briefly state my background on this. Before I became lead developer of Cocos2D-iphone, I was schooled in threaded programming, and have spend several years doing low level threaded stuff, like Windows drivers, PCI, serial, and also threaded programming on ex. high end DSP’s. When venturing into multithreading, the first question you have to ask, is: Do I have code, where the order of execution is irellevant? If not, multithreading will not increase performance. Only decrease it. This has been described in Amdahl’s law, and while I do not fully agree with it, it still gives you a pretty good estimate of what you should expect. If your code can be executed in no particular order, the next step is performance. The overhead of syncronizing multithreaded code, is quite considerable. While working for an american company building ticket scanners for horse racing, I spoke to some of their low level network programmers, which is an area where threaded programming has been used a lot. They said, that with a proper implemented first thread, their ultimate goal would be an 50% speed increase. Second thread was only expected to give a 10% gain. So in best case, two cores are not 200%, but 150%, and three cores are not 300% but 160%. As for mobile game programming, performance has always been a key issue, so with an incresed number of CPU’s, it is natural to look into multi threading. This was also the case in mid 2000, when multi CPU’s became the standard in PC’s. One of the companies exploring this, was ID, with the title Quake 4. They build a multi threaded render engine, which improved performance. The huge difference was, that in the Quake4 days, the CPU was the bottle neck. This is not the case on mobile devices, where the GPU is severly hamstrung due to power consumptions. It is also worth to mention, that ID dropped multithreading in the successor Doom3. All in all, I do not think multithreading the Cocos2D API is worth it. GPU will on mobile devices, for some time to come, be the limitation, so that is where I think we should look for performance improvements.