SlideShare a Scribd company logo
Executable Bloat
                              How it happens and how we can fight it

                                   Andreas Fredriksson <dep@dice.se>




Thursday, February 24, 2011
Background
                    • Why is ELF/EXE size a problem?
                     • Executable code takes up memory
                     • Consoles have precious little RAM
                     • More available memory enables richer
                                gameplay
                              • It is a lot of work to fix after the fact
Thursday, February 24, 2011
Software Bloat
                                 Software bloat is a term used to describe the
                                 tendency of newer computer programs to have a
                                 larger installation footprint, or have many
                                 unnecessary features that are not used by end
                                 users, or just generally use more system resources
                                 than necessary, while offering little or no benefit
                                 to its users.



                    • Wikipedia has this definition (emphasis mine)
                    • We are wasting memory we could be using
                              to make better games



Thursday, February 24, 2011
C++ Bloat

                    • Let’s look at a few typical C++ techniques
                     • Some are even enforced in coding
                                standards
                              • Often recommended in literature
                              • Often accepted without question

Thursday, February 24, 2011
Interface Class Hiding
                    • Hiding single class Foo         struct IFoo {
                                                         virtual void func() = 0;
                              behind interface IFoo   };



                              • Intent is to avoid    class Foo : public IFoo {

                                                      };
                                                         virtual void func();

                                including heavy Foo
                                header everywhere
                              • Sometimes used for
                                PRX/DLL loading


Thursday, February 24, 2011
Interface Class Hiding
                              struct IFoo {                      0              IFoo vtbl
                                 virtual void a() = 0;           typeinfo_ptr
                                 virtual void b() = 0;           pure_vcall
                              };                                 pure_vcall
                              class Foo : public IFoo {
                                 virtual void a();
                                 virtual void b();               0
                                                                                 Foo vtbl
                              };                                 typeinfo_ptr
                                                                 Foo_a
                                                                 Foo_b


                    •         One hidden cost - virtual tables
                              •   One for each class
                              •   In PPU ABI cost is higher as Foo_a will be a
                                  pointer to a 4+4 byte ABI struct


Thursday, February 24, 2011
Interface Class Hiding
                    • Additional overhead
                     • Every call site needs vcall adjustment
                     • ctor/dtor needs vptr adjustment
                    • Total SNC overhead, 8 functions: 528 bytes
                     • Likely more, due to callsites
                     • These bytes have no value = bloat
Thursday, February 24, 2011
Excessive Inlining

                    • Typically associated with templates
                     • Templates are almost always inline
                     • Smart pointers
                     • String classes

Thursday, February 24, 2011
Excessive Inlining
                              bool eastl_string_find(eastl::string& str) {
                              !   return str.find("foo") != eastl::string::npos;
                              }
                              bool c_string_find(const char* str) {
                              !   return strstr(str, "foo") != 0;
                              }



                    • Compare two string searches
                     • c_string_find version - 56 bytes (SNC)
                     • eastl_string_find - 504 bytes (SNC)
                    • These bytes add zero value = bloat
Thursday, February 24, 2011
Excessive Inlining
                    • Many other common inlining culprits
                     • operator+= string concatenation
                     • Call by value w/ inline ctor
                    • Hard to control via compiler options
                     • Sometimes you want inlining
                     • Better to avoid pointless cases
Thursday, February 24, 2011
Static Initialization
                              static const eastl::string strings[] = { "foo0", "foo1" };
                              static const Vec3 vecs[] = { { 0.2, 0.3, 0.5 }, { ... } };




                    •         Extreme hidden costs for constructors
                    •         Generates initializer function
                    •         Have seen arrays of this kind generate over 20
                              kb of initializer code in the wild (SNC)
                              •   Array of 10 eastl::strings - 1292 bytes
                              •   Array of 10 vec3 - 368 bytes


