Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do optimizers move temporary non-POD types out of loops?

Given the following code:

while(is_running)
{
     std::vector<buffer> buffers;

     // fill buffers

     // use buffers
}

Does modern compilers perform the following transformation?

std::vector<bufer> buffers;

while(is_running)
{
     // fill buffers

     // use buffers

     buffers.clear();
}
like image 697
ronag Avatar asked Jul 07 '13 08:07

ronag


1 Answers

The only way to know with certainty would be to test, but I would be rather surprised to see an optimizer carry out this optimization.

To even begin to carry out this optimization, the compiler would have to either 1) know enough about the internals of the functions involved to "realize" (for example) that operator new and operator delete are basically mirror images of either other, or 2) it would have to generate all the code for all the functions inline (all the way down to the invocations of operator new and operator delete, and have enough intelligence to be able to deduce the same conclusion from the code.

I can barely imagine the first, but don't recall having ever seen it. Given the complexities of a typical heap manager, the second strikes me as truly unbelievable.

Bottom line: I've been surprised before, and I'm sure I will be again -- but but would be a bigger surprise than most.

like image 145
Jerry Coffin Avatar answered Oct 19 '22 03:10

Jerry Coffin