Giving hints to the compiler/optimizer (#pragma hint ...)

Fri Jun 22 06:32:00 GMT 2018

it would be nice to have a mechanism to let the programmer give hints to the 


for (size_t i = 1; i < vec.size(); ++i)

If the size of vec cannot change while in this code block, then one better 
would do this instead:

const size_t vec_sz = vec.size();
for (size_t i = 1; i < vec_sz; ++i)

But, IMO it would be much better if one could just give a hint to the 
compiler/optimizer instead:

#pragma hint const vec.size()
for (size_t i = 1; i < vec.size(); ++i)

Then the compiler/optimizer could cache the vec.size(), ie. fill a const 
(register) variable just once and use that instead, like in the previous 
manual version.
(The manual version has of course the disadvantage that user has to define an 
additional variable and because of that, put the code in its own scope...)

So, passing such hints to the compiler/optimizer is IMO a good general way.
Would be nice if the gcc developers would implement such a mechanism.

Technically: such a "#pragma hint ..." would apply just to the next line of 
code, ie. automatically 'unhinting'.

More information about the Gcc-help mailing list