Stories
Slash Boxes
Comments

SoylentNews is people

posted by Fnord666 on Sunday August 25 2019, @01:57PM   Printer-friendly
from the quite-the-contrary dept.

Submitted via IRC for SoyCow3196

Why const Doesn't Make C Code Faster

In a post a few months back I said it's a popular myth that const is helpful for enabling compiler optimisations in C and C++. I figured I should explain that one, especially because I used to believe it was obviously true, myself. I'll start off with some theory and artificial examples, then I'll do some experiments and benchmarks on a real codebase: Sqlite.

Let's start with what I used to think was the simplest and most obvious example of how const can make C code faster. First, let's say we have these two function declarations:

void func(int *x);
void constFunc(const int *x);

And suppose we have these two versions of some code:

void byArg(int *x)
{
  printf("%d\n", *x);
  func(x);
  printf("%d\n", *x);
}

void constByArg(const int *x)
{
  printf("%d\n", *x);
  constFunc(x);
  printf("%d\n", *x);
}

To do the printf(), the CPU has to fetch the value of *x from RAM through the pointer. Obviously, constByArg() can be made slightly faster because the compiler knows that *x is constant, so there's no need to load its value a second time after constFunc() does its thing. It's just printing the same thing. Right? Let's see the assembly code generated by GCC with optimisations cranked up:


Original Submission

 
This discussion has been archived. No new comments can be posted.
Display Options Threshold/Breakthrough Mark All as Read Mark All as Unread
The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
  • (Score: 5, Informative) by Ethanol-fueled on Sunday August 25 2019, @03:32PM (4 children)

    by Ethanol-fueled (2792) on Sunday August 25 2019, @03:32PM (#885206) Homepage

    The point of const is to make variables immutable so people don't fuck with them, not optimize for speed.

    Starting Score:    1  point
    Moderation   +4  
       Insightful=1, Informative=3, Total=4
    Extra 'Informative' Modifier   0  

    Total Score:   5  
  • (Score: 2, Informative) by Anonymous Coward on Sunday August 25 2019, @04:44PM (1 child)

    by Anonymous Coward on Sunday August 25 2019, @04:44PM (#885229)

    I was waiting for someone to point this out.

    Furthermore at least back in the DOS, proprietary unix and 8bit days, I seem to remember it providing other benefits that could speed things up, but it was a secondary effect mostly due to the optimization paths compilers of the day used.

    const's primary purpose has always been to ensure a pointer is immutable within the context of the C code itself. Writing in assembly could always bypass it and certain C compilers would fail to take it into account in all circumstances if you were particularly 'creative' with your means of access or rewriting of the pointer. But if you were following the standards for data typing, const was there to protect your functions or data structure from someone doing something stupid or malicious while working with a pointer or data structure.

    We used to call them clobbering vs non-clobbering functions which either modified in place, or had to create and return a new copy of a data structure with a new pointer in order to ensure consistency in the codebase. It was the tradeoff between memory consumption and ensuring a sane program state in the older memory constrained days of application and operating system development.

    • (Score: 2, Informative) by Anonymous Coward on Sunday August 25 2019, @06:49PM

      by Anonymous Coward on Sunday August 25 2019, @06:49PM (#885299)

      Look at his command line.

      gcc -S -Wall -O3 test.c

      That is the most aggressive optimization switch. Two of those are basic constant folding and basic constant propagation. When compiling, it can look and determine both of the variables, despite only one being marked, are not changed during actual execution and makes such a substitution, when it believes it is safe to do so. To your point, when GCC compilers were dumber "const" was important because it would signal the compiler you could handle the constants that way and it didn't have to guess whether or not it was safe.

      And that doesn't get into any other black magic the -O3 switch will do. If anything, he should have shown what a bare command line, -O, -O2, and -Os, all against the same code. He is smart enough to know the difference between a t test and a U test (although not that these are dependent samples and a different test should be used). I reckon he is smart enough to try and show the difference that way.

  • (Score: 0) by Anonymous Coward on Monday August 26 2019, @12:10AM

    by Anonymous Coward on Monday August 26 2019, @12:10AM (#885437)

    const doesn't necessarily make objects immutable though. The variable can be changed elsewhere. const instead specifies access permissions to a variable.

  • (Score: 0) by Anonymous Coward on Monday August 26 2019, @04:24PM

    by Anonymous Coward on Monday August 26 2019, @04:24PM (#885693)

    Yeah but that's why God had to invent const_cast to override the excessive use of const by others to protect us.