Compensates to store the value of a struct member in local variable?

4

I see several programmers doing this. Instead of accessing the member of a struct directly, it copies the value to a local variable to the function and uses this variable.

Is there performance gain in this? Does it matter if the structure is in the stack or heap ?

If there is not, why do this?

    
asked by anonymous 09.02.2017 / 13:34

1 answer

5

Let's create a code that makes two ways to use the variable, both in the stack and heap :

#include <stdio.h>
#include <stdlib.h>

typedef struct {
   int i;
} Tipo;

int main() {
    Tipo x = { .i = 10 };
    printf("%d", x.i);
    int y = x.i;
    printf("%d", y);
    Tipo *z = malloc(sizeof(Tipo));
    z->i = 10;
    printf("%d", z->i);
    y = z->i;
    printf("%d", y);
}

See the Assembly generated in GodBolt . See running on ideone . And at Coding Ground . Also put it on GitHub for future reference .

Accessing the member in the stack generated this Assembly:

mov    eax,DWORD PTR [rbp-0x20]
mov    esi,eax
mov    edi,0x400694
mov    eax,0x0
call   400460 <printf@plt>

And variable access generated this Assembly:

mov    eax,DWORD PTR [rbp-0x4]
mov    esi,eax
mov    edi,0x400694
mov    eax,0x0
call   400460 <printf@plt>

Obviously the address is different, but the code is exactly the same. But to do this second there had to be the copy of the structure value for the local variable, so had to execute more code:

mov    eax,DWORD PTR [rbp-0x20]
mov    DWORD PTR [rbp-0x4],eax

So the performance of using local variable is worse. If you access the local variable several times this additional cost ends up being diluted, but it will still always be worse. There are codes that do not matter. But in C there are cases that make a difference. Extra access to the variable can cost anywhere from 1 nanosecond, up to tens of nanoseconds, depending on whether it is in the L1 cache or needs access to RAM.

Now let's look at heap by going straight:

mov    rax,QWORD PTR [rbp-0x10]
mov    eax,DWORD PTR [rax]
mov    esi,eax
mov    edi,0x400694
mov    eax,0x0
call   400460 <printf@plt>

And by accessing the local variable:

mov    eax,DWORD PTR [rbp-0x4]
mov    esi,eax
mov    edi,0x400694
mov    eax,0x0
call   400460 <printf@plt>

Here access to the structure member is slightly worse with one more statement. If you use the variable a few times it does not compensate because there is still a code to make the copy for the local variable, and in this case it is even slightly more expensive because of Indirect :

mov    rax,QWORD PTR [rbp-0x10]
mov    eax,DWORD PTR [rax]
mov    DWORD PTR [rbp-0x4],eax

If you use the local variable multiple times you can compensate for yes. The indirection adds an extra cost and the local variable can make it occur only once in the code.

If the gain does not occur in a specific situation I would say that the person copies to the local variable, or because he wanted to better document what he is doing using a meaningful variable name giving more readability to the code and performance is not important, or the person does not know that it is potentially worse.

It has language that can behave differently.

    
09.02.2017 / 13:34