Code Bloat Hunting

February 6, 2010 @ 1:39 | In Programming | 4 Comments | twitter rss digg
Image with lots of books

I have dedicated the last few days of work to face a problem that is getting bigger and bigger. Although we have a quite modular codebase, use interfaces whenever possible to hide implementation details and apply most of the recipes recommended in classic books, our linking times have started to be a problem in some of our libraries. Linking times is not the only problem. Our binary sizes are becoming really fat.

After a thoroughly study with the help of tools like Sizer, Symbol Sort (I strongly recommend reading the articles associated to this tool: 1, 2, 3, 4, 5, 6 and passing all them to your co-workers) we discovered lot of code bloating generated by improper template usage. The template is a powerful tool that can be easily misused although I am not against using it. We use template metaprogramming in lots of places like reflection, annotations, uri-like dependency properties, serialization and more places where it is worth it. The problem with templates is that normally declarations and definitions go in the same header file. With that structure a compiler is normally forced (although some compilers allow customization) to generate template instances in each obj file. Redundancy is eliminated in the linking phase, of course taking time. This is one of the faces of template bloating. The other face appears when you templatize functions when it is not strictly necessary. And you pay for it in the final size of your binaries.

Trying to solve all this, we applied a solution that although yet not standard seems to work in all compilers we tested: exporting explicit template instantiations. This technique applies when you have a template that is to be instantiated for only a few known types. Those instantiations are exported from a dll. This allows even hiding big functions in the .cpp without having to show them in the header. With this solution you have the best of both worlds: short functions can be inlined by the compiler and large functions instead of being instantiated in each compilation unit are referenced as an exported symbol. By the way, a special behavior of Visual Studio (to me, it is a bug) has the effect of non inlining functions being defined outside the class when exporting the template. We had to move our definitions (that we usually have pseudo hidden in a .inl file) to inside the class declaration.

We applied this strategy in several places reducing the bloatage by a considerable factor. For example, the vector library seems an ideal target because you normally only instantiate that library for a few types (float, double, int) and “big” functions like Invert() for 4×4 matrices can be hidden and exported from the dll. Another good candidate is std::string (instances for chat and wchar_t). Although, at least in MSVC, this job is already being done by the crt (not tested in other platforms).

I hope you find this useful. Thanks for reading and welcome to all the new subscribers since my last post!

  1. As always, interesting and practical post. I was remembering when I used boost to bind with python interpreter. What a big binary size !

    Comment by gyakoo
    February 8, 2010 @ 8:18 #

  2. I would really love to see some sample code of how you structured your template definitions (from vector for e.g.) to avoid code bloat. Your description leaves me wanting more. Thanks in advance

    Comment by Ganeshram Iyer
    February 9, 2010 @ 1:21 #

  3. Something like this for the header:

    template<class T> struct Vector2
    template struct __declspec(dllimport) Vector2<NsFloat32>;
    template struct __declspec(dllimport) Vector2<NsFloat64>;
    template struct __declspec(dllimport) Vector2<NsInt>;

    And for the implementation:

    template struct __declspec(dllexport) Vector2<NsFloat32>;
    template struct __declspec(dllexport) Vector2<NsFloat64>;
    template struct __declspec(dllexport) Vector2<NsInt>;

    I you want more detail do not hesitate to contact me in private…

    Comment by ent
    February 12, 2010 @ 2:26 #

  4. Thanks for the example. Is this what Vandevoorde describes as “explicit instantiation” in his book ( Thanks in advance for helping a newbie like me.

    Comment by Ganeshram Iyer
    February 12, 2010 @ 18:01 #

Sat, 28 Feb 2015 00:28:24 +0000 / 27 queries. 1.242 seconds / 5 Users Online

gentoo link wordpress link apache link PHP link

Theme modified from Pool theme. Valid XHTML and CSS