Performance Advantages of the Const Specifier in C11

The benefits of the 'const' qualifier have been discussed on various issues, but these discussions relate mainly to the rejection of unintentional modification and clarity of intent. I would like to ask if there is any difference in the resulting program due to the use of a qualifier - in particular, is there a gain in performance?

In one of the other questions, it was noted that the compiler cannot use the natural assumption that an object does not change if it is passed to a function with the qual qualifier, given the existence of const_cast in C ++. This is not in C11, so does const allow any optimizations in C11 to be skipped without it?

Another possibility for performance differences is to mock memory for static objects. Is there a performance advantage for having an object in read-only space? This may be hardware dependent, I would be interested in both general ideas and comments related to specific configurations.

EDIT: It would be a very broad request to give a general argument stating that there can be no increase in performance needed to use "const". However, denying this statement is likely to be a rather short answer. So, to be more specific and not excluding anything related to my general question, the forms of answers that would be especially useful are as follows:

  • Any example of a case where performance enhancement is achieved using "const".

  • Any argument as to why the compiler cannot do the optimization due to the use of 'const'. This will probably not be hardware specific, and therefore, if so, perhaps this can be summarized. If you need to clarify this, I'm most interested in using 'const' in function calls.

  • Any answer to the question of whether it is possible to use read-only memory with static objects to achieve improved performance in desktop or server x86-64 implementations.

Having said that, I would be grateful for the most general answer to the question, which was initially stated as reasonable.

+4
source share
1 answer

, , , const.

, :

 extern void foo(const int*const);

 int bar(const int*const arr) {
   int k = arr[0];
   foo(arr);
   return k+arr[0];
 }

arr[0] ( k foo)

, gcc-4.9 ( ) , clang-3.5 ( -O3 -fverbose-asm Linux/x86-64).

, C11, , C99 .

+3

All Articles