Re:Add a const here, delete a const there
by brucedawson on Monday January 09, 2017 @12:26PM
(#13357946)
Author here. I'm a big believer in adding const everywhere - local variables, globals, member functions, etc.
Marking globals as const can move them to the read-only segment which has some modest performance improvements, but in most other cases const doesn't affect code-gen at all. Using const prevents programmers from doing certain things, and the compiler enforces that. That suggests that the compiler already knows whether or not programmers are doing those things - it doesn't need a 'const' hint to tell it. And in those cases where it can't tell, the 'const' hint is usually not sufficient for it to depend on.
In short, with the exception of moving globals to the read-only segment I am not aware of any performance improvements from using const.
Marking globals as const can move them to the read-only segment which has some modest performance improvements, but in most other cases const doesn't affect code-gen at all. Using const prevents programmers from doing certain things, and the compiler enforces that. That suggests that the compiler already knows whether or not programmers are doing those things - it doesn't need a 'const' hint to tell it. And in those cases where it can't tell, the 'const' hint is usually not sufficient for it to depend on.
In short, with the exception of moving globals to the read-only segment I am not aware of any performance improvements from using const.