LD_BIND_NOW Can Make the Executable run Slower?

孤街浪徒 提交于 2020-01-05 09:13:27

问题


I am curious if an executable is poorly written that it has much dead code, referring to 1000s of functions externally (i.e. .so files) but only 100s of those functions are actually called during runtime, will LD_BIND_NOW=1 be worse than LD_BIND_NOW not set? Because the Procedure Linkage Table will contain 900 useless function addresses? Worse in a sense of memory footprint and performance (as I don't know whether the lookup is O(n)).

I am trying to see whether setting LD_BIND_NOW to 1 will help (by comparing to LD_BIND_NOW not set):
1. a program that runs 24 x 5 in terms of latency
2. saving 1 microsecond is considered big in my case as the code paths being executed during the life time of the program are mainly processing incoming messages from TCP/UDP/shared memory and then doing some computations on them; all these code paths take very short time (e.g. < 10 micro) and these code paths will be run like millions of times

Whether LD_BIND_NOW=1 helps the startup time doesn't matter to me.


回答1:


saving 1 microsecond is considered big in my case as the executions by the program are all short (e.g. <10 micro)

This is unlikely (or you mean something else). A typical call to execve(2) -the system call used to start programs- is usually lasting several milliseconds. So it is rare (and practically impossible) that a program executes (from execve to _exit(2)) in microseconds.

I suggest that your program should not be started more than a few times per second. If indeed the entire program is very short lived (so its process lasts only a fraction of a second), you could consider some other approach (perhaps making a server running those functions).

LD_BIND_NOW will affect (and slow down) the start-up time (e.g. in the dynamic linker ld-linux(8)). It should not matter (except for cache effects) the steady state execution time of some event loop.

See also references in this related answer (to a different question), they contain detailed explanations relevant to your question.

In short, the setting of LD_BIND_NOW will not affect significantly the time needed to handle each incoming message in a tight event loop.

Calling functions in shared libraries (containing position-independent code) might be slightly slower (by a few percents at most, and probably less on x86-64) in some cases. You could try static linking, and you might consider even link time optimization (i.e. compiling and linking all your code -main programs and static libraries- with -flto -O2 if using GCC).

You might have accumulated technical debt, and you could need major code refactoring (which takes a lot of time and effort, that you should budget).



来源:https://stackoverflow.com/questions/48109146/ld-bind-now-can-make-the-executable-run-slower

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!