23

(This is a question about gcc and clang, but might apply to other compilers.)

If I compile my C or C++ code, and generate debug info using the -g switch, does this in itself degrade performance of the compiled program in any way...

  1. With minimum optimization (-O0)?
  2. With maximum optimization (-O3)?

Note: I don't mean the performance penalty of having to parse/load the executable, which is larger due to the extra content; I mean the code that's run.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • 1
    Compare a program compiled with `-g` and the same program compiled without `-g`. You should see no difference in the actual generated code. – Some programmer dude Aug 30 '16 at 08:45
  • 4
    @JoachimPileborg: The fact that I don't see a difference in the code generated for a specific program does not necessarily mean there's never any difference... – einpoklum Aug 30 '16 at 08:50
  • Strictly seen: Yes, at least when you keep the debugging symbols within the executable, because that means there's more to load (or skip over during loading). This is IMO negligible in practice, though. – Daniel Jour Aug 30 '16 at 08:52
  • @DanielJour: Yeah, ok, fair point, but it's not what I meant. Added a note to clarify. – einpoklum Aug 30 '16 at 08:56
  • As far as I'm aware, -O3 will degrade the value of the debug information, not the other way around. – StoryTeller - Unslander Monica Aug 30 '16 at 09:07

3 Answers3

20

I do not think there is any performance difference. Indeed the generated code would be the same and -g is usable with -O according to the documentation here. Besides, debugging symbols aren't written into the code but into another section called "debug section" which isn't even loaded at runtime (only by a debugger)

-g will not change what optimizations are run, or code generated. This is gcc policy as stated here

However it might be useful to note that the same documentation states that:

The shortcuts taken by optimized code may occasionally be surprising: some variables you declared may not exist at all; flow of control may briefly move where you did not expect it; some statements may not be executed because they compute constant results or their values are already at hand; some statements may execute in different places because they have been moved out of loops. Nevertheless it is possible to debug optimized output. This makes it reasonable to use the optimizer for programs that might have bugs.

So in the end the debugging will never hurt your optimisation but the opposite is false and using -O3 might degrade your debugging information (by deleting useless variables for instance).

Note that it may be better in that case to use -Og (as stated here) since it will:

Optimize debugging experience. -Og enables optimizations that do not interfere with debugging. It should be the optimization level of choice for the standard edit-compile-debug cycle, offering a reasonable level of optimization while maintaining fast compilation and a good debugging experience.

However this would impact performance because some optimisation passes that would interfere with the debugging would not be done.


Edit:

The links and quotes answer your question for gcc.It might not apply to other compilers such as clang. However I have found some documentation as well for clang. For instance here:

Basically, the debug information allows you to compile a program with “-O0 -g” and get full debug information, allowing you to arbitrarily modify the program as it executes from a debugger. Compiling a program with “-O3 -g” gives you full debug information that is always available and accurate for reading (e.g., you get accurate stack traces despite tail call elimination and inlining), but you might lose the ability to modify the program and call functions where were optimized out of the program, or inlined away completely.

LBes
  • 3,366
  • 1
  • 32
  • 66
  • @einpoklum it was just a reminder. But I suppose the beginning answers your questions. It was just additional information as you may know it but others may not. I saw that you removed your comments, any reasons for that? I think I did answer your question here – LBes Aug 30 '16 at 09:53
4

The -g flag adds debugging information to the binary. This exists in a separate section (.stab and .stabstr) of the executable from the .text CPU run bit. When run outside the debugger, the debug section is not loaded by the operating system loader. The debug info can also easily be stripped out using the strip utility to generate binaries that are identical to one compiled without the -g flag.

Normally however when you want to debug stuff you will compiled without optimizations and the NDEBUG preprocessor macro. However these things are not controlled by the -g flag.

doron
  • 27,972
  • 12
  • 65
  • 103
1

There wont be any performance hit if you run it outside a debugger. The debug symbols are to aid the debugging. The generated code shall be the same in both the cases.

aneesh jose
  • 381
  • 2
  • 8