Issue
I have a question about reducing the dynamic linking time.
I've got a binary which links with 189 shared libraries. I profiled the execution with perf
tool and the dynamic linker (ld.so) takes up 40% of the runtime of my program, approximately 90 milliseconds of time.
Is there any way to optimize dynamic library loading time, or am I forced to use static linking?
I run this program a lot of times.
Solution
- Avoid C++ constructors in libraries as they cause delays for running constructors themselves, for resolving symbols and loading code from hard drive
- Minimize number of exported symbols by compiling libraries with
-fvisibility=hidden
(and selectively using__attribute__((visibility("default")))
for symbols that you do want to export) - Use
--as-needed
to minimize library dependencies - Make sure that lazy binding is enabled (i.e.
LD_BIND_NOW
is not set and library is not compiled with-Wl,-z,now
) - Finally you can use the Prelink tool to precompute symbol offsets statically
Library load time can be measured by exporting LD_DEBUG=statistics
Answered By - yugr Answer Checked By - Timothy Miller (WPSolving Admin)