Advertisements

blargg wrote:
> wrote:
>> blargg wrote:
>>> Care to show any examples of where const helps speed-wise? That is, a
>>> program where removing const wouldn't change the semantics of the program,
>>> but would reduce its speed. I'm not arguing against using const, just
>>> questioning this often-made claim.
>> const could help a compiler determine if a variable
>> could be replaced with a constant in machine code.
>> Opcodes with operands, usually have smaller faster
>> versions which operate on constants.
> [...]
>
> Yes, but can you provide EXAMPLES of where const actually helps the
> compiler determine this, where it couldn't determine it without const?

'const' in C++ has serves multiple purposes. It can 1) declare constness
of an object itself, and 2) declare constness of an access path to an
object, 3) serve as a distinguishing trait in overload resolution.

How the third role can be used in "optimization" is rather obvious. But
these are basically user optimizations, as opposed to complier
optimizations. I assume that you are talking about compiler
optimizations, to which the third role is not immediately relevant.

I don't think I need to provide examples of how the first role of
'const' can speed up the code. They are immediately obvious as well.
Constant values can be calculated at compile time. Code that depends on
constant values can be reduced at compile time. Etc.

The second role of 'const' is more about discipline, than about
optimization. In general case, the constness of access path does not
imply the constness of the object this access path leads to. Which means
that in general case without preforming a thorough aliasing analysis,
the compiler can't do much based on the constness of access path itself.
And if the compiler is smart enough to perform a sufficiently thorough
aliasing analysis, it should be smart enough to optimize it regardless
of whether the explicit 'const' is supplied by the user or not. So, I'd
agree that the second role of 'const' by itself is not immediately
useful for compiler optimizations. However, it is worth noting that it
might become quite useful if the user explicitly gives the compiler the
permission to perform "overly agressive" optimizations (meaning, that
they might be formally invalid in general case) by assuming some
aliasing properties of the code, instead of trying to derive them from
the code itself. (I'm not sure, but it is quite possible that the
combination of 'const' with C99's 'restrict' allows for more valid
straightforward optimizations that just a mere 'restrict'.)

In case you haven't noticed, this message is cross-posted to two groups
where C++ code is entirely appropriate; the Subject: line might also be
a hint.
> which contains the const keyword,
> and which also does something different when const is removed.

The above program does something quite different when the comment
markers are removed from around the second 'const'.

This is not valid C and is an example of why cross-posting between C and
C++ groups is generally a bad idea. Please keep answers which are
specifically C++ in groups where C++ is topical.
--
Flash Gordon
If spamming me sent it to
If emailing me use my reply-to address
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/

<snip>
>> In order to make a point,
>> you would have to post a correct c program
>
> In case you haven't noticed, this message is cross-posted to two groups
> where C++ code is entirely appropriate; the Subject: line might also be
> a hint.

It is also posted to a group where C++ is NOT topical. The way pete
indicated this might not be the best, but it is certainly true that the
example given was not correct for comp.lang.c where only C is topical.
Is it really too difficult for people with C++ specific answers to drop
the cross-post to comp.lang.c?
--
Flash Gordon
If spamming me sent it to
If emailing me use my reply-to address
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/

Pete Becker wrote, On 11/11/08 20:24:
> On 2008-11-11 15:16:00 -0500, Flash Gordon <> said:
>
>> James Kuyper wrote, On 11/11/08 19:12:
>>> pete wrote:
>>
>> <snip>
>>
>>>> In order to make a point,
>>>> you would have to post a correct c program
>>>
>>> In case you haven't noticed, this message is cross-posted to two groups
>>> where C++ code is entirely appropriate; the Subject: line might also be
>>> a hint.
>>
>> It is also posted to a group where C++ is NOT topical. The way pete
>> indicated this might not be the best, but it is certainly true that the
>> example given was not correct for comp.lang.c where only C is topical.
>> Is it really too difficult for people with C++ specific answers to drop
>> the cross-post to comp.lang.c?
>
> It's no more difficult than for people with comments about the
> appropriateness of postings in comp.lang.c to drop the cross-post to
> comp.lang.c++.

