Hacker News new | ask | show | jobs
by iainmerrick 2850 days ago
I haven’t checked but I wouldn’t be surprised if they are duplicated.

(Edit to add: if this done, it’ll be part of the link-time optimization step, which I think is relatively new in all the major C++ compilers.)

If you include debugging symbols, there will probably be multiple copies of the duplicated code just because it has different names.

You certainly should be able to merge duplicated functions after stripping symbols but I don’t know if that’s a standard thing. I’ve definitely been disappointed in the past by the performance of “dead code stripping” optimization. It’s not quite as trivial a problem as it sounds because when you have groups of functions that call each other, you need to unify two graphs rather than just look for individual identical blobs. The obvious implementation (multiple passes looking for identical leaf functions) is slow and will miss some cases.

2 comments

Debug symbols don’t inhibit identical code folding. What you get is one copy of the function and one arbitrary file/line entry which makes it look like your program makes impossible function calls, if you happen to walk the stack. This can easily be seen in a C++ program with lots of Protocol Buffers because protobuf generated code has lots of identical small functions.
> which I think is relatively new in all the major C++ compilers.

LTO has been available in gcc since 2009 and even longer in clang AFAIK. MSVC had LTO for... I don't know but googling a bit shows that visual studio 2005 supports /GL.

Yeah, sorry, I should have clarified “relatively recent” as meaning the last 10 years or so. That’s recent for C++! :)

This stuff is being actively worked on so I don’t know if merging identical template instantiations is a thing yet. Any idea?

MSVC has /OPT:ICF which does this. GCC has -fipa-icf, and the GNU Gold linker has --icf={safe,all} options (since it's operating on ASM it needn't be at the compiler level).

In both cases it does not only work with templates, but with any kind of function. However if you use --icf=all, some subtle bugs can appear since function pointers that would compare different in a default build would now compare equal. I have never been bitten by those.

Cool! I’ll give those a try.