Sunday, July 10, 2022

[SOLVED] How to reduce dynamic library loading time?

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

  1. Avoid C++ constructors in libraries as they cause delays for running constructors themselves, for resolving symbols and loading code from hard drive
  2. 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)
  3. Use --as-needed to minimize library dependencies
  4. Make sure that lazy binding is enabled (i.e. LD_BIND_NOW is not set and library is not compiled with -Wl,-z,now)
  5. 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)