I had not registered that it was James Kuyper, who does read
comp.lang.c, who made the comment above about it being topical in two of
the groups this is cross-posted to. Sorry. If I had registered I would
have dropped the cross-post.

In general where threads are cross-posted between comp.lang.c and
comp.lang.c++ it is people who don't read comp.lang.c who post messages
cross-posted to it which are not topical "here", so in general making
such a comment just in comp.lang.c can't do any good. People in all
three of these groups (including comp.lang.c) need to watch out for
cross-posts and drop comp.ang.c++ where it is a C specific answer, and
comp.lang.c where it is a C++ answer, and tell the OP what a bad idea
the cross-post is because of the problems it causes.
--
Flash Gordon
If spamming me sent it to
If emailing me use my reply-to address
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/

On 11 Nov 2008 at 21:20, Flash Gordon wrote:
> People in all three of these groups (including comp.lang.c) need to
> watch out for cross-posts and drop comp.ang.c++ where it is a C
> specific answer, and comp.lang.c where it is a C++ answer,

You might /like/ people to do this for your own political reasons. But
they don't /need/ to. They can do whatever they think is appropriate,
whether it suits your prejudices or not.
> and tell the OP what a bad idea the cross-post is because of the
> problems it causes.

There is no problem - you just choose to believe there is for your own
political reasons.

Flash Gordon wrote:
> Pete Becker wrote, On 11/11/08 20:24:
>> On 2008-11-11 15:16:00 -0500, Flash Gordon <> said:
>>
>>> James Kuyper wrote, On 11/11/08 19:12:
>>>> pete wrote:
>>> <snip>
>>>
>>>>> In order to make a point,
>>>>> you would have to post a correct c program
>>>> In case you haven't noticed, this message is cross-posted to two groups
>>>> where C++ code is entirely appropriate; the Subject: line might also be
>>>> a hint.
>>> It is also posted to a group where C++ is NOT topical. The way pete
>>> indicated this might not be the best, but it is certainly true that the
>>> example given was not correct for comp.lang.c where only C is topical.
>>> Is it really too difficult for people with C++ specific answers to drop
>>> the cross-post to comp.lang.c?
>> It's no more difficult than for people with comments about the
>> appropriateness of postings in comp.lang.c to drop the cross-post to
>> comp.lang.c++.
>
> I had not registered that it was James Kuyper, who does read
> comp.lang.c, who made the comment above about it being topical in two of
> the groups this is cross-posted to. Sorry. If I had registered I would
> have dropped the cross-post.

I think that a comparison between C and C++ with regard to 'const' and
pointers should be on-topic in all three groups, but that everyone
involved in such a discussion should be careful to distinguish which
language they're talking about at any given point in the discussion,
except when making a statement that they believe to be true in both
languages.

On Nov 11, 4:34 pm, (blargg) wrote:
> wrote:
> > blargg wrote:
> > > Care to show any examples of where const helps speed-wise?
> > > That is, a program where removing const wouldn't change
> > > the semantics of the program, but would reduce its speed.
> > > I'm not arguing against using const, just questioning this
> > > often-made claim.
> > const could help a compiler determine if a variable could be
> > replaced with a constant in machine code. Opcodes with
> > operands, usually have smaller faster versions which operate
> > on constants.
> [...]
> Yes, but can you provide EXAMPLES of where const actually
> helps the compiler determine this, where it couldn't determine
> it without const?

It's generally true of top-level const. Something like:

int const i = 42 ;

, for example. The compiler knows that regardless of what
happens elsewhere in a legal program, i will always be equal to
42.
> About the only one is a non-static variable at file scope,
> where the compiler can't in general assume its value after
> code in other translation units has executed, unless it's
> declared const. A variantis a local const object whose address
> is passed to a function; removing const would prevent the
> compiler from assuming its value doesn't change during the
> function call. I contend that these are rare cases.

In C, probably, since such variables can't be used in constant
expressions. In C++, it's actually quite common.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!