Search code examples
cperformancestructfunction-calls

Does the byte size of an argument affect the overhead of an opaque function call?


This is best asked with a code example:

typedef struct {
    ... // Fields take up many bytes (>= 32 bytes)
} some_struct;

void alternative_1(some_struct arg);
void alternative_2(const some_struct *arg);

Assuming that both function alternatives are implemented inside an already compiled binary (and therefore cannot be inlined), is there a difference in the function call overhead?


Solution

  • Obviously yes:

    When calling alternative_1, the whole struct data is pushed onto the stack prior to branching to the function (which may be quite a lot).

    Whereas alternative_2 only has a single pointer to the struct pushed onto the stack.

    There is also a big difference when operating on the struct:

    • In the case of alternative_1 you work on a local copy.
    • When modifying the struct in alternative_2 you change the original data.

    Its quite similar to a function receiving either an integer or a pointer to an integer.