Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stack (local) or global variables?

Tags:

c++

c

Will using global variables give a speed up? In Intel's Architecture Software Developer Manual (about the microprocessor) it is recommended to use local variables instead of global. But, consider the following code:

void process_tcp_packets(void) {
    char tcp_data[128];

    do some stuff with the tcp_data[]....
}

void process_udp_packets(void) {
    char udp_data[128];

    do some stuff with the udp_data[]
}
void main_event_loop(void) {

    while(1) {
         if (pending_tcp_requests) {
             process_tcp_packets();
         }
         if (pending_udp_requests) {
             process_udp_packets();
         }
   }
}

When main_event_loop() is executed, the control of the flow depends on the variables "pending_tcp_requests" and "pending_udp_requests". Both functions process_tcp_packets() and process_udp_packets(), when called, will allocate local variables at the current stack pointer. This means if the code is constantly switching both functions, the local variables will be allocated at the same memory address. Sharing the memory address between both functions will evict data from current L1 cache and slow execution. So, by using global variables instead of local ones we can speed up execution. Is it correct or not?

If so, is there any drawback for using global variable in this case?

like image 831
Nulik Avatar asked Nov 27 '25 17:11

Nulik


2 Answers

Global variables are very unlikely to give you a benefit over the stack in this case.

  1. "Allocating" memory on the stack is simply adding a number to the stack pointer, a ½ cycle operation.
  2. At 64kb, the L1 cache is large enough to contain many stack frames. Moreover the most recent few stack frames are (because you push params when calling a function) almost always in the cache. In fact, the region for the next frame to be allocated is usually in L1, because you've often just exited some other function that used it.
  3. By contrast, global variables quickly fall out of L1. Remember, the stack is touched at the beginning and end of every function (just to push the return address), and process_tcp_packets() and process_udp_packets() will both use approximately the same address space to store their locals.
  4. Sharing memory addresses between functions does not evict data from cache. On the contrary, it makes you more likely to hit cache. The more recently you have touched an address, the more likely it is to be in cache.
like image 53
Crashworks Avatar answered Nov 30 '25 07:11

Crashworks


Sharing the memory address between both functions will evict data from current L1 cache and slow execution. So, by using global variables instead of local ones we can speed up execution. Is it correct or not?

Your premise is not correct for single-threaded use of memory.

Sharing the same memory between threads using different CPU caches will cause cache contention (and usually flushing). Sharing memory between functions on the same thread using the same CPU cache will not do this at all.

Also, on some platforms, local variables require less instructions to access than global variables (as they are offsets local to the stack pointer and can usually be encoded in <=2 bytes, where as global variables may not be).

like image 45
MSN Avatar answered Nov 30 '25 06:11

MSN



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!