共享库与静态库的加载时间 [英] Loading time for shared libraries vs static libraries

查看:34
本文介绍了共享库与静态库的加载时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于共享库与静态库加载时间的问题.

I have a question on shared libraries vs static libraries loading time.

假设我有一个使用 liba、libb、libc 的可执行文件 foo.exe.此外,在给定时间,机器上运行的可执行文件实例超过 10 个.

Assume that i have a executable foo.exe which uses liba, libb, libc. Also at a given time there are more than 10 instances of the executable running on the machine.

现在如果以上 3 个库是共享库:第一个实例加载到 RAM 中:所花费的时间将是 foo.exe 的 main() 加载内存所花费的时间(假设它可以忽略不计)+ 加载 liba 的时间 + 加载 libb 的时间 + 加载 libc 的时间第二个实例已启动:现在假设运行此可执行文件的第二个实例.由于所有图书馆已经加载到主内存中,所花费的时间只是将 main() 加载到内存中,可以忽略不计.

Now if the above 3 libraries were shared libraries : 1st Insance is loaded into RAM : The time taken will be time taken by main() of foo.exe to be loaded memory (assuming its negligible) + time to load liba + time to load libb + time to load libc 2nd instance is started : Now assume a second instance of this executable is run. Since all libraries are already loaded into the main memory, time taken is only for loading the main() to the memory which is negligible .

现在如果以上 3 个库是静态库:第一个实例加载到 RAM 中:所花费的时间将是加载 foo.exe 的 main() 所花费的时间内存(假设它可以忽略不计)+ 加载 liba 的时间 + 加载 libb 的时间 + 加载 libc 的时间(当然它现在是整个可执行文件的所有部分)第二个实例已启动:现在假设运行此可执行文件的第二个实例.所用时间为foo.exe 的 main() 再次加载内存所花费的时间(假设它可以忽略不计)+ 加载 liba 的时间 + 加载 libb 的时间 + 加载 libc 的时间.(因为每个可执行文件都不能共享库,因为这些是静态库)

Now if the above 3 libraries were static libraries : 1st Insance is loaded into RAM : The time taken will be time taken by main() of foo.exe to be loaded memory (assuming its negligible) + time to load liba + time to load libb + time to load libc (Offcourse its now all part of the executable as a whole) 2nd instance is started : Now assume a second instance of this executable is run. Time taken will be again time taken by main() of foo.exe to be loaded memory (assuming its negligible) + time to load liba + time to load libb + time to load libc. (Since each executable cant share librareies as these are static librareies)

所以我的结论是静态库的加载时间会更长.但是我被告知共享库在加载过程中比静态库花费更多的时间,所以会有延迟,所以共享库不是一个好的选择.这怎么可能?

So my conclusion is that with static library the loading time will be more. But i was told that shared libraries take more time during loading than static libraries, so there will be a delay and so shared libraries is not a good option. How is this possible ?

推荐答案

链接(解析引用)不是免费的.使用静态链接,解析在生成二进制文件时一劳永逸地完成.使用动态链接,每次加载二进制文件时都必须完成.更不用说编译为在共享库中运行的代码可能比编译为静态链接的代码效率低.确切的成本取决于体系结构和系统对动态链接的实现.

Linking (resolving references) is not free. With static linking, the resolution is done once and for all when the binary is generated. With dynamic linking, it has to be done every time the binary is loaded. Not to mention that code compiled to run in a shared library can be less efficient than code compiled to be linked statically. The exact cost depends on the architecture and on the system's implementation of dynamic linking.

对于 32 位 x86 指令集,动态库的成本可能相对较高:在 ELF 二进制格式,必须牺牲已经稀缺的寄存器之一才能使动态链接代码可重定位.较旧的 a.out 格式将每个共享库放置在一个固定位置,但这并没有t 规模.我相信 Mac OS X 有一个中间系统,当动态库放置在地址空间中的预定位置时,但冲突是在单个计算机的规模上解决的(安装新系统后漫长的优化系统性能"阶段)软件).在某种程度上,这个系统(称为预绑定)允许您拥有蛋糕并吃掉它也.我不知道现在 Apple 几乎转向 amd64 架构是否仍然需要预绑定.

The cost of making a library dynamic can be relatively high for the 32-bit x86 instruction set: in the ELF binary format, one of the already scarce registers has to be sacrificed to make dynamically linked code relocatable. The older a.out format placed each shared library at a fixed place, but that didn't scale. I believe that Mac OS X has had an intermediate system when dynamic libraries where placed in pre-determined locations in the address space, but the conflicts were resolved at the scale of the individual computer (the lengthy "Optimizing system performance" phase after installing new software). In a way, this system (called pre-binding) allows you to have your cake and eat it too. I do not know if prebinding is still necessary now that Apple pretty much switched to the amd64 architecture.

此外,在现代操作系统上,静态和动态链接的代码仅在使用时才从磁盘加载(调入),但这与您的问题非常正交.

Also, on a modern OS both statically and dynamically linked code is only loaded (paged in) from disk if it is used, but this is quite orthogonal to your question.

这篇关于共享库与静态库的加载时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