Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will the compiler optimize functions which return structures with fixed size arrays?

Assuming I have a struct in C/C++ with fixed size array members, for example:

#define SIZE 10000
struct foo{
  int vector_i[SIZE];
  float vector_f[SIZE];
};

and I would like to create a function that will return an instance of foo, like:

foo func(int value_i, float value_f){
  int i;
  foo f;
  for(i=0;i<SIZE;i++) f.vector_i[i] = value_i;
  for(i=0;i<SIZE;i++) f.vector_f[i] = value_f;
  return f;
}

If I call the function using:

foo ff = func(1,1.1);

will the compiler perform some kind of optimization (ie TCO)?

Will the executable fill directly ff variable, or it will fill first f of func and then copy all values from f to ff?

How can I check if the optimization is performed?

like image 709
ztik Avatar asked May 12 '16 11:05

ztik


People also ask

What is return function in optimization?

In the context of the C++ programming language, return value optimization (RVO) is a compiler optimization that involves eliminating the temporary object created to hold a function's return value. RVO is allowed to change the observable behaviour of the resulting program by the C++ standard.

Is used to optimize the compiler?

Compiler optimization is generally implemented using a sequence of optimizing transformations, algorithms which take a program and transform it to produce a semantically equivalent output program that uses fewer resources or executes faster.

What optimization does GCC do?

The compiler optimizes to reduce the size of the binary instead of execution speed. If you do not specify an optimization option, gcc attempts to reduce the compilation time and to make debugging always yield the result expected from reading the source code.


1 Answers

My answer applies to c++.

Will the compiler perform some kind of optimization (ie TCO)?

By TCO do you mean "tail call optimization"? The function doesn't make a function call at the end (a tail call, if you will), so that optimization doesn't apply.

The compiler can elide the copy from the return value to the temporary due to named return value optimization. The copy-initialization from the temporary can also be elided.


How can I check if the optimization is performed?

By reading the generated assembly code.

If you can't read assembly, another approach would be to add copy and move constructors that have side effects and observe whether those side effects occur. However, modifying the program can have effect on whether the compiler decides to optimize (but side effects are not required to prevent copy elision).


If you don't want to rely on optimization, you should explicitly pass an exiting object to the function by reference (pointer in c), and modify it in place.


Standard reference for copy elision [class.copy] §31 (current standard draft)

When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object, even if the constructor selected for the copy/move operation and/or the destructor for the object have side effects. [...]

The section describes the criteria, which are met in this case. The quote was generated from the standard document draft at 2016-04-07. Numbering may vary across different versions of the standard document and rules have slightly changed. The quoted part has been unchanged since c++03, where the section is [class.copy] §15.

like image 162
eerorika Avatar answered Nov 16 '22 01:11

eerorika