Archived

This topic is now archived and is closed to further replies.

jorgander

passing as 'const' improves speed?

Recommended Posts

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).

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
quote:
Original post by Arild Fines
[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


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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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?

Share this post


Link to post
Share on other sites