linuxshared-librarieslddynamic-linking

How to reduce dynamic library loading time?


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
    6. If some of your libraries are only loaded rarely or in specific use-cases, you can delay their loading until first use. There are builtin options for this in Windows (/DELAYLOAD) and macOS (-lazy-l) and on Linux you can use Implib.so.

    Library load time can be measured by exporting LD_DEBUG=statistics