0

I want to write an application in C++ with the best runtime performance. So I decided to inline all methods.

I have the problem mentioned here.

It gives me undefined reference error if I use inline keyword inside cpp file in both MSVC 2015 and MinGW compilers.

But if I want to inline all methods inside the header file, there would be no cpp files needed. Is that true? Why is that?

Community
  • 1
  • 1
Matin Lotfaliee
  • 1,745
  • 2
  • 21
  • 43
  • 1
    Inline nowadays is not about performance – deviantfan Nov 01 '16 at 19:01
  • 4
    Write the code so it works. If it is slow then use a profiler to determine what is slow and fix that part. Repeat as needed. Don't just decide that you need to inline everything. – crashmstr Nov 01 '16 at 19:02
  • @deviantfan so what is your suggestion about performance? – Matin Lotfaliee Nov 01 '16 at 19:02
  • @MatinLotfaliee The way this question is asked tempts me to answer "leave it to your compiler". Sure, it's possible to surpass automatic optimizations, but not trivial. – deviantfan Nov 01 '16 at 19:03
  • 1
    @MatinLotfaliee: The rules of optimization: 1) Don't (make your program robust first). 2) Don't (make your program readable) 3) Profile. Optimize only those functions requiring optimization; see what the User's think. – Thomas Matthews Nov 01 '16 at 19:04
  • 1
    If you've got a performance problem, chances are it's because you're using the wrong algorithm or data structure, not because you failed to inline everything... – Roger Lipscombe Nov 01 '16 at 19:08
  • Note that if you use plain `inline` the compiler can ignore it and avoid inlining. There are compiler-specific directives that can force inlining, but using them is not a good idea. Inlining large functions that are seldom called will cause the executable to bloat, yet eliminate very few call and ret instruction. The outcome will likely be code that runs slower than without inlining. – Eran Nov 01 '16 at 19:23

2 Answers2

3

The keyword inline has nothing to do with performance in this day and age and nothing to do with inlining a function!

In fact it has to do with the One Definition Rule (or ODR)!

The ODR states that a C++ program shall have only one definition of each function.

This means that the following is will produce an error:

file.cpp

void fun() {}

main.cpp

void fun() {}

This is an error, because there are two definitions of the same function in two different translational units (.cpp files) which is a violation of the ODR.

Now the inline keyword allows you to get around this. It allows you to define the same function in multiple translational units, as long as the function body is exactly the same! This allows you to define the function in a header file which can then be included into multiple .cpp files.

That being said. What you described will not cause a performance slowdown. The compiler will inline the correct functions in the appropriate time. It will make your code run faster than you could ever do it yourself.

DeiDei
  • 10,205
  • 6
  • 55
  • 80
  • This is wrong. In my IAR compiler, in Debug mode (no optimization), inlining a function can make a big difference. On the embedded system I'm working on, we are not using Release mode. The debug code is released without symbols (to make debugging returned products easier). I have shown in the assembly language listings that inline does make a difference. – Thomas Matthews Nov 01 '16 at 19:10
  • 2
    @ThomasMatthews You're releasing a finished product in Debug mode? I can't wrap my head around that. Anyways the point I was trying to make was that you shouldn't do the compiler's job by stating which function to inline away. It can do it for you. – DeiDei Nov 01 '16 at 19:14
  • 1
    @ThomasMatthews that's equivalent to putting `register` in front of variables in order to save memory accesses. In an un-optimized build, it could make a difference. But anyone looking for performance will have the compiler optimize the code, which will eliminate the need for `inline` and `register`. And btw, overzealous inlining might actually hurt performance. A compiler is likely to avoid this pitfall. – Eran Nov 01 '16 at 19:19
  • @eran: In debug mode, (no optimization), the keyword "register" also assists in performance. I've seen this in the assembly language listing. I'm still trying to figure out how to make the IAR ARM compiler generate an LDM or STM instruction. :-) – Thomas Matthews Nov 01 '16 at 19:25
  • 1
    @ThomasMatthews that's my point - `register` can definitively speed up debug builds, but on release builds it's useless because using registers is probably the first thing an optimizing compiler will do for you. Since on most cases the compiler takes care of code optimization (and it does way more than inlining and assigning registers), the use case for `inline` and `register` is very limited. – Eran Nov 01 '16 at 19:34
  • 1
    "Debug mode (no optimization) ..... embedded system" - Looky you with all your extra resources to throw around willynilly. – mascoj Nov 01 '16 at 20:10
1

No, this is not true. Your main function cannot be inline by definition. See this link for more information.

Community
  • 1
  • 1
mascoj
  • 1,313
  • 14
  • 27