Pooling is magic, Максим Клыга
-
Upload
it-share -
Category
Technology
-
view
1.157 -
download
1
description
Transcript of Pooling is magic, Максим Клыга
![Page 1: Pooling is magic, Максим Клыга](https://reader033.fdocuments.net/reader033/viewer/2022052821/5549a318b4c9050c738b50ba/html5/thumbnails/1.jpg)
P O O L I N G I S M A G I CP R A C T I C A L R E S O U R C E M A N A G E M E N T F O R M O B I L E D E V I C E S
![Page 2: Pooling is magic, Максим Клыга](https://reader033.fdocuments.net/reader033/viewer/2022052821/5549a318b4c9050c738b50ba/html5/thumbnails/2.jpg)
W H AT D O W E K N O W A B O U T M O B I L E
![Page 3: Pooling is magic, Максим Клыга](https://reader033.fdocuments.net/reader033/viewer/2022052821/5549a318b4c9050c738b50ba/html5/thumbnails/3.jpg)
W H AT D O W E K N O W A B O U T M O B I L E
• Battery is limited
![Page 4: Pooling is magic, Максим Клыга](https://reader033.fdocuments.net/reader033/viewer/2022052821/5549a318b4c9050c738b50ba/html5/thumbnails/4.jpg)
W H AT D O W E K N O W A B O U T M O B I L E
• Battery is limited
• Processing power is limited
![Page 5: Pooling is magic, Максим Клыга](https://reader033.fdocuments.net/reader033/viewer/2022052821/5549a318b4c9050c738b50ba/html5/thumbnails/5.jpg)
W H AT D O W E K N O W A B O U T M O B I L E
• Battery is limited
• Processing power is limited
• Storage is limited
![Page 6: Pooling is magic, Максим Клыга](https://reader033.fdocuments.net/reader033/viewer/2022052821/5549a318b4c9050c738b50ba/html5/thumbnails/6.jpg)
W H AT D O W E K N O W A B O U T M O B I L E
• Battery is limited
• Processing power is limited
• Storage is limited
• RAM is limited
![Page 7: Pooling is magic, Максим Клыга](https://reader033.fdocuments.net/reader033/viewer/2022052821/5549a318b4c9050c738b50ba/html5/thumbnails/7.jpg)
W H AT D O W E K N O W A B O U T M O B I L E
• Battery is limited
• Processing power is limited
• Storage is limited
• RAM is limited
• Ubiquitous
![Page 8: Pooling is magic, Максим Клыга](https://reader033.fdocuments.net/reader033/viewer/2022052821/5549a318b4c9050c738b50ba/html5/thumbnails/8.jpg)
W H AT D O W E K N O W A B O U T G A M E S
![Page 9: Pooling is magic, Максим Клыга](https://reader033.fdocuments.net/reader033/viewer/2022052821/5549a318b4c9050c738b50ba/html5/thumbnails/9.jpg)
W H AT D O W E K N O W A B O U T G A M E S
• Consume all of the CPU
![Page 10: Pooling is magic, Максим Клыга](https://reader033.fdocuments.net/reader033/viewer/2022052821/5549a318b4c9050c738b50ba/html5/thumbnails/10.jpg)
W H AT D O W E K N O W A B O U T G A M E S
• Consume all of the CPU
• Consume all of the GPU
![Page 11: Pooling is magic, Максим Клыга](https://reader033.fdocuments.net/reader033/viewer/2022052821/5549a318b4c9050c738b50ba/html5/thumbnails/11.jpg)
W H AT D O W E K N O 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
![Page 12: Pooling is magic, Максим Клыга](https://reader033.fdocuments.net/reader033/viewer/2022052821/5549a318b4c9050c738b50ba/html5/thumbnails/12.jpg)
W H AT D O W E K N O 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
![Page 13: Pooling is magic, Максим Клыга](https://reader033.fdocuments.net/reader033/viewer/2022052821/5549a318b4c9050c738b50ba/html5/thumbnails/13.jpg)
W H AT D O W E K N O 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
![Page 14: Pooling is magic, Максим Клыга](https://reader033.fdocuments.net/reader033/viewer/2022052821/5549a318b4c9050c738b50ba/html5/thumbnails/14.jpg)
![Page 15: Pooling is magic, Максим Клыга](https://reader033.fdocuments.net/reader033/viewer/2022052821/5549a318b4c9050c738b50ba/html5/thumbnails/15.jpg)
R E D U C E B AT T E R Y 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
![Page 16: Pooling is magic, Максим Клыга](https://reader033.fdocuments.net/reader033/viewer/2022052821/5549a318b4c9050c738b50ba/html5/thumbnails/16.jpg)
R E D U C E A P P L 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
![Page 17: Pooling is magic, Максим Клыга](https://reader033.fdocuments.net/reader033/viewer/2022052821/5549a318b4c9050c738b50ba/html5/thumbnails/17.jpg)
G A M E S A R E D I F F E R E N T
![Page 18: Pooling is magic, Максим Клыга](https://reader033.fdocuments.net/reader033/viewer/2022052821/5549a318b4c9050c738b50ba/html5/thumbnails/18.jpg)
G A M E S A R E D I F F E R E N T
• A lot is known in advance:
• Usage patterns are known
• Data dependencies are known
• Asset restrictions are known
![Page 19: Pooling is magic, Максим Клыга](https://reader033.fdocuments.net/reader033/viewer/2022052821/5549a318b4c9050c738b50ba/html5/thumbnails/19.jpg)
U S E Y O U R K N O W L E D G E
![Page 20: Pooling is magic, Максим Клыга](https://reader033.fdocuments.net/reader033/viewer/2022052821/5549a318b4c9050c738b50ba/html5/thumbnails/20.jpg)
U S E Y O U R K N O W L E D G E
• Set restrictions for designers and artists
• Custom build process:
• Use available tools
• Write your own
![Page 21: Pooling is magic, Максим Клыга](https://reader033.fdocuments.net/reader033/viewer/2022052821/5549a318b4c9050c738b50ba/html5/thumbnails/21.jpg)
M E M O R Y A L L O C 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
![Page 22: Pooling is magic, Максим Клыга](https://reader033.fdocuments.net/reader033/viewer/2022052821/5549a318b4c9050c738b50ba/html5/thumbnails/22.jpg)
M E M O R Y A L L O C 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)
![Page 23: Pooling is magic, Максим Клыга](https://reader033.fdocuments.net/reader033/viewer/2022052821/5549a318b4c9050c738b50ba/html5/thumbnails/23.jpg)
G O T TA G O FA S T
• Linear allocator:
• Just increase the pointer and adjust for alignment
• As fast as it can get
• Free all memory at once (reset pointer)
![Page 24: Pooling is magic, Максим Клыга](https://reader033.fdocuments.net/reader033/viewer/2022052821/5549a318b4c9050c738b50ba/html5/thumbnails/24.jpg)
L I N E A R A L L O 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/)
![Page 25: Pooling is magic, Максим Клыга](https://reader033.fdocuments.net/reader033/viewer/2022052821/5549a318b4c9050c738b50ba/html5/thumbnails/25.jpg)
B U T W A I T, W H AT A B O U T
• Objects with undefined lifetime:
• Use object pools
• Objects with variable size:
• Custom allocator (several free-lists for different sizes)
![Page 26: Pooling is magic, Максим Клыга](https://reader033.fdocuments.net/reader033/viewer/2022052821/5549a318b4c9050c738b50ba/html5/thumbnails/26.jpg)
O B J E C T P O O L 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
![Page 27: Pooling is magic, Максим Клыга](https://reader033.fdocuments.net/reader033/viewer/2022052821/5549a318b4c9050c738b50ba/html5/thumbnails/27.jpg)
O B J E C T P O O L S
• Particle systems
• Components
• Game entities
• Sounds
• etc.
![Page 28: Pooling is magic, Максим Клыга](https://reader033.fdocuments.net/reader033/viewer/2022052821/5549a318b4c9050c738b50ba/html5/thumbnails/28.jpg)
R E S O U R C E L I 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
![Page 29: Pooling is magic, Максим Клыга](https://reader033.fdocuments.net/reader033/viewer/2022052821/5549a318b4c9050c738b50ba/html5/thumbnails/29.jpg)
R E S O U R C E L I 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
![Page 30: Pooling is magic, Максим Клыга](https://reader033.fdocuments.net/reader033/viewer/2022052821/5549a318b4c9050c738b50ba/html5/thumbnails/30.jpg)
R E D U C E M E M O R Y 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
![Page 31: Pooling is magic, Максим Клыга](https://reader033.fdocuments.net/reader033/viewer/2022052821/5549a318b4c9050c738b50ba/html5/thumbnails/31.jpg)
‘ P R O T O T Y P E ’ PAT T E R N
• Animations
• Game entities (Similar to prefabs in Unity)
• Anything that could share an immutable part between instances
![Page 32: Pooling is magic, Максим Клыга](https://reader033.fdocuments.net/reader033/viewer/2022052821/5549a318b4c9050c738b50ba/html5/thumbnails/32.jpg)
R E D U C E M E M O R Y 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
![Page 33: Pooling is magic, Максим Клыга](https://reader033.fdocuments.net/reader033/viewer/2022052821/5549a318b4c9050c738b50ba/html5/thumbnails/33.jpg)
• Always base your decisions on data
• USE PROFILER
• Don’t use this rules as a dogma, think for yourself
![Page 34: Pooling is magic, Максим Клыга](https://reader033.fdocuments.net/reader033/viewer/2022052821/5549a318b4c9050c738b50ba/html5/thumbnails/34.jpg)
S TA R T P R E A L L O C AT I N G A N D
S T O P W O R R Y I N G
![Page 35: Pooling is magic, Максим Клыга](https://reader033.fdocuments.net/reader033/viewer/2022052821/5549a318b4c9050c738b50ba/html5/thumbnails/35.jpg)
Q U E S T I O N S ?
M A X K LY G A
E M A I L : M A X . K LY G A @ G M A I L . C O M
T W I T T E R : @ N E K U 4 2