dlopen vs链接开销 [英] dlopen vs linking overhead
问题描述
当我从图书馆调用函数时,(1)和(2)之间是否有性能差异?请注意,我知道会有不同的初始化特征(如dlopen的成本,首次使用符号等的开销),但是在稳定状态下,两者的选择是否同样快或是一个更快?
谢谢。
如果库是共享对象(即某些 lib * .so
文件)使用 gcc -Wall -fPIC -O2
编译,并链接到 gcc -shared
那么它是一个 ELF 独立代码共享库。
PIC有一点比起64位x86-64的32位x86(其寄存器少)的成本更高,这有一些寻址模式便于PIC
它是一样的(稳定状态)性能明智的如果它是 dlopen
-ed或如果它是动态链接。因为在这两种情况下,真正的链接都是由动态链接器完成的(例如 ld-linux.so
)sine libdl.so
基本上是动态链接器的包装器。
当调用的是 lib * .so
中的代码,而且如果您 dlopen
它或如果您链接。
如果图书馆静态链接到图书馆,事情可能会有所不同 LIB * .A
。您甚至可以使用链接时间优化链接图库和程序 / a>最近的GCC编译器的能力(编译和链接与 gcc -flto -Wall -O2
)
阅读Drepper的如何编写共享库论文和程序库HowTo 和 Levine's Linkers&装载机。
Suppose I have a library - foo.so . When building my binary (which needs this library), I can either (1) link foo.so , or, (2) within the program source code, dlopen this library and then call functions provided by this library
Is there any performance difference between (1) and (2) when I call a function from the library? Note that I am aware that there will be different initialization characteristics (like the cost of the dlopen , overhead for first usage of a symbol etc) but in the steady state, are both alternatives equally fast or is one faster?
Thanks.
If the library is a shared object (ie some lib*.so
file) compiled with gcc -Wall -fPIC -O2
and linked with gcc -shared
then it is an ELF Position Independent Code shared library.
PIC is a bit more costly on 32 bits x86 -which has few registers- than on 64 bits x86-64 -which has some addressing mode facilitating PIC
It is the same (in steady state) performance wise if it is dlopen
-ed or if it is dynamically linked. Because in both cases the real linking is done by the dynamic linker (e.g. ld-linux.so
) sine libdl.so
is basically a wrapper to the dynamic linker.
What matters performance wise when called is the code inside the lib*.so
and it does not change if you dlopen
it or if you link it.
Things could be slightly different if the library is statically linked lib*.a
. You could even compile and link both the library and the program with the link time optimization ability of recent GCC compilers (compile and link with gcc -flto -Wall -O2
)
Read Drepper's How to Write Shared Library paper and the Program Library HowTo and Levine's Linkers & Loaders book.
这篇关于dlopen vs链接开销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!