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, Troll) by Anonymous Coward on Sunday August 25 2019, @02:20PM (8 children)

    by Anonymous Coward on Sunday August 25 2019, @02:20PM (#885173)

    They could try using ProggyClean with blackboard theme for readability. Code-wise, they could use Contributor Covenant to squash any inclusivity bugs.

    Starting Score:    0  points
    Moderation   -1  
       Troll=1, Total=1
    Extra 'Troll' Modifier   0  

    Total Score:   -1  
  • (Score: 0) by Anonymous Coward on Sunday August 25 2019, @02:46PM (4 children)

    by Anonymous Coward on Sunday August 25 2019, @02:46PM (#885185)

    inclusivity bugs

    Here we go, was not enough to watch for null pointers and bounds but now also for minorities and LGTB code.
    What's next, an FSM exception code?

    • (Score: 3, Funny) by The Mighty Buzzard on Sunday August 25 2019, @02:56PM

      Nope, exceptions are problematic since they imply that the caller is somehow exceptional.

      --
      My rights don't end where your fear begins.
    • (Score: 0) by Anonymous Coward on Sunday August 25 2019, @07:28PM (1 child)

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

      What should I do if my NullPointerException has completed hir transition from an ArrayOutOfBoundsException?

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

      by Anonymous Coward on Monday August 26 2019, @03:03PM (#885662)

      More like the owners and managers who recognize harassment and gay bashing don't hire people who create hostile work environments, and intelligent open source projects don't give a shit about your mad skillz until you learn to be a human being who doesn't hold people's sexual orientations or physiologic characteristics against them.

      I suppose you're against working with all them cripples and darkies too.

  • (Score: 0) by Anonymous Coward on Sunday August 25 2019, @02:57PM (2 children)

    by Anonymous Coward on Sunday August 25 2019, @02:57PM (#885195)

    Please don't let this guy on our team. Please don't let this guy on our team. Please don't let...

    • (Score: 0) by Anonymous Coward on Sunday August 25 2019, @06:48PM (1 child)

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

      You are just scared the boss will find out your code is riddled with exclusivity.

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

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

        Yes, it's scary. She may get promoted and asked to supervise snowflake's blackbox gay code, and that sucks.