Thursday, February 24, 2011
Static Initialization
                              static const char* strings[] = { "foo0", "foo1", ... };
                              static const float vecs[][3] = { { 0.2, 0.3, 0.5 }, ... };




                    • Just don’t do it - prefer POD types
                      • Make sure data ends up in .rodata segment
                      • Adjust code using array accordingly
                    • Alternatively make data load with level
                      • No space overhead when not used
Thursday, February 24, 2011
operator<<
                    • A special case of excessive inlining
                    • Creeps up in formatted I/O
                      • Assert macros
                    • Prefer snprintf()-style APIs if you must
                              format text at runtime
                              • Usually less than half the overhead
                              • Ideally avoid text formatting altogether
Thursday, February 24, 2011
Sorting
                    • STL sort is a bloat generator
                      • Specialized for each type - faster
                                compares..
                              • ..but usually whole merge/quicksort
                                duplicated per parameter type! - often
                                around 1-2kb code
                    • We have 140 sort calls in the code base - up
                              to 280 kb overhead..


Thursday, February 24, 2011
Sorting
                    • Prefer qsort() for small/medium datasets
                     • Adds callback overhead on PPU..
                     • Rule of thumb - qsort < 32-64 elements
                    • Same applies to many other template
                              algorithms
                              • Use only when it really buys something
Thursday, February 24, 2011
Part 2:
                              What you can do



Thursday, February 24, 2011
Accept the Domain
                    •         Console coding is a very specific problem space
                              •   Think and verify before you apply general
                                  desktop C++ advice or patterns
                              •   Bloat is caused by humans, not compilers
                    •         Example: "Virtual functions are essentially free"
                              •   True on x86 architecture (most of the time)
                              •   On PS3 PPU often two cache misses - ~1200
                                  cycle penalty + ELF size bloat already covered


Thursday, February 24, 2011
Day to day
                    • Think about full impact of your changes
                     • .text size impact
                     • .data/.rodata size impact
                     • Bring it up & discuss in code reviews
                    • Make sure your code is reasonably sized for
                              the problem it solves!


Thursday, February 24, 2011
Avoid “reuse” bloat
                    •         YAGNI - “You ain’t gonna need it”
                              •   Just write simple code that can be extended if
                                  needed
                              •   We typically never extend systems without
                                  altering their interfaces anyway
                    •         Game code is disposable, a means to an end
                              •   Make sure it works well NOW
                              •   Avoid “single-feature frameworks”


Thursday, February 24, 2011
Avoid repetition
                    •         Can often move repeated code to data
                              •   Higher information density but same end result
                          RegisterFunc("foo", func_foo);
                          RegisterFunc("bar", func_bar);
                          RegisterFunc("baz", func_baz);
                          // ...

                         static const struct {
                             const char *name;   void (*func)(void);
                         } opdata[] = {
                         !   { "foo", func_foo   },
                         !   { "bar", func_bar   },
                         !   { "baz", func_baz   },
                             // ...
                         };

                         for (int i=0; i < sizeof_array(opdata); ++i)
                             RegisterFunc(opdata[i].name, opdata[i].func);




Thursday, February 24, 2011
Compiler Output
                    • Look at the generated code!
                      • That’s what you’re checking in, not C++
                      • Don’t assume code is improved by the
                                compiler
                              • No magic going on, compilers are stupid
                    • Develop an intuition for what to expect
                      • Verify assumptions as you code
Thursday, February 24, 2011
Assembly
                    • Learn enough assembly to read compiler
                              output
                              • Function calls (calling convention)
                              • Memory loads and stores
                              • FP/Vector instructions
                    • It’s not very difficult - just do it
                      • Also improves your debugging skills
Thursday, February 24, 2011
Guidelines
                    • Avoid string classes, concatenation
                     • Excessive inlining
                    • Avoid template containers for simple
                              problems
                              • Inlining + instantiation cost
                              • Prefer C arrays for most jobs
Thursday, February 24, 2011
Guidelines
                    • Avoid complex types in function signatures
                              and interfaces
                               • Requires caller to jump through hoops
                               • Often bloats all call sites
                               • Prefer raw POD types
                                • (T* ptr, int count) is better
                                    than (std::vector<T>&)


