Can const-correctness improve performance?

[亡魂溺海] 提交于 2019-11-26 17:30:37

问题


I have read numerous times that enforcing const-correctness in your C or C++ code is not only a good practice with regards to maintainability, but also it may allow your compiler to perform optimizations. However, I have read the complete opposite, too — that it does not affect performance at all.

Therefore, do you have examples where const correctness may aid your compiler with improving your program's performance?


回答1:


const correctness can't improve performance because const_cast and mutable are in the language, and allow code to conformingly break the rules. This gets even worse in C++11, where your const data may e.g. be a pointer to a std::atomic, meaning the compiler has to respect changes made by other threads.

That said, it is trivial for the compiler to look at the code it generates and determine if it actually writes to a given variable, and apply optimizations accordingly.

That all said, const correctness is a good thing with respect to maintainability. Otherwise, clients of your class could break that class's internal members. For instance, consider the standard std::string::c_str() -- if it couldn't return a const value, you'd be able to screw around with the internal buffer of the string!

Don't use const for performance reasons. Use it for maintainability reasons.




回答2:


Yes it can.

Most consts are purely for the benefit of the programmer and do not help the compiler optimize because it's legal to cast them away and so they don't tell the compiler anything useful for optimization. However, some consts cannot be (legally) cast away and these do provide the compiler with useful information for optimization.

As an example, access to a global variable defined with a const type can be inlined while one without a const type cannot be inlined because it might change at runtime.

https://godbolt.org/g/UEX4NB

C++:

int foo1 = 1;
const int foo2 = 2;

int get_foo1() {
    return foo1;
}

int get_foo2() {
    return foo2;
}

asm:

foo1:
        .long   1
foo2:
        .long   2
get_foo1():
        push    rbp
        mov     rbp, rsp
        mov     eax, DWORD PTR foo1[rip] ; foo1 must be accessed by address
        pop     rbp
        ret
get_foo2():
        push    rbp
        mov     rbp, rsp
        mov     eax, 2 ; foo2 has been replaced with an immediate 2
        pop     rbp
        ret

In practical terms, keep in mind that while const can improve performance, in most cases it won't or it will but the change will not be noticeable. The primary usefulness of const is not optimization.


Steve Jessop gives another example in his comment on the original question which brings up something worth mentioning. In a block scope, it's possible for a compiler to deduce if a variable will be mutated and optimize accordingly, regardless of const, because the compiler can see all uses of the variable. In contrast, in the example above, it's impossible to predict if foo1 will be mutated since it could be modified in other translation units. I suppose a hypothetical sentient ultra-compiler could analyze an entire program and determine if it's valid to inline access to foo1... but real compilers can't.




回答3:


in my experience, no

For scalar variables, compiler is able to determine whenever the value is changed and perform necessary optimization itself.

For array pointers, const correctness is no guarantee that values are really constant in presence of potential aliasing problems. Hence compiler can not use const modifier alone to perform optimizations

if you are looking optimization, you should consider __restrict__ or special function modifiers/attributes: http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html




回答4:


A bit old, but still applies: http://www.gotw.ca/gotw/081.htm And some more: http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/



来源:https://stackoverflow.com/questions/3435026/can-const-correctness-improve-performance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!