always inline functions in different .so

502 Views Asked by At

In Linux, using gcc if I declare a function in a dynamic library as __attribute__((always_inline)) and then I dynamically load that library, will the function be inlined by the dynamic linker?

I am compiling dynamic libraries, in -O3 and not passing any link time optimization flags -flto

Real question is: is it worth declaring a function as always inline in a dynamic library, when it's called most of the times from another library?

1

There are 1 best solutions below

1
On BEST ANSWER

The glibc dynamic linker will not inline any functions found in the objects it loads.

However, if you declare and define an always_inline function in a header file, the compiler will inline that function, even if there is an implementation in a DSO. This has two consequences:

  • You get the speed benefit of inlining.

  • All the information in the function definition has been compiled into caller, so it might no longer be possible to replace the DSO with a different implementation.

In other words, it's probably a bit faster, but providing ABI compatibility is suddenly much harder.

So whether this is worth doing really depends on the performance needs and what kind of future changes to the library you expect.