Thursday, February 24, 2011
Guidelines
                    • Avoid inheritance, interfaces and virtual
                              functions
                              • Hidden costs are subtle
                              • Prefer function pointers for callbacks
                              • Prefer free functions on predeclared
                                types for header stripping


Thursday, February 24, 2011
Guidelines
                    •         Avoid operator<< streaming
                              •   Prefer printf() style APIs
                              •   Easy to make your own formatter for often-
                                  used types
                    •         Avoid singletons
                              •   They just add bloat around data that's just as
                                  global anyway
                              •   Prefer free functions around static data


Thursday, February 24, 2011
Summary
                    • Make sure the code/data you’re adding is
                              reasonably sized for the problem it solves
                              • Use no more than necessary
                    • Pick up some assembly and look at the
                              compiler output
                    • Always measure, examine & question!

Thursday, February 24, 2011
Questions?
                              Twitter: @deplinenoise
                               Email: <dep@dice.se>




Thursday, February 24, 2011

More Related Content

What's hot (20)

PDF
Rendering AAA-Quality Characters of Project A1
Ki Hyunwoo
 
PDF
More explosions, more chaos, and definitely more blowing stuff up
Intel® Software
 
PDF
Lighting Shading by John Hable
Naughty Dog
 
PPTX
Parallel Futures of a Game Engine (v2.0)
repii
 
PPTX
Terrain in Battlefield 3: A Modern, Complete and Scalable System
Electronic Arts / DICE
 
PDF
Taking Killzone Shadow Fall Image Quality Into The Next Generation
Guerrilla
 
PPTX
Optimizing the Graphics Pipeline with Compute, GDC 2016
Graham Wihlidal
 
PDF
The Rendering Technology of 'Lords of the Fallen' (Game Connection Europe 2014)
Philip Hammer
 
PPTX
Shiny PC Graphics in Battlefield 3
Electronic Arts / DICE
 
PPTX
Triangle Visibility buffer
Wolfgang Engel
 
PPT
Z Buffer Optimizations
pjcozzi
 
PDF
Cascade Shadow Mapping
Sukwoo Lee
 
PPTX
Rendering Technologies from Crysis 3 (GDC 2013)
Tiago Sousa
 
PDF
Screen Space Reflections in The Surge
Michele Giacalone
 
PDF
Efficient Rendering with DirectX* 12 on Intel® Graphics
Gael Hofemeier
 
PPT
Secrets of CryENGINE 3 Graphics Technology
Tiago Sousa
 
PPT
Frostbite Rendering Architecture and Real-time Procedural Shading & Texturing...
repii
 
PPTX
Hable John Uncharted2 Hdr Lighting
ozlael ozlael
 
PPT
Light prepass
changehee lee
 
PPT
A Bit More Deferred Cry Engine3
guest11b095
 
Rendering AAA-Quality Characters of Project A1
Ki Hyunwoo
 
More explosions, more chaos, and definitely more blowing stuff up
Intel® Software
 
Lighting Shading by John Hable
Naughty Dog
 
Parallel Futures of a Game Engine (v2.0)
repii
 
Terrain in Battlefield 3: A Modern, Complete and Scalable System
Electronic Arts / DICE
 
Taking Killzone Shadow Fall Image Quality Into The Next Generation
Guerrilla
 
Optimizing the Graphics Pipeline with Compute, GDC 2016
Graham Wihlidal
 
The Rendering Technology of 'Lords of the Fallen' (Game Connection Europe 2014)
Philip Hammer
 
Shiny PC Graphics in Battlefield 3
Electronic Arts / DICE
 
Triangle Visibility buffer
Wolfgang Engel
 
Z Buffer Optimizations
pjcozzi
 
Cascade Shadow Mapping
Sukwoo Lee
 
