Live data from Hacker News

Why Const Doesn't Make C Code Faster

theartofmachinery.com

51–60 of 197 posts

Re: Why Const Doesn't Make C Code Faster

#51
This issue is why D has both const and immutable qualifiers. const can't be optimized because there may be other mutable references to the same memory object. But for immutable references, there cannot be.

It is possible to cast away const and immutable in D, but these are only allowed in system code, presumably where the programmer actually does know what he's doing.

Re: Why Const Doesn't Make C Code Faster

#52
Const won't necessarily make things faster but constant values will (whether or not explicitly inferred). I noticed, for example, a 2x+ speedup in one of my ray tracers by downgrading my dynamic 2d/3d/4d vector class* to always have a constant size of 3.

* (poor choice, I know)

Re: Why Const Doesn't Make C Code Faster

#53

> This is true in practice because enough real-world C code has “I know what I’m doing” casting away of const. I found this out the hard way when I implemented OptimumC, the first data flow analysis optimizer for DOS C compilers back in the 1980's. I had to undo doing optimizations on const. Anecdote: Optimum C blew away all the other C compilers on a magazine benchmark article because it figured out that the benchma…

Nice

Re: Why Const Doesn't Make C Code Faster

#54

gcc has some attributes to help with that, for example try __attribute__((pure)) on funcs that will always return the same thing for the same inputs (given the same global state) and do not modify said input or state (and thus can be called more or fewer times than programmer wrote). Usually gcc will be quite aggressive with optimizations if you use that. For even more aggressiveness, try __attribute__((const)) which…

Does it enforce these attributes or are they just a promise by the code author?

D has a `pure` attribute for functions, and yes it is enforced. It's enforced even to the point where it becomes a PITA, but when you manage to make functions pure, you know it's solid.

One pain point with pure functions is you can't insert debug logging statements. D has a special case for that - purity for a statement isn't checked if it is prefixed with the `debug` keyword:

    pure int square(int x) {
        debug printf("called square\n");
        return x * x;
    }
which is very, very handy.

Re: Why Const Doesn't Make C Code Faster

#55
post #18

Unfortunately in C++, the compiler is not allowed to assume that if it passes a const reference to a function, that function will not change the object. The function is allowed to cast away const and modify the object. I'm not happy that they did it that way; I would have preferred it if cast-away-const were more restricted (for example, allowed when calling a child function that takes a char* but doesn't modify the…

> Unfortunately in C++, the compiler is not allowed to assume that if it passes a const reference to a function, that function will not change the object.

how could it make that assumption ? there is nothing that prevents you to implement the function in fortran or even in raw assembly

Re: Why Const Doesn't Make C Code Faster

#56

> This is true in practice because enough real-world C code has “I know what I’m doing” casting away of const. I found this out the hard way when I implemented OptimumC, the first data flow analysis optimizer for DOS C compilers back in the 1980's. I had to undo doing optimizations on const. Anecdote: Optimum C blew away all the other C compilers on a magazine benchmark article because it figured out that the benchma…

Sounds like you tried to game the system and got bitchslapped.

Re: Why Const Doesn't Make C Code Faster

#57
post #41

On some microcontrollers, marking variables/arrays as const allows the compiler to access them directly from flash rather than having to copy them into RAM at startup. I used this to great effect on a PIC24 with 256KB flash/16KB RAM.

Good point! On Arduino sometimes your program won't fit unless you store some parts in flash.

Re: Why Const Doesn't Make C Code Faster

#58

Const won't necessarily make things faster but constant values will (whether or not explicitly inferred). I noticed, for example, a 2x+ speedup in one of my ray tracers by downgrading my dynamic 2d/3d/4d vector class* to always have a constant size of 3. * (poor choice, I know)

Having a constant size probably made it easy for the compiler to properly generate bytecode that uses vectorized instructions

Re: Why Const Doesn't Make C Code Faster

#59

Earlier quoted context omitted.

Does it enforce these attributes or are they just a promise by the code author?

D has a `pure` attribute for functions, and yes it is enforced. It's enforced even to the point where it becomes a PITA, but when you manage to make functions pure, you know it's solid. One pain point with pure functions is you can't insert debug logging statements. D has a special case for that - purity for a statement isn't checked if it is prefixed with the `debug` keyword: pure int square(int x) { debug printf("c…

This seems a very pragmatic solution.

Re: Why Const Doesn't Make C Code Faster

#60
post #18

Unfortunately in C++, the compiler is not allowed to assume that if it passes a const reference to a function, that function will not change the object. The function is allowed to cast away const and modify the object. I'm not happy that they did it that way; I would have preferred it if cast-away-const were more restricted (for example, allowed when calling a child function that takes a char* but doesn't modify the…

Cast away const. One of the many reasons I gave up on C++. What is const if it can be cast away?
Post reply on HN