passing as 'const' improves speed?

Started by
8 comments, last by jorgander 20 years, 11 months ago
currently, i''m of the mind that if i want to pass a struct to a function, i should pass a reference to it. this way, the entire struct is not pushed on to stack, just it''s address. of course, the called function has to resolve the reference-as-pointer, which can also take extra cycles, so i''m not sure which is actually faster... also, if a called function does not plan to change one of it''s parameters, it should add the ''const'' modifier to it. i assume that this is so that another copy of the variable does not have to be pushed onto the stack (since ANSI-C is call-by-value). what i want to find out is whether it''s faster to pass a structure as ''const'' if a function does not plan to modify it (instead of passing it''s address as a pointer). this way, the called function does not have to resolve the pointer *and* the whole structure is not pushed onto the stack (if i am right about how const structures are passed).
Advertisement
Correct. I recommend const if the function does not modify the parameter.

As for the function reference, pointer, and const, I recommend passing in a reference to a constant structure. Using pointer inplace of reference is another solution. The key is to implement const if the function does not modify the object.

Kuphryn
A const does NOTHING during run-time. It''s simply a way to prevent the compiler from modifying a variable that you have passed. Once compiled, it produces the EXACT same output.

Also, there is no "resolving the reference as pointer". In assembly, it simply pulls the address of the variable, it doesn''t do any sort of resolving or converting.



The entire idea of const is to keep programmers from making mistakes. It will not make anything faster.
quote:Original post by Ready4Dis
The entire idea of const is to keep programmers from making mistakes. It will not make anything faster.

The use of const can enable the compiler to make certain optimizations it cannot make otherwise.


"To assert that the earth revolves around the sun is as erroneous as to claim that Jesus was not born of a virgin."
-- Cardinal Bellarmine
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
quote:Original post by Arild Fines
Original post by Ready4Dis
The entire idea of const is to keep programmers from making mistakes. It will not make anything faster.

The use of const can enable the compiler to make certain optimizations it cannot make otherwise.


"To assert that the earth revolves around the sun is as erroneous as to claim that Jesus was not born of a virgin."
– Cardinal Bellarmine


Exactly.

If I recall correctly, one big difference (between using const and non-const) is the compiler can assume there won''t be any aliasing problems. For example, if your function takes a reference to two structs, and you pass the same struct twice, there may be problems if you start modifying the data. The compiler can''t cache values in registers, because changes to one strucutre are also making changes to the other.

Most compilers have safe-guards to keep aliasing from being a problem. If you pass your references as constant, then these safeguards can be removed, improving execution speed.

–TheMuuj
--TheMuuj
as i understand it (and i could be wrong):

when a variable gets used, the value first must be fetched from it's address. well, in the case of a pointer, the value is itself an address. so, the value must be fetched, then the value changed to an address, then the value of that address must be fetched. so, two fetch operations are done instead of one. someone correct me if i am wrong.

i can't remember where, but i read in a reputable book/article that using the const keyword in function paramter declarations enabled the compiler to make some optimization(s). if this is the case, then the run-time would indeed be different. again, correct me if i am wrong. i dont know enough about low-level programming to know all the details, i just want to write bitchin fast code.

edit: whoops didnt see TheMuuj's post. thanks for clarifying.

[edited by - jorgander on May 1, 2003 8:50:27 PM]
Low-level, passing by address is the same thing as passing by reference, const or not. The difference is in the syntax and semantics of your objects. You type . instead of ->, but a pointer dereference still takes place. If you have an object, then you can provide different functions for the const flavor, perhaps returning const data instead of a mutable reference.

For small data types, and numeric types, passing by value is usually faster - it''s faster to pass a complex by value and copy-assign (aim for RVO - Return Value Optimization) the results than to pass a reference and manipulate the data through the pointer. An aggressive optimizer can take a const& and turn it into a pass-by-value (MSVC, Intel), but most compilers will not because it is not a safe optimization to perform without checking for access to mutable properties, const_cast, or other underhanded ways of manipulating so-called const objects.

References are pointers, they just use the syntax of locals, and make it hard to reassign or assign 0 to the address (but definitely not impossible).
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
gotw.81:
Constant Optimization?
Difficulty: 3 / 10

quote:It''s a common belief that const-correctness helps compilers generate tighter code. Const is indeed a Good Thing, but the point of this issue of GotW is that const is mainly for humans, rather than for compilers and optimizers. When it comes to writing safe code, const is a great tool that lets programmers write safer code with compiler checking and enforcement. Even when it comes to optimization, const is still principally useful as a tool that lets human class designers better implement handcrafted optimizations, and less so as a tag for omniscient compilers to automatically generate better code
quote:Original post by Arild Fines
The use of <tt>const</tt> can enable the compiler to make certain optimizations it cannot make otherwise.
Care to show an example?

If const optimizations were allowed, const_cast and mutable wouldn''t exist, as they get around them in ways that would probably be NP for the compiler to detect.

This topic is closed to new replies.

Advertisement