Jump to content
  • Advertisement
Sign in to follow this  
Aressera

Template Specialization Code Bloat

This topic is 2554 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm in the process of writing a custom string class that has 4 different template specializations: one for ASCII characters, one for UTF-8, one for UTF-16 and one for UTF-32. I've got it mostly done and I'm starting to notice that the object file containing the different versions is about 100K per architecture (I'm compiling a universal binary under Xcode so the actual size is 4x that). I've defined most of the methods for the classes in a source file so that they are compiled and linked, rather than being inlined (which makes the bloat even worse) when they are used.

I've determined that the biggest source of the overhead (about 75% of the total size) is my custom number-to-string and back conversion methods that are templatized for character type and primitive type. I've considered doing all of the conversion in ASCII character space and then copying the results to whatever unicode type is needed, but I'm not sure if it is worth the allocation/copy overhead to get the savings in binary size.

So, my question is, should I be worried about the size of the compiled code enough to warrant the allocate/copy operation for unicode strings? I realize that once the file is linked into an executable the linker will discard the unused functions anyway.

Share this post


Link to post
Share on other sites
Advertisement
Don't worry about it.

Link-time code generation and COMDAT folding basically make template-generated code bloat a non-issue in modern compilers.

Share this post


Link to post
Share on other sites
Seconded, it is not worth worrying about, especially when developing for the PC.

Share this post


Link to post
Share on other sites
If you are sure your templates are instantiated for a lot of different type, it will help if you take care of "virtual function bloat".
If there is virtual function in your templates, you will end up get a lot of unnecessary RTTI information (compiler will generate RTTI for class with virtual function).
To avoid the RTTI, one approach is to build your own v-table using function pointers.

However, for 99.9% time, I really doubt you need to worry about the "virtual function bloat".

Share this post


Link to post
Share on other sites
You can apply the refactoring described at http://eugenedruy.wordpress.com/2009/07/19/refactoring-template-bloat/ to cut down some of that overhead.

Share this post


Link to post
Share on other sites
Template bloat can affect build times. If each of your obj files has many of the same template instantiations, they can get a lot bigger than they need to be.

That can be fixed with judicious use of extern template instantiations.

But unless you have a really long build...

Don't worry about it.

Share this post


Link to post
Share on other sites

Template bloat can affect build times.


Quite true.

I've ever tested to include Boost Signals2 in my code, then the build became much much slower. :(

Share this post


Link to post
Share on other sites

I've ever tested to include Boost Signals2 in my code, then the build became much much slower. :(
That has little or nothing to do with code bloat.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!