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: 1, Funny) by Anonymous Coward on Sunday August 25 2019, @05:07PM (3 children)

    by Anonymous Coward on Sunday August 25 2019, @05:07PM (#885234)

    C, Algol, and Fortran are obsolete languages for old people. Rockstar coders don't use old dead languages for social media marketing. Coding today is about selling an app to a billion idiot users, not about being clever with a computer.

    Starting Score:    0  points
    Moderation   +1  
       Funny=1, Total=1
    Extra 'Funny' Modifier   0  

    Total Score:   1  
  • (Score: 2) by barbara hudson on Sunday August 25 2019, @07:17PM (2 children)

    by barbara hudson (6443) <barbara.Jane.hudson@icloud.com> on Sunday August 25 2019, @07:17PM (#885318) Journal
    Coding today is all about tracking people to sell ads. If your app doesn't track people as invasively as possible (remember the dildo that could be worked via a smartphone app that sent data to the mother shit?) you are obsolete.

    Speaking of which, WTF is up with getting rid of the "volatile" keyword?

    --
    SoylentNews is social media. Says so right in the slogan. Soylentnews is people, not tech.
    • (Score: 2) by Immerman on Monday August 26 2019, @04:56PM (1 child)

      by Immerman (3985) on Monday August 26 2019, @04:56PM (#885705)

      Say what?!? They're getting rid of volatile? I hadn't heard that, and it's madness. Not that I've used it very often, maybe once every few years, but when it's needed....

      What's the alternative? Especially in a world where multi-threading has become compulsory if you want decent performance. Just assume everything is volatile and eliminate vast swaths of optimizations? Lock anything that might possibly be modified elsewhere behind mutexes? ...I suppose that might actually be good practice in a lot of cases, but I shudder at the thought of the performance penalties it would impose.