IOS PERFORMANCE Getting the most out of your Games and Apps
AGENDA Intro to Performance The top 10 optimizations for your games and apps Instruments & Example Q&A
WHO AM I? Founder of Prop Group www.prop.gr Background in enterprise software, now iphone+ipad games! 2D physics game, Payload in the AppStore
In Progress... www.cocos2dbook.com
SPACE VIKING GAME Learn how to build the Space Viking Game! www.cocos2dbook.com
WHY CARE ABOUT PERFORMANCE? Better Performance = Better experience and responsiveness to the user Less battery use Headroom for additional features
POP QUIZ! What is the slowest (non-network) hardware subsystem/ component on ios Devices? What is the 2nd slowest? (Hint: They are both battery hogs) Answer: Flash Memory (storage) and RAM
IT S ALL ABOUT THE MEMORY On iphone 3GS for example: L1 Cache ~ 1 CPU Cycle L2 Cache ~ 10 CPU Cycles Normal RAM ~ 80 Cycles Addition ~ 3 Cycles
TIP #1 IMAGE BIT DEPTH Images are stored in RAM decompressed (Length X Width X Bit Depth )/8 = Bytes in RAM Example: Black 1024x1024 image, saves as a 25K PNG file (1024x1024x32)/8 = 4.1 Megabytes!
TIP #1CONTINUED Use the lowest bit depth you can. Less bits per pixel = less memory = faster game Set the CCTexture2D default format before loading [CCTexture2D setdefaultalphapixelformat:kcctexture2dpixelformat_rgba4444]; You can change the CCTexture2D format anytime
TIP #2 USE SPRITE SHEETS Use tightly packed Sprite Sheets via the CCSpriteBatchNode Reduce your OpenGL ES texture binds from many calls to 1 Easy to create using TexturePacker or Zwoptex Less Texture Memory = Better Performance
PERF TIP #2 (CONTINUED) USE SPRITE SHEETS
PERFORMANCE TIP #2 CONTINUED
SPRITE SHEETS EXAMPLE
HOW DO WE GET TO THIS?
BACKGROUND
BACKGROUND ANIMATIONS
TRUCK & CONTROLS
HOW MANY BIND CALLS?
SPRITE SHEETS CCSpriteBatchNode - collection of CCSprites from a Sprite Sheet, OpenGL ES draw calls batched Support for using Zwoptex Sprite Sheets/Texture Atlas
TIP #3 REDUCE, RE-USE, RECYCLE Re-Use your CCSprites, and other Cocos2D Objects. If you have a set number of characters/sprites, allocate them at initialization time
TIP #4 AUDIO FORMATS/LOADING Load audio in a background thread, or during an intro sequence. Preload it before you need it. Take care of mp3 hardware decoder, send the background track first (or your largest mp3 file)
TIP #5 THUMB SUPPORT Thumb, turn it OFF for Armv6, and ON for Armv7 Thumb on Armv6 has NO floating point support. MapKit uses a ton of floating point!
TIP #6 COCOS2D TEXT LABELS CCLabelTTF is... super slow - creates a new texture every time you change the text. (Hint: This is Bad Bad Bad) Use CCLabelBMFont instead Create your font atlases in Glyph Designer or Hiero
TIP #7 USE THE SCHEDULER Never update in -(void)draw Use [self scheduleupdate] -(void)update:(cctime)deltatime method called on every frame Update Driven by CADisplayLink Render
TIP #8 LIMIT PARTICLE SYSTEMS SIZE Remember ios Device!= Mac Re-Use particle systems instead of re-creating every time you need it Test on the actual device
TIP #9 MEMORY WARNINGS Respond to memory warnings. What can you unload? (Animations, sound effects, textures) Call CCTextureCache, to purge the textures after you dealloc your scene. [[CCTextureCache sharedtexturecache] removeunusedtextures];
TIP #10 PROFILE YOUR APP Profile early and often Find your bottlenecks and focus on the biggest issues Don t optimize prematurely Leaks in the Simulator, everything else on the actual device!
WWDC 2011 INSTRUMENTS SESSIONS Session 310 - What s New with Instruments Session 312 - ios Performance and Power Optimization Session 416 - Tools for Tuning OpenGL ES Apps on ios
WWDC 2010 INSTRUMENTS SESSIONS Session 304 - What s New with Instruments Session 309 - Advanced Performance Analysis with Instruments Session 311 - Advanced Memory Analysis with Instruments Session 306 - Automating User Interface Testing with Instruments
INSTRUMENTS EXAMPLE Ole the Viking versus Space Debris Tons of falling debris, lots of elements on the screen Similar to space shooters, side scrollers, and tower defense games
SPACE VIKING + SPACE DEBRIS
1100 CCSPRITES - NO CCSPRITEBATCHNODE IPHONE 4 (18FPS)
WHAT ABOUT A TEXTURE ATLAS?
1100 CCSPRITES - 1CCSPRITEBATCHNODE IPHONE 4 (40FPS)
CCSPRITES VS CCSPRITEBATCHNODE CPU Utilization for drawing sprites went down: 58% to 17% Frame rate doubled: 18FPS to 40FPS But...
SPRITE RE-USE Each time a debris CCSprite reached the ground it was removed (dealloc) and a new one was created (alloc) Why not just re-position it and reuse it?
1100 CCSPRITES - 1CCSPRITEBATCHNODE IPHONE 4 (60FPS)
CCSPRITEBATCHNODE AND REUSE CPU Utilization for drawing sprites went down: 58% to 17% to 0.3% Frame rate increase: 18FPS to 40FPS to 60 FPS
ONE MORE THING...
BATCH YOUR ACCELEROMETER DATA Store the data in an array, don t do anything else with it on the callback Process the accelerometer updates (array) on the -update call before each frame is rendered.
COMBINE LIKE TASKS Think about what you can calculate together instead of 1 at a time AI/Enemy Logic, Collisions? Can you partition and only calculate what is needed? Use ScheduleWithPriority to control update callback order
MORE DETAILS ON THE BLOG http://cocos2dbook.com
THANK YOU rod@prop.gr twitter.com/rodstrougo www.prop.gr www.cocos2dbook.com