W A B O U T G A M E S • Consume all of the CPU • Consume all of the GPU • Can consume a lot of storage space • Consume all of RAM • Thats what people do on mobile
RY C O N S U M P T I O N • Reduce your network activity • Every time cell radio is activated for 20-30 seconds • Batch requests • Prefetch as much data as possible • Maximize bandwidth usage • Reduce storage activity • Don’t keep threads busy for no reason
I C AT I O N S I Z E • Different assets for different device categories • Use a smaller runtime if available • Use texture atlases • Enable batch drawing • Less memory • Use compressed textures where possible • Use WebP for uncompressed images
AT I O N PAT T E R S • Application scope (allocated at app start, freed on shutdown) • Level scope (allocated at level start, freed on level end) • Frame scope (allocated for one frame, sometimes for two) • Temporary allocations
AT I O N PAT T E R N S • Grab all required memory once on app start - this is all you will have • Divide it between several heaps for different subsystems (avoids fragmentation, decoupling) • Use stack allocations where possible (alloca, structs, static arrays)
C AT O R • Can only free in reverse order (better free all at once at scope exit) • How do we deal with temporary allocations? • Separate heap for temporaries? • Nope! Double-ended linear allocator • What about cleanup? • Scope stack (http://dice.se/publications/scope-stack- allocation/)
B O U T • Objects with undefined lifetime: • Use object pools • Objects with variable size: • Custom allocator (several free-lists for different sizes)
S • Preallocate certain number of objects of same type • Use objects from pool whenever one needs an object of that type • Return objects to pool when done (resource released, reference count is zero) • Can process all the pool in one go • Better cache efficiency • Set restrictions on object count
F E T I M E • Use registers for resources of same type (fonts, textures, etc.) • Use reference counters only as last resort: • Thrash CPU cache • Unnecessary work • Unbounded pauses on deallocation cascades
F E T I M E • Don’t leave some resource hanging if it will be reused in some other level • Usually its ok to unload and load stuff again to avoid heap fragmentation
RY U S A G E • ‘Prototype’ pattern: • Extract some part to be shared by all objects • Usually used as a template for creating stuff • Best allocation is no allocation at all
RY U S A G E • Unity: • Use value types (structs, primitive types) • Avoid boxing/unboxing • Don’t use default GUI • Reuse buffers and objects where possible