Ross Lagerwall
Ross Lagerwall

Reputation: 93

Is there more overhead to calling a function in a separate compilation unit?

I know that there's more overhead to calling a function in a DSO due to the double jump. Is there more overhead in calling a function in a separate compilation unit compared with calling one in the same compilation unit (assuming it's not inlined in both cases)?

Upvotes: 5

Views: 248

Answers (2)

rodrigo
rodrigo

Reputation: 98348

Generally speaking, they will be the same, not countin inlining or other global optimization oportunities. But there may be subtle differences depending on the architecture.

For example, in Linux/Unix, the issue is not between functions of different CU, but whether the function you are calling has external linkage or not:

void foo() {}
void bar()
{ foo(); }

Or:

static void foo() {}
void bar()
{ foo(); }

If this code is compiled into a shared object (but not into an executable!), then the external foo() might be overriden by another shared object (via LD_LIBRARY_PRELOAD for example), but the static one cannot. Thus, the call to external functions inside shared object, even from the same CU, must be done using a relocatable jump.

In Windows, however, there is no such thing as LD_LIBRARY_PRELOAD, so there is no such difference.

Upvotes: 5

Medinoc
Medinoc

Reputation: 6608

The only possible overhead I see is with variable-length jump macros, which might always have the maximum length when jumping across compilation units (unless the linker optimizes that).

Upvotes: 0

Related Questions