Optimization of function calls

Is it possible to assume that the C ++ compiler optimizes a function call of a member function of a class that sets only class variables? Example:

class A { private: int foo; public: void bar(int foo_in) { foo = foo_in; } } 

So, if I did it

 A test; A.bar(5); 

can the compiler optimize this for direct access to the member and set it like this?

+6
source share
3 answers

Yes, it is called inlining.

In addition, c++ designed specifically to support or facilitate for the compiler such optimizations in rather complex cases of inheritance and patterns.

Some would say that this is a rather distinctive feat of c++ as a high-level language compared to others. Its "high-level" functions (basically I mean general programming patterns) were developed with such optimizations in mind. This is also one of the reasons that makes it considered effective in terms of performance.

That's why I would expect decent work when developing inline with any reputable compiler.

From what I read, this is also the reason that it is difficult to get all the fancy stuff of other high-level languages, such as the reflection engine, or other well-known from, for example, Java or python. This is due to the fact that c++ makes it easy to embed almost everything possible, so it is difficult to inspect optimized code.

Edit:

Because you said that you are writing OpenGL material, where the performance of setters and getters is important and such optimizations, I decided to figure it out a bit and show a slightly more interesting example where you can rely on the built-in mechanism.

You can write some interfaces, avoiding the virtual mechanism, but using templates. For instance:

 //This is a stripped down interface for matrices for physical objects //that have Hamiltonian and you can apply external field and temperature to it template< class Object > class Iface { protected: Object& t; public: Iface(Object& obj) : t(obj) {}; Vector get_eigen_vals() {return t.get_eigen_vals(); }; Matrix get_eigen_vectors() {return t.get_eigen_vectors(); }; void set_H(VectorD vect) { t.set_H(vect); }; void set_temp(double temp) {t.set_temp(temp);}; }; 

If you declare such an interface, you can wrap the object using this interface object and pass an instance of this interface class to your functions / algorithms, and still have everything inline because it works by reference to the Object . A good compiler optimizes the entire Iface output.

+7
source

To answer the question a little more than just paste:

The standard has something like an as-if rule. It says that the compiler is allowed to make any changes to your program if this does not affect the observed behavior. There are even exceptions that allow them to change things that technically change the observed behavior.

It can call function calls and even complete classes. He can do basically anything, if it does not break anything.

+1
source

Yes, the compiler can optimize this call. This is actually a very simple investment case. The compiler is allowed to do much more tan (it can unroll loops, optimize local variables, replace calculations with constants, etc.)

0
source

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


All Articles