Rendering Technologies from Crysis 3 (GDC 2013)
Tiago Sousa
 
Screen Space Reflections in The Surge
Michele Giacalone
 
Efficient Rendering with DirectX* 12 on Intel® Graphics
Gael Hofemeier
 
Secrets of CryENGINE 3 Graphics Technology
Tiago Sousa
 
Frostbite Rendering Architecture and Real-time Procedural Shading & Texturing...
repii
 
Hable John Uncharted2 Hdr Lighting
ozlael ozlael
 
Light prepass
changehee lee
 
A Bit More Deferred Cry Engine3
guest11b095
 

Viewers also liked (20)

PPTX
FrameGraph: Extensible Rendering Architecture in Frostbite
Electronic Arts / DICE
 
PPTX
Scope Stack Allocation
Electronic Arts / DICE
 
PPTX
Parallel Futures of a Game Engine
repii
 
PPT
Introduction to Data Oriented Design
Electronic Arts / DICE
 
PPTX
DirectX 11 Rendering in Battlefield 3
Electronic Arts / DICE
 
PPTX
4K Checkerboard in Battlefield 1 and Mass Effect Andromeda
Electronic Arts / DICE
 
KEY
Colin Barre-Brisebois - GDC 2011 - Approximating Translucency for a Fast, Che...
Colin Barré-Brisebois
 
PPS
Audio for Multiplayer & Beyond - Mixing Case Studies From Battlefield: Bad Co...
Electronic Arts / DICE
 
PPTX
A Real-time Radiosity Architecture
Electronic Arts / DICE
 
PPT
5 Major Challenges in Interactive Rendering
Electronic Arts / DICE
 
PPT
Destruction Masking in Frostbite 2 using Volume Distance Fields
Electronic Arts / DICE
 
