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, Insightful) by Dr Spin on Sunday August 25 2019, @03:02PM (3 children)

    by Dr Spin (5239) on Sunday August 25 2019, @03:02PM (#885197)

    ... by using const - with two printf()s in there?

    WTF?

    You could save two whole CPU cycles out of about 5k cycles?

    Besides which, if the value does not change, then the optimiser should create the same code whether const is there or not.
    What machine architecture are you using, with what caching? Have you looked at the code this generates? Could you
    read it anyway?

    Compiler writers are mostly very clever* people, and most C compilers have had more than 10 years of optimisation.

    * probably also autistic, OCD and fanatic about saving 1/2 a CPU cycle in even the most obscure cases, and definitely
    out to prove they are better than the last guy.

    --
    Warning: Opening your mouth may invalidate your brain!
    Starting Score:    1  point
    Moderation   +3  
       Insightful=3, Interesting=1, Overrated=1, Total=5
    Extra 'Insightful' Modifier   0  
    Karma-Bonus Modifier   +1  

    Total Score:   5  
  • (Score: 0, Touché) by Anonymous Coward on Sunday August 25 2019, @09:08PM (2 children)

    by Anonymous Coward on Sunday August 25 2019, @09:08PM (#885377)

    saving 2 cycles out of 5k cycles, for a single pass, your right why waste the time.

    Now call it 2G times... That is real processing power waste.

    • (Score: 0) by Anonymous Coward on Monday August 26 2019, @06:34AM (1 child)

      by Anonymous Coward on Monday August 26 2019, @06:34AM (#885540)

      By your logic putting two lines of code in a function and calling the function 2k times is the real rea processing power waste. Calling functions is expenssive.

      • (Score: 2) by Pino P on Monday August 26 2019, @03:36PM

        by Pino P (4721) on Monday August 26 2019, @03:36PM (#885677) Journal

        Finding opportunities to expand a function inline as if it were a macro is why compiler developers invented link-time optimization (LTO). SQLite uses a related technique called "amalgamation," where the entire library is compiled as a single translation unit.