Executable Bloat - How it happens and how we can fight it
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

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

on

  • 53,694 views

 

Statistics

Views

Total Views
53,694
Views on SlideShare
8,364
Embed Views
45,330

Actions

Likes
3
Downloads
68
Comments
0

21 Embeds 45,330

http://publications.dice.se 35837
http://www.frostbite.com 5079
http://dice.se 4053
http://www.frostbite.com.cluster.triggerfish.se 278
http://translate.googleusercontent.com 34
http://webcache.googleusercontent.com 17
http://twitter.com 9
http://web.archive.org 4
http://thesite.dice.se 3
http://www.frostbiteengine.com 3
http://131.253.14.98 2
https://schoolisgood.com 2
http://frostbiteengine.com 1
http://www.inoreader.com 1
http://cache.baidu.com 1
http://m.battlefield3gamer.com 1
https://vpn.dragonflygame.com 1
http://translate.yandex.net 1
http://www.google.com 1
https://twitter.com 1
http://feedly.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

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

  • 1. Executable Bloat How it happens and how we can fight it Andreas Fredriksson <dep@dice.se>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 factThursday, 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 gamesThursday, 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 questionThursday, 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 loadingThursday, 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 structThursday, 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 = bloatThursday, February 24, 2011
  • 8. Excessive Inlining • Typically associated with templates • Templates are almost always inline • Smart pointers • String classesThursday, 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 = bloatThursday, 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 casesThursday, 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 bytesThursday, 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 usedThursday, 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 altogetherThursday, 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 somethingThursday, February 24, 2011
  • 16. Part 2: What you can doThursday, 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 coveredThursday, 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 codeThursday, 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 skillsThursday, 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 jobsThursday, 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 strippingThursday, 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 thats just as global anyway • Prefer free functions around static dataThursday, 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: <dep@dice.se>Thursday, February 24, 2011