class Squa...">

What does compiler optimization mean "constant distribution"?

From effective C ++ by Scott Meyers:

template<typename T, std::size_t n> class SquareMatrix: private SquareMatrixBase<T> { public: SquareMatrix( ) : SquareMatrixBase<T>(n, 0), pData(new T[n*n]) { this->setDataPtr(pData.get()); } ... private: boost::scoped_array<T> pData; }; 

No matter where the data is stored, the key result from the bloating point of view is that now many - possibly all - member SquareMatrixs functions can be simple inline calls to versions of the base class that are shared with all other matrices containing data of the same type, regardless of their size. At the same time, SquareMatrix objects of different sizes are different types, therefore, although, for example, SquareMatrix <double, 5> and Using SquareMatrix <double, 1 0> the same member acts in SquareMatrixBase <double>, theres no chance SquareMatrix <double, 5> an object for a function that expects SquareMatrix <double, 1 0>. Nice no?

Nice, yes, but not for free. Versions of inversion with matrix sizes associated with them are likely to generate better code than general where the size is transferred as or stored in the object. For example, in dimensional versions, sizes would be compile-time constants, therefore for optimizations such as continuous distribution, including them, they are added to the generated instructions as immediate operands. This cannot be done in a size-independent version.

In the above description, in the last paragraph it was referred to as "therefore entitled to such optimization as continuous distribution, including their addition into the generated instructions as direct operands." What does this statute mean? Please clarify this.

Thanks!

+4
source share
2 answers

Constant distribution is a very simple (in principle) optimization left to compilers.

 size_t radius = 5; size_t diameter = 2*radius; float perimeter = diameter * 3.1416f; 

This will be reduced by the compiler by propagating constants:

  • note that the value of radius known
  • perform a calculation of 2*radius (this is a constant folding)
  • diameter value is known
  • perform diameter * 3.1416f
  • the value of perimeter known

Thus, the program is equivalent to:

 size_t radius = 5; size_t diameter = 10; float perimeter = 31.416f; 

Note that there are many other forms of optimization, for example, if radius and diameter are no longer needed, we can delete them and save only the perimeter .

+8
source

if you have the source code:

 a:=3; b:=4; b+=1; b+=2; a:=a+b; 

then the operators can be optimized by compilers like:

 b==7; a==10; 

This is called continuous spreading and permanent folding.

0
source

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


All Articles