简体   繁体   English

静态与动态库性能

[英]Static vs. Dynamic Library Performance

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? 我的问题是:一旦dll已经加载到内存中,它还依赖于它吗? 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? 我的意思是,一旦初始化和所有事情发生,动态库的函数调用和执行是否比静态库需要更长的时间?

Disclaimer : I am a Linux-fu grasshopper, so there might be some inaccuracies here and there (or just everywhere). 免责声明 :我是一个Linux-fu蚱蜢,因此可能存在一些不准确的地方(或者到处都是)。 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. 如果不是,我相信好的SO人会迅速纠正我。 :-) :-)

Oh, and the links I provided are Windows-centric. 哦,我提供的链接是以Windows为中心的。 I would appreciate if anyone can provide the proper Linux-centric links. 如果有人能提供正确的以Linux为中心的链接,我将不胜感激。

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 . 使用动态库时,成本取决于您使用的是加载时动态链接还是运行时动态链接

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. 当进程加载器加载DLL时,它将调用运行时链接程序来修复进程内存,以便这些调用直接进入实际的函数实现。 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. 在Windows上,它由NT DLL加载器完成,它在进程初始化时调用DLL上的LoadLibrary。 On Linux, it's done by the runtime linker, ld-linux.so. 在Linux上,它由运行时链接程序ld-linux.so完成。

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. 使用/DELAYLOAD 加载时动态链接 ,该过程基本相同,除了编译器生成调用小存根的代码,这将检查库是否已加载,如果没有,将调用NT DLL加载器。 Thus, the DLL will be loaded on demand, and the process loader doesn't have to load it at process initialization time. 因此,DLL将按需加载,并且进程加载器不必在进程初始化时加载它。 This results in faster process startup time, but the call performance is still the same. 这样可以加快进程启动时间,但通话性能仍然相同。 (Note however, that delay load suffers from other drawbacks) (但请注意,延迟负载存在其他缺点)

I don't know if there's a corresponding Linux support, but I would be surprised if there isn't. 我不知道是否有相应的Linux支持,但如果没有,我会感到惊讶。

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. 在Windows上,它必须使用LoadLibrary和GetProcAddress,在Linux上它是dlopen,dlsym和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.) (虽然如果你知道自己在做什么,你可能会疯狂并修复你的进程内存以避免指针解除引用。然而,这样做的努力比你获得的性能要好一个数量级。它。)

我认为性能方面的最大区别在于,使用静态库,编译器可以优化对库的函数调用,但在动态库中,编译器对它调用的函数的行为一无所知。

The machine code itself in a DLL or static library have ofc the same performance. DLL或静态库中的机器代码本身具有相同的性能。 A compiler can though more aggressively optimize an executable with static libraries, especially when link-time code generation is turned on. 编译器可以通过静态库更积极地优化可执行文件,尤其是在启用链接时代码生成时。 One can think of removal of unused variables and duplicate code and placement of code close to each other when used together (see PGO). 可以考虑在一起使用时删除未使用的变量和重复的代码以及代码的放置(参见PGO)。

When code is shared across applications it's better to use DLL's from a system performance point of view, since the overall memory pressure of the system is less (when os is able to map views of memory sections across processes which Windows does). 当代码在应用程序之间共享时,从系统性能的角度来看最好使用DLL,因为系统的整体内存压力较小(当操作系统能够跨Windows进程映射内存部分的视图时)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 动态与静态DLL链接是不同的 - Dynamic vs. Static DLL Linking is different Windows服务性能与Windows应用程序性能 - Windows Service performance vs. Windows Application performance 动态链接 - Linux 与。 Windows - Dynamic linking - Linux Vs. Windows Windows与Linux内存分配/ std :: list构造函数性能 - Windows vs. Linux memory allocation/std::list constructor performance 注册表调用与在静态变量中存储用户选项的效率 - Efficiency of registry call vs. storing user options in a static variable 尝试使用C库并使用swig将其公开给Windows中的Python。 有关静态与动态以及使用Visual Studio进行swig的问题 - Trying to take a C library and use swig to expose it to Python in Windows. Questions about static vs dynamic and using Visual Studio for swig Poco作为具有动态运行时的静态库需要DLL的导入库吗? - Poco as static library with dynamic runtime requires import library of DLL? Windows服务和线程工作代码的静态与非静态方法 - Windows service and static vs. non-static method for thread work code 如何在动态lib(/ MD)项目中使用静态库(/ MT)? - How to use a static library (/MT) in a dynamic lib (/MD) project? Windows C ++ Eclipse MinGW链接动态库和静态库 - Windows C++ Eclipse MinGW link both dynamic and static library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM