12 ms·
I'm pretty sure that C++ compilers don't do this either. In C++ this is resolved by leaning on the "One Definition Rule" which authorizes the linker to assume a
by jey 6y ago
I'm pretty sure that C++ compilers don't do this either. In C++ this is resolved by leaning on the "One Definition Rule" which authorizes the linker to assume a symbol that's defined in multiple translation units is identical in all of them and so it can arbitrarily pick one of them to keep in the output.
- Karliss 6y agoODR and linker removing multiple copies of symbol with the same name and deduplicating function with different names but identical bodies are two different techniques for two different purpose. Modern C++ toolchains will do both. Linker choosing one of the copies for inline function is means of dealing with multiple definition problem, deduplicating identical functions is size optimization. Here an example GCC deduplicating can_zero_in_permitted_state and can_fast_zero_in_permitted_state by inserting jump from one to another https://godbolt.org/z/7xYrxG https://godbolt.org/z/7xYrxG. In the example static was removed as otherwise they would be removed completely.
- throwaway_pdp09 6y agoWith respect to all, I feel this is all missing the point. Expecting a compiler to remove dups just isn't sensible when you can do it yourself so easily on the generation side. Just drop the AST/equivalent data structure into a hash table, with a quick exists check before you add the next one. Those that match, turn that code into a sub-function which all the rest call.
- rwmj 6y agoI think we can all be right. A benefit of generating the code is that this deduplication is much easier than if all the code was hand-written (especially w.r.t keeping the code updated as new API functions get added).