Skip to content

Instantly share code, notes, and snippets.

@dtoma
Last active February 22, 2017 06:58
Show Gist options
  • Save dtoma/b4265656d1b560665f390857914daa52 to your computer and use it in GitHub Desktop.
Save dtoma/b4265656d1b560665f390857914daa52 to your computer and use it in GitHub Desktop.
gcc attributes for perf tuning

error/warning

Code exammple:

#include <string>

std::string __attribute__((warning("getString not inlined/removed"))) getString() {
  return "Foo";
}

int main() {
  return getString().size();
}

Compiling this with gcc 6.3 and no flags, we get the following warning:

<source>: In function 'int main()':
<source>:10:19: warning: call to 'getString' declared with attribute warning: getString not inlined/removed
return getString().size();
~~~~~~~~~^~

Whereas with -O3, we don't get a warning and the assembly is a lot shorter:

getString[abi:cxx11]():
        lea     rdx, [rdi+16]
        mov     rax, rdi
        mov     BYTE PTR [rdi+18], 111
        mov     QWORD PTR [rdi+8], 3
        mov     BYTE PTR [rdi+19], 0
        mov     QWORD PTR [rdi], rdx
        mov     edx, 28486
        mov     WORD PTR [rdi+16], dx
        ret
main:
        mov     eax, 3
        ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment