静态与动态库性能 [英] Static vs. Dynamic Library Performance

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

问题描述

通常认为静态库的性能优于动态库。我的问题是:dll是否已经加载到内存中,是否还取决于它?我的意思是,一旦初始化和所有事情都发生了,动态库的函数调用和执行时间是否比静态库长?

It's a general notion that performance of static libraries is greater than that of dynamic. My question is: does it also depend on once the dll is already loaded in memory? I mean, once the initialization and all has happened, does the function calling and execution take longer in case of dynamic libraries than static libraries?

推荐答案

免责声明:我是Linux-fu蚱grass,所以到处(或到处都是)可能存在一些错误。但是总体思路应该是相对正确的。如果不是这样,我相信优秀的SO人员会很快纠正我。 :-)

Disclaimer: I am a Linux-fu grasshopper, so there might be some inaccuracies here and there (or just everywhere). But the general idea should be relatively correct.Sort of. And if it's not, I am sure the good SO people will quickly correct me. :-)

哦,我提供的链接是以Windows为中心的。如果有人可以提供适当的以Linux为中心的链接,我将不胜感激。

Oh, and the links I provided are Windows-centric. I would appreciate if anyone can provide the proper Linux-centric links.

简短的回答:可以。但是,即使这样做,性能差异也可以忽略不计。

Short answer: It might. However, even if it does, the performance difference is really negligible.

当您链接静态库时,编译器会生成代码以直接执行所有函数调用。创建过程并执行该代码后,函数调用是一条简单的调用指令。

When you link a static library, the compiler generates the code to do all function calls directly. When the process is created, and that code is executed, the function call is a simple call instruction.

使用动态库时,成本取决于您是否使用了动态库。使用加载时动态链接运行时动态链接

When you use a dynamic library, the cost depends on whether you are using load-time dynamic linking or run-time dynamic linking.

通过 加载时动态链接 ,编译器仍然会生成代码以直接调用该函数,就好像它是静态链接一样。当进程加载器加载DLL时,它将调用运行时链接程序来修复进程内存,以便这些调用直接进入实际的函数实现。这必须在从已加载的库进行任何函数调用之前发生。在Windows上,这是由NT DLL加载程序完成的,它在进程初始化时调用DLL上的LoadLibrary。在Linux上,这是由运行时链接程序ld-linux.so完成的。

With load-time dynamic linking, the compiler still generates code to call the function directly, as if it's a statically linked. When the process loader loads the DLL, it'll invoke the runtime linker to fix the process memory, so that those calls go directly to the actual function implementations. This has to happen before any call to a function from the loaded library is made. on Windows, it's done by the NT DLL loader, which calls LoadLibrary on the DLL at process initialization time. On Linux, it's done by the runtime linker, ld-linux.so.

使用 / DELAYLOAD 加载时动态链接,该过程本质上是相同,除了编译器生成调用小存根的代码外,后者将检查是否已加载库,如果未加载,则将调用NT DLL加载程序。因此,DLL将按需加载,并且进程加载器不必在进程初始化时加载它。这样可以缩短进程的启动时间,但呼叫性能仍然相同。 (但是请注意,延迟负载还有其他缺点)

With /DELAYLOAD load-time dynamic linking, the process is esentially the same, except the compiler generates code to call small stubs, which will check if the library is loaded, and if not, will call the NT DLL loader. Thus, the DLL will be loaded on demand, and the process loader doesn't have to load it at process initialization time. This results in faster process startup time, but the call performance is still the same. (Note however, that delay load suffers from other drawbacks)

我不知道是否有相应的Linux支持,但是如果没有相应的支持,我会感到惊讶。

I don't know if there's a corresponding Linux support, but I would be surprised if there isn't.

使用 运行时动态链接 ,您的代码将维护函数指针并决定何时加载库。在Windows上,它必须使用LoadLibrary和GetProcAddress;在Linux上,它是dlopen,dlsym和dlclose。无论如何,对进程启动时间的影响与对于延迟加载加载时间动态链接的含义相同;但是,在每个方法调用上取消引用的指针的确增加了很小的开销。 (尽管如果您知道自己在做什么,您可能会发疯并修复进程内存,以避免指针取消引用。但是,正确地做到这一点的努力要比获得的性能好处大一个数量级。

With run-time dynamic linking, your code maintains the function pointers and decides when to dload the library. On Windows, it has to use LoadLibrary and GetProcAddress, on Linux it's dlopen, dlsym and dlclose. In any case, the implications for the process startup time are the same as for the delay-load load-time dynamic linking; however, the pointer dereferencing on each method call does add a small negligible cost. (Although if you know what you are doing, you could go crazy and fix-up your process memory to avoid the pointer dereferencing. The effort to do this right however is an order of magnitude bigger than the perf benefits you'll get for doing it.)

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

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