InformationResourcesCategories |
Ridding ourselves of the C++ template bloatPosted by Bender in Software at 08:39 | Tuesday, July 31. 2007Trackbacks
Trackback specific URI for this entry
No Trackbacks
Comments
Display comments as
(Linear | Threaded)
I don't know about GCC, but Microsoft's C compiler can perform what it calls "identical COMDAT folding". This looks through the generated object code and finds functions that are identical. It then merges them all together.
It sounds as though you're saying that this doesn't work, or perhaps you're saying that it doesn't work with another compiler? Avoiding ugly code like what you've got above seems like a worthwhile thing to do. If you can do it with a simple compiler switch, then we'd be much better off.
#1
on
2007-07-31 09:01
(That should say "Microsoft's C compiler".)
Here's a link to the docs: http://msdn2.microsoft.com/en-us/library/bxwfs976(vs.80).aspx It's the /OPT:ICF switch, but it should be on by default.
#2
on
2007-07-31 09:04
Fred's comment about identical COMDAT folding makes an important point. Some compiler / linker combinations are able to elimate duplicate code... sometimes. This isn't doable with all C objects and many times even data structures templated with plain old data (POD) data types cannot be "folded" into a unified set of code.
For example if you have a class called BigInteger which contains 64-bit integers and you also have a class called SmallInteger which contains 32-bit integers. The compiled binary code actually differs between an Array and Array. The described casting in the above article may be a good candidate for such a case (if you don't need to worry about byte alignments between elements of the array). The BigInteger/SmallInteger situation in the above paragraph makes even more sense when you are making a 64-big program and you want all your structures to be 64-bit aligned in memory, thus speeding up access. (Unaligned data can dramatically hurt performance) Hope you enjoyed the article... Look for a new article from me on eliminating C bloat within a few days!
#3
on
2007-07-31 12:46
Gcc (or rather ld) implements a similar mechanism via .linkonce directives. Multiple copies of the same code are emmited, and all but one is discarded at linktime.
very interesting, but I don't agree with you
Idetrorce
#5
on
2007-12-15 02:58
Only registered users may post comments here. Get your own account here and then log into this blog. Your browser must support cookies.
|
QuicksearchPollsDid you buy an iPhone 3G?
Archives ArchivesLogin |