Sign in to follow this  
Aressera

Template Specialization Code Bloat

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
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 [i]really[/i] long build...
[quote name='ApochPiQ' timestamp='1310948016' post='4836548']
Don't worry about it.
[/quote]

Share this post


Link to post
Share on other sites
[quote name='mrbastard' timestamp='1311011388' post='4836904']
Template bloat can affect build times.
[/quote]

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
[quote name='wqking' timestamp='1311043979' post='4837159']
I've ever tested to include Boost Signals2 in my code, then the build became much much slower. :(
[/quote]That has little or nothing to do with code bloat.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this