链接器通常会优化掉来自不同 C++ 模板实例的重复代码吗?
Does the linker usually optimize away duplicated code from different c++ template instances?
我使用 C++ 编程已经有一段时间了,我非常喜欢使用模板。由于我涉足嵌入式编程,我最近一直在想的是,人们应该如何期望 linker 在模板参数不同的模板实例中的代码重复方面表现。
对于具有相同参数的同一模板的多个实例,众所周知,这会在 link 时间内被优化掉(另请参阅:How does C++ link template instances)
但是在我的例子中,我感兴趣的是 linker 是否会识别两个使用不同参数实例化的模板之间的任何重复代码。由于它们是不同的类型,我假设它们不会自动折叠。然而,由于它们可能有一些不依赖于模板参数的功能,因此两者之间是相同的 类 人们可能会假设 link er 可以优化那些,从而节省 space.
在这种情况下预期的行为是什么?
gold 链接器正是这样做的。
Safe ICF: Pointer Safe and Unwinding Aware Identical Code Folding in Gold:
We have found that large C++ applications and shared libraries tend to have many functions whose code is identical with another function. As much as 10% of the code could theoretically be eliminated by merging such identical functions into a single copy. This optimization, Identical Code Folding (ICF), has been implemented in the gold linker. At link time, ICF detects functions with identical object code and merges them into a single copy.
我使用 C++ 编程已经有一段时间了,我非常喜欢使用模板。由于我涉足嵌入式编程,我最近一直在想的是,人们应该如何期望 linker 在模板参数不同的模板实例中的代码重复方面表现。
对于具有相同参数的同一模板的多个实例,众所周知,这会在 link 时间内被优化掉(另请参阅:How does C++ link template instances)
但是在我的例子中,我感兴趣的是 linker 是否会识别两个使用不同参数实例化的模板之间的任何重复代码。由于它们是不同的类型,我假设它们不会自动折叠。然而,由于它们可能有一些不依赖于模板参数的功能,因此两者之间是相同的 类 人们可能会假设 link er 可以优化那些,从而节省 space.
在这种情况下预期的行为是什么?
gold 链接器正是这样做的。
Safe ICF: Pointer Safe and Unwinding Aware Identical Code Folding in Gold:
We have found that large C++ applications and shared libraries tend to have many functions whose code is identical with another function. As much as 10% of the code could theoretically be eliminated by merging such identical functions into a single copy. This optimization, Identical Code Folding (ICF), has been implemented in the gold linker. At link time, ICF detects functions with identical object code and merges them into a single copy.