Local variables and C ++ registers

I often saw that the compiler puts local function variables in registers. And I have a question regarding this.

If I use some class member variable (integral / pointer, etc.) to a large extent, does it make sense to temporarily copy it to a local variable, work with it, and not copy the result to a class member?

For example (fill out a one-page ptr list):

struct MyClass{ struct ObjectHolder{ ObjectHolder* next_free; }; ObjectHolder *next_free = nullptr; void fill(){ ObjectHolder *copy_of_free = next_free ; // copy to register? for (int i = 0; i < capacity; ++i) { ObjectHolder &obj = array[i]; // build chain of pointers obj.next_free = copy_of_free; copy_of_free = &obj; } next_free = copy_of_free; // back to memory } } 
+6
source share
4 answers

What you described in your code goes beyond copying member variables into local variables to enable registry placement. Essentially, your question illustrates the difference between the two approaches:

  • Compute the result locally before writing it to member variables, vs.
  • Writing intermediate results to member variables while doing calculations.

Your program follows approach number 1. It is more reliable than # 2 because the object remains in a consistent state while the calculation is in progress.

Approach # 1 can give the compiler more options to optimize your code. However, this is a secondary effect; your object remaining consistent during the calculation is more important.

+4
source

These manipulations make no sense, because the compiler itself can put the value of the data element in a register that optimizes the code. Moreover, if you put the data item in a local variable, it can happen in such a way that it makes the code more complex.

0
source

This is supposed to be an optimization that makes your code run faster, at a price that is less readable and therefore likely to contain errors. In your case, I do not quite understand what the code is doing, but obviously there is an error. And there is a good chance that the compiler is smart enough to do what you are trying to do.

In general, the rule: only speed optimization after you have measured the code speed and found that it is too slow. If it is fast enough so that you do not measure speed, then it is fast enough. Then measure before and after making the changes and use your changes only if the speed increases and the code still works correctly.

Instead of using optimizations that reduce the quality of the code, you should find places in your code where you do something in an unnecessarily complicated way and make things simpler and more efficient in this way. Removing unnecessary complications, as a rule, makes your code more correct, more convenient and faster.

-1
source

Let micro-optimization of the compiler using register can make your code slower, because you do not allow the compiler to optimize the way it wants.
In case of performance problems, try to avoid a big waste of time (for example, copying large objects when you can pass them by reference) and optimize only when it is checked (using the standard) that you need. By the way, the test will indicate what needs to be optimized.

-1
source

Source: https://habr.com/ru/post/972093/


All Articles