2

In my rendering code, I pass string literals as a parameter to my rendering blocks so I can identify them in debug dumps and profiling. It looks similar to this:

// rendering client
draw.begin("east abbey foyer");
    // rendering code
draw.end();

void Draw::begin(const char * debugName){
    #ifdef DEBUG
        // code that uses debugName
    #endif
    // the rest of the code doesn't use debugName at all
}

In the final program, I wouldn't want these strings to be around anymore. But I also want to avoid using macros in my rendering client code to do this; in fact, I would want the rendering client code to KEEP the strings (in the code itself) but not actually compile them into the final program.

So what I'm wondering is, if I change the code of draw.begin(const char*) to not use it's parameter at all, will my compiler optimize that parameter and it's associated costs away (perhaps even going so far as to exclude it from the string table)?

Anne Quinn
  • 12,609
  • 8
  • 54
  • 101
  • 2
    Why do you care? Do you have some reason to think this is an issue? Why make problems where there are none? – David Schwartz Jan 20 '16 at 20:59
  • Related: http://stackoverflow.com/questions/14291549/will-compiler-optimize-out-unused-arguments-of-static-function – NathanOliver Jan 20 '16 at 21:02
  • @DavidSchwartz - mostly just curiosity as to what would happen. This is actually the easiest way of structuring things, so the optimization would just be a happy bonus, and less pressure to provide witty strings for any dataminers – Anne Quinn Jan 20 '16 at 21:09

4 Answers4

2

Beware of anyone giving you concrete answers to questions like this. The only way you can be really certain what your compiler is doing in your configuration is to look at the results.

One of the easiest ways to do this is to step through the disassembly in the debugger.

Or you can get the compiler to output an assembly listing (see How to view the assembly behind the code using Visual C++?) and examine exactly what it is really doing.

Community
  • 1
  • 1
Damyan
  • 1,563
  • 9
  • 15
1

In practice, it depends on whether the compiler has access to the source code of the implementation of Draw::begin()

here's an illustration:

#include <iostream>

void do_nothing(const char* txt)
{

}

int main()
{
    using namespace std;
    do_nothing("hello");
    return 0;
}

compile with -O3:

yields:

_main:                                  ## @main
    .cfi_startproc
## BB#0:
    pushq   %rbp
Ltmp3:
    .cfi_def_cfa_offset 16
Ltmp4:
    .cfi_offset %rbp, -16
    movq    %rsp, %rbp
Ltmp5:
    .cfi_def_cfa_register %rbp
    xorl    %eax, %eax
    popq    %rbp
    retq
    .cfi_endproc

i.e. the string and the call to do_nothing is entirely optimised away

However, define do_nothing in another module and we get:

_main:                                  ## @main
    .cfi_startproc
## BB#0:
    pushq   %rbp
Ltmp0:
    .cfi_def_cfa_offset 16
Ltmp1:
    .cfi_offset %rbp, -16
    movq    %rsp, %rbp
Ltmp2:
    .cfi_def_cfa_register %rbp
    leaq    L_.str(%rip), %rdi
    callq   __Z10do_nothingPKc
    xorl    %eax, %eax
    popq    %rbp
    retq
    .cfi_endproc

    .section    __TEXT,__cstring,cstring_literals
L_.str:                                 ## @.str
    .asciz  "hello"

i.e. a redundant load, call and the address of the string is passed.

So if you want to optimise debug info away you'll want to implement Draw::begin() inline (in the same way the stl does).

Richard Hodges
  • 68,278
  • 7
  • 90
  • 142
0

Yes, you could muck about with optimization and that may or may not work depending on the version of your compiler etc.

A much better way to do this is explicitly, not with MACROs (god forbid), but with a simple function that either generates the tag you want for debug purposes or does nothing for production code.

You want to look at std::assert and perhaps use it.

Paul Evans
  • 27,315
  • 3
  • 37
  • 54
0

The possibility of doing so will depend on visibility of function in the call site. If function body is not visible, optimization discussed will not be possible at all. If function is visible, than yes - you can not even guarantee there will be a function call!

SergeyA
  • 61,605
  • 5
  • 78
  • 137