PPT
The Intersection of Game Engines & GPUs: Current & Future (Graphics Hardware ...
repii
 
PPT
Bending the Graphics Pipeline
Electronic Arts / DICE
 
PPTX
Shadows & Decals: D3D10 Techniques in Frostbite (GDC'09)
repii
 
PPTX
How High Dynamic Range Audio Makes Battlefield: Bad Company Go BOOM
Anders Clerwall
 
PPTX
Parallel Graphics in Frostbite - Current & Future (Siggraph 2009)
repii
 
PPTX
Mantle for Developers
Electronic Arts / DICE
 
PPT
Stable SSAO in Battlefield 3 with Selective Temporal Filtering
Electronic Arts / DICE
 
PPTX
5 Major Challenges in Real-time Rendering (2012)
Electronic Arts / DICE
 
PPTX
Rendering Battlefield 4 with Mantle
Electronic Arts / DICE
 
FrameGraph: Extensible Rendering Architecture in Frostbite
Electronic Arts / DICE
 
Scope Stack Allocation
Electronic Arts / DICE
 
Parallel Futures of a Game Engine
repii
 
Introduction to Data Oriented Design
Electronic Arts / DICE
 
DirectX 11 Rendering in Battlefield 3
Electronic Arts / DICE
 
4K Checkerboard in Battlefield 1 and Mass Effect Andromeda
Electronic Arts / DICE
 
Colin Barre-Brisebois - GDC 2011 - Approximating Translucency for a Fast, Che...
Colin Barré-Brisebois
 
Audio for Multiplayer & Beyond - Mixing Case Studies From Battlefield: Bad Co...
Electronic Arts / DICE
 
A Real-time Radiosity Architecture
Electronic Arts / DICE
 
5 Major Challenges in Interactive Rendering
Electronic Arts / DICE
 
Destruction Masking in Frostbite 2 using Volume Distance Fields
Electronic Arts / DICE
 
The Intersection of Game Engines & GPUs: Current & Future (Graphics Hardware ...
repii
 
Bending the Graphics Pipeline
Electronic Arts / DICE
 
Shadows & Decals: D3D10 Techniques in Frostbite (GDC'09)
repii
 
How High Dynamic Range Audio Makes Battlefield: Bad Company Go BOOM
Anders Clerwall
 
Parallel Graphics in Frostbite - Current & Future (Siggraph 2009)
repii
 
Mantle for Developers
Electronic Arts / DICE
 
Stable SSAO in Battlefield 3 with Selective Temporal Filtering
Electronic Arts / DICE
 
5 Major Challenges in Real-time Rendering (2012)
Electronic Arts / DICE
 
Rendering Battlefield 4 with Mantle
Electronic Arts / DICE
 
Ad

Similar to Executable Bloat - How it happens and how we can fight it (20)

PDF
Strategies to improve embedded Linux application performance beyond ordinary ...
André Oriani
 
PPTX
C++11
Andrey Dankevich
 
KEY
Mysterious c++
xprayc
 
PDF
AMI4CCM_IDL2CPP
Remedy IT
 
PDF
개발 과정 최적화 하기 내부툴로 더욱 강력한 개발하기 Stephen kennedy _(11시40분_103호)
changehee lee
 
PPTX
Using the Windows 8 Runtime from C++
Microsoft Developer Network (MSDN) - Belgium and Luxembourg
 
PDF
Oop06 6
schwaa
 
PDF
Industry - Program analysis and verification - Type-preserving Heap Profiler ...
ICSM 2011
 
DOC
C++ Online Training
Srihitha Technologies
 
PDF
PVS-Studio
PVS-Studio
 
PPT
Course1
Constantin Nicolae
 
PDF
Operating Systems - Dynamic Memory Management
Emery Berger
 
PPTX
Silicon Valley Code Camp - Do you C what I C
Embarcadero Technologies
 
PDF
Object-oriented Design: Polymorphism via Inheritance (vs. Delegation)
Adair Dingle
 
PDF
Virtual Method Table and accident prevention
Andrey Karpov
 
PDF
Errors detected in the Visual C++ 2012 libraries
PVS-Studio
 
PPTX
DConf 2016: Bitpacking Like a Madman by Amaury Sechet
Andrei Alexandrescu
 
PDF
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
Sang Don Kim
 
PPTX
Whats New in Visual Studio 2012 for C++ Developers
Rainer Stropek
 
PDF
C++ CoreHard Autumn 2018. Debug C++ Without Running - Anastasia Kazakova
corehard_by
 
Strategies to improve embedded Linux application performance beyond ordinary ...
André Oriani
 
Mysterious c++
xprayc
 
AMI4CCM_IDL2CPP
Remedy IT
 
개발 과정 최적화 하기 내부툴로 더욱 강력한 개발하기 Stephen kennedy _(11시40분_103호)
changehee lee
 
Oop06 6
schwaa
 
Industry - Program analysis and verification - Type-preserving Heap Profiler ...
ICSM 2011
 
C++ Online Training
Srihitha Technologies
 
PVS-Studio
PVS-Studio
 
Operating Systems - Dynamic Memory Management
Emery Berger
 
Silicon Valley Code Camp - Do you C what I C
Embarcadero Technologies
 
Object-oriented Design: Polymorphism via Inheritance (vs. Delegation)
Adair Dingle
 
Virtual Method Table and accident prevention
Andrey Karpov
 
Errors detected in the Visual C++ 2012 libraries
PVS-Studio
 
DConf 2016: Bitpacking Like a Madman by Amaury Sechet
Andrei Alexandrescu
 
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
Sang Don Kim
 
Whats New in Visual Studio 2012 for C++ Developers
Rainer Stropek
 
C++ CoreHard Autumn 2018. Debug C++ Without Running - Anastasia Kazakova
corehard_by
 
Ad

More from Electronic Arts / DICE (20)

PPTX
GDC2019 - SEED - Towards Deep Generative Models in Game Development
Electronic Arts / DICE
 
PPT
SIGGRAPH 2010 - Style and Gameplay in the Mirror's Edge
Electronic Arts / DICE
 
PDF
SEED - Halcyon Architecture
Electronic Arts / DICE
 
PDF
Syysgraph 2018 - Modern Graphics Abstractions & Real-Time Ray Tracing
Electronic Arts / DICE
 
PPTX
Khronos Munich 2018 - Halcyon and Vulkan
Electronic Arts / DICE
 
PDF
CEDEC 2018 - Towards Effortless Photorealism Through Real-Time Raytracing
Electronic Arts / DICE
 
PPTX
CEDEC 2018 - Functional Symbiosis of Art Direction and Proceduralism
Electronic Arts / DICE
 
PPTX
SIGGRAPH 2018 - PICA PICA and NVIDIA Turing
Electronic Arts / DICE
 
PPTX
SIGGRAPH 2018 - Full Rays Ahead! From Raster to Real-Time Raytracing
Electronic Arts / DICE
 
PPTX
HPG 2018 - Game Ray Tracing: State-of-the-Art and Open Problems
Electronic Arts / DICE
 
PDF
EPC 2018 - SEED - Exploring The Collaboration Between Proceduralism & Deep Le...
Electronic Arts / DICE
 
PDF
DD18 - SEED - Raytracing in Hybrid Real-Time Rendering
Electronic Arts / DICE
 
PDF
Creativity of Rules and Patterns: Designing Procedural Systems
Electronic Arts / DICE
 
PPTX
Shiny Pixels and Beyond: Real-Time Raytracing at SEED
Electronic Arts / DICE
 
PPTX
Future Directions for Compute-for-Graphics
Electronic Arts / DICE
 
PPTX
A Certain Slant of Light - Past, Present and Future Challenges of Global Illu...
Electronic Arts / DICE
 
PPTX
Physically Based Sky, Atmosphere and Cloud Rendering in Frostbite
Electronic Arts / DICE
 
PPTX
High Dynamic Range color grading and display in Frostbite
Electronic Arts / DICE
 
PPTX
Lighting the City of Glass
Electronic Arts / DICE
 
PPTX
Photogrammetry and Star Wars Battlefront
Electronic Arts / DICE
 
GDC2019 - SEED - Towards Deep Generative Models in Game Development
Electronic Arts / DICE
 
SIGGRAPH 2010 - Style and Gameplay in the Mirror's Edge
Electronic Arts / DICE
 
SEED - Halcyon Architecture
Electronic Arts / DICE
 
Syysgraph 2018 - Modern Graphics Abstractions & Real-Time Ray Tracing
Electronic Arts / DICE
 
Khronos Munich 2018 - Halcyon and Vulkan
Electronic Arts / DICE
 
CEDEC 2018 - Towards Effortless Photorealism Through Real-Time Raytracing
Electronic Arts / DICE
 
CEDEC 2018 - Functional Symbiosis of Art Direction and Proceduralism
Electronic Arts / DICE
 
SIGGRAPH 2018 - PICA PICA and NVIDIA Turing
Electronic Arts / DICE
 
SIGGRAPH 2018 - Full Rays Ahead! From Raster to Real-Time Raytracing
Electronic Arts / DICE
 
HPG 2018 - Game Ray Tracing: State-of-the-Art and Open Problems
Electronic Arts / DICE
 
EPC 2018 - SEED - Exploring The Collaboration Between Proceduralism & Deep Le...
Electronic Arts / DICE
 
DD18 - SEED - Raytracing in Hybrid Real-Time Rendering
Electronic Arts / DICE
 
Creativity of Rules and Patterns: Designing Procedural Systems
Electronic Arts / DICE
 
Shiny Pixels and Beyond: Real-Time Raytracing at SEED
Electronic Arts / DICE
 
Future Directions for Compute-for-Graphics
Electronic Arts / DICE
 
A Certain Slant of Light - Past, Present and Future Challenges of Global Illu...
Electronic Arts / DICE
 
Physically Based Sky, Atmosphere and Cloud Rendering in Frostbite
Electronic Arts / DICE
 
High Dynamic Range color grading and display in Frostbite
Electronic Arts / DICE
 
Lighting the City of Glass
Electronic Arts / DICE
 
Photogrammetry and Star Wars Battlefront
Electronic Arts / DICE
 

Executable Bloat - How it happens and how we can fight it

  • 1. Executable Bloat How it happens and how we can fight it Andreas Fredriksson <[email protected]> Thursday, February 24, 2011
  • 2. Background • Why is ELF/EXE size a problem? • Executable code takes up memory • Consoles have precious little RAM • More available memory enables richer gameplay • It is a lot of work to fix after the fact Thursday, February 24, 2011
  • 3. Software Bloat Software bloat is a term used to describe the tendency of newer computer programs to have a larger installation footprint, or have many unnecessary features that are not used by end users, or just generally use more system resources than necessary, while offering little or no benefit to its users. • Wikipedia has this definition (emphasis mine) • We are wasting memory we could be using to make better games Thursday, February 24, 2011
  • 4. C++ Bloat • Let’s look at a few typical C++ techniques • Some are even enforced in coding standards • Often recommended in literature • Often accepted without question Thursday, February 24, 2011
  • 5. Interface Class Hiding • Hiding single class Foo struct IFoo { virtual void func() = 0; behind interface IFoo }; • Intent is to avoid class Foo : public IFoo { }; virtual void func(); including heavy Foo header everywhere • Sometimes used for PRX/DLL loading Thursday, February 24, 2011
  • 6. Interface Class Hiding struct IFoo { 0 IFoo vtbl virtual void a() = 0; typeinfo_ptr virtual void b() = 0; pure_vcall }; pure_vcall class Foo : public IFoo { virtual void a(); virtual void b(); 0 Foo vtbl }; typeinfo_ptr Foo_a Foo_b • One hidden cost - virtual tables • One for each class • In PPU ABI cost is higher as Foo_a will be a pointer to a 4+4 byte ABI struct Thursday, February 24, 2011
  • 7. Interface Class Hiding • Additional overhead • Every call site needs vcall adjustment • ctor/dtor needs vptr adjustment • Total SNC overhead, 8 functions: 528 bytes • Likely more, due to callsites • These bytes have no value = bloat Thursday, February 24, 2011
  • 8. Excessive Inlining • Typically associated with templates • Templates are almost always inline • Smart pointers • String classes Thursday, February 24, 2011
  • 9. Excessive Inlining bool eastl_string_find(eastl::string& str) { ! return str.find("foo") != eastl::string::npos; } bool c_string_find(const char* str) { ! return strstr(str, "foo") != 0; } • Compare two string searches • c_string_find version - 56 bytes (SNC) • eastl_string_find - 504 bytes (SNC) • These bytes add zero value = bloat Thursday, February 24, 2011
  • 10. Excessive Inlining • Many other common inlining culprits • operator+= string concatenation • Call by value w/ inline ctor • Hard to control via compiler options • Sometimes you want inlining • Better to avoid pointless cases Thursday, February 24, 2011
  • 11. Static Initialization static const eastl::string strings[] = { "foo0", "foo1" }; static const Vec3 vecs[] = { { 0.2, 0.3, 0.5 }, { ... } }; • Extreme hidden costs for constructors • Generates initializer function • Have seen arrays of this kind generate over 20 kb of initializer code in the wild (SNC) • Array of 10 eastl::strings - 1292 bytes • Array of 10 vec3 - 368 bytes Thursday, February 24, 2011
  • 12. Static Initialization static const char* strings[] = { "foo0", "foo1", ... }; static const float vecs[][3] = { { 0.2, 0.3, 0.5 }, ... }; • Just don’t do it - prefer POD types • Make sure data ends up in .rodata segment • Adjust code using array accordingly • Alternatively make data load with level • No space overhead when not used Thursday, February 24, 2011
  • 13. operator<< • A special case of excessive inlining • Creeps up in formatted I/O • Assert macros • Prefer snprintf()-style APIs if you must format text at runtime • Usually less than half the overhead • Ideally avoid text formatting altogether Thursday, February 24, 2011
  • 14. Sorting • STL sort is a bloat generator • Specialized for each type - faster compares.. • ..but usually whole merge/quicksort duplicated per parameter type! - often around 1-2kb code • We have 140 sort calls in the code base - up to 280 kb overhead.. Thursday, February 24, 2011
  • 15. Sorting • Prefer qsort() for small/medium datasets • Adds callback overhead on PPU.. • Rule of thumb - qsort < 32-64 elements • Same applies to many other template algorithms • Use only when it really buys something Thursday, February 24, 2011
  • 16. Part 2: What you can do Thursday, February 24, 2011
  • 17. Accept the Domain • Console coding is a very specific problem space • Think and verify before you apply general desktop C++ advice or patterns • Bloat is caused by humans, not compilers • Example: "Virtual functions are essentially free" • True on x86 architecture (most of the time) • On PS3 PPU often two cache misses - ~1200 cycle penalty + ELF size bloat already covered Thursday, February 24, 2011
  • 18. Day to day • Think about full impact of your changes • .text size impact • .data/.rodata size impact • Bring it up & discuss in code reviews • Make sure your code is reasonably sized for the problem it solves! Thursday, February 24, 2011
  • 19. Avoid “reuse” bloat • YAGNI - “You ain’t gonna need it” • Just write simple code that can be extended if needed • We typically never extend systems without altering their interfaces anyway • Game code is disposable, a means to an end • Make sure it works well NOW • Avoid “single-feature frameworks” Thursday, February 24, 2011
  • 20. Avoid repetition • Can often move repeated code to data • Higher information density but same end result RegisterFunc("foo", func_foo); RegisterFunc("bar", func_bar); RegisterFunc("baz", func_baz); // ... static const struct { const char *name; void (*func)(void); } opdata[] = { ! { "foo", func_foo }, ! { "bar", func_bar }, ! { "baz", func_baz }, // ... }; for (int i=0; i < sizeof_array(opdata); ++i) RegisterFunc(opdata[i].name, opdata[i].func); Thursday, February 24, 2011
  • 21. Compiler Output • Look at the generated code! • That’s what you’re checking in, not C++ • Don’t assume code is improved by the compiler • No magic going on, compilers are stupid • Develop an intuition for what to expect • Verify assumptions as you code Thursday, February 24, 2011
  • 22. Assembly • Learn enough assembly to read compiler output • Function calls (calling convention) • Memory loads and stores • FP/Vector instructions • It’s not very difficult - just do it • Also improves your debugging skills Thursday, February 24, 2011
  • 23. Guidelines • Avoid string classes, concatenation • Excessive inlining • Avoid template containers for simple problems • Inlining + instantiation cost • Prefer C arrays for most jobs Thursday, February 24, 2011
  • 24. Guidelines • Avoid complex types in function signatures and interfaces • Requires caller to jump through hoops • Often bloats all call sites • Prefer raw POD types • (T* ptr, int count) is better than (std::vector<T>&) Thursday, February 24, 2011
  • 25. Guidelines • Avoid inheritance, interfaces and virtual functions • Hidden costs are subtle • Prefer function pointers for callbacks • Prefer free functions on predeclared types for header stripping Thursday, February 24, 2011
  • 26. Guidelines • Avoid operator<< streaming • Prefer printf() style APIs • Easy to make your own formatter for often- used types • Avoid singletons • They just add bloat around data that's just as global anyway • Prefer free functions around static data Thursday, February 24, 2011
  • 27. Summary • Make sure the code/data you’re adding is reasonably sized for the problem it solves • Use no more than necessary • Pick up some assembly and look at the compiler output • Always measure, examine & question! Thursday, February 24, 2011
  • 28. Questions? Twitter: @deplinenoise Email: <[email protected]> Thursday, February 24, 2011