Function Method Definition in .cpp vs .h

To reduce the compilation time of a fairly large structure at work, I considered defining class class methods in .h files for their associated .cpp file, if they were either very large or required to compile, which could be transferred to the associated .cpp file . For clarity, here's a far-fetched example (although Foo::inc is a tiny method)

main.cpp

 #include "Foo.h" int main(int argc, char** argv) { Foo foo(argc); foo.inc(); return foo.m_argc; } 

Foo.h before (no Foo.cpp yet) :

 class Foo { public: int m_argc; Foo (int argc) : m_argc(argc) {} void inc() { m_argc++; } }; 

Foo.h after :

 class Foo { public: int m_argc; Foo (int argc) : m_argc(argc) {} void inc(); }; 

foo.cpp

 #include "Foo.h" void Foo::inc() { m_argc++; } 

A long time ago, a colleague mentioned that there may be times when this can lead to a slowdown in performance at runtime. I searched this case on Google, but didn’t seem to find it, the accepted answer to this question is the closest I could find, but it’s not, just mentioning that this can happen: Moving inline methods from a file header to .cpp files

On the side of the note, I am not interested in the case where the method explicitly uses inline , the answer I linked above was the closest I could find what I was looking for

In which case (if any) can slowdowns occur?

+6
source share
2 answers

Your original method void inc() { m_argc++; } void inc() { m_argc++; } was automatically inline , so the compiler was allowed to replace the call with the built-in version.

As soon as you move the method from the class definition to the module, this method is no longer inline , the built-in extension is not executed, there is a standard method call, and the result may be slower.

+7
source

Reducing header dependencies is always a good idea to reduce compilation time. This is one of the best elements on lists, such as What methods can be used to speed up C ++ compilation time?

I would recommend - if not already done - to take a look at the main players who eat up your compilation time with Profiling the C ++ Compilation Process

And there are helpers for sorting your inclusion dependencies, see Automating #include refactoring in C ++

The question of moving code to source files will slow down your performance at runtime: it depends. Generally speaking, you can say that you give the compiler the ability to inline if you have a function in the header.

I like to quote from the C ++ FAQ - Inlining :

Do built-in features improve performance?

Yes and no. Sometimes. May be.

There are no simple answers. built-in functions can make code faster; they can make it slower. They can make an executable file they can be smaller. They can cause beating, they can prevent beating. And they can be and often are completely irrelevant for speed.

What the compiler, or perhaps later the linker, does, depends on which compiler compiler you use and what the compiler / linker is .

See everything that happens when in inline - Using the GNU compiler compilation :

... GCC does not perform any functions when it does not optimize ...

Some links:

+2
source

Source: https://habr.com/ru/post/989897/


All Articles