Quote:
Originally Posted by tuxdev
inline is just a hint.
|
It is also an exception to the "one definition rule".
If you want to define a function in a header file that is included in multiple cpp files, the function must be inline to avoid violating the "one definition rule".
Quote:
It's ignored for recursive functions and functions that are too large.
|
I've mainly looked at that topic in ICC, rather than GCC. Except for that ODR exception, ICC normally ignores almost anything the programmer might try to influence whether a function is inlined or not. ICC just decides for itself what is "best" in each case. Often those are terrible decisions.
A recursive function can be inlined and ICC often does. Inlining a function does not imply that everything it calls gets inlined. Obviously you can't inline everything a recursive function calls. It is sometimes effective to inline a function without inlining everything it calls. That is especially true of recursive functions. ICC does a pretty good job of inlining one or more levels of a recursive function. I don't know how (or how well) GCC handles that.
ICC's worst inlining decisions seem to apply the same top down logic (needed to inline a few levels of a recursive function) but apply them to more common non recursive situations where it is a terrible idea. Inlining in most cases would be much smarter bottom up. GCC does better than ICC in specific cases where ICC inlining decisions are especially stupid. But that is a very biased sample set. I know much less about GCC in more general cases.
Quote:
Originally Posted by gothrog
Is it good to set a recursive function as inline?
I would think it would increase the performance speed of the program.
|
A small recursive function usually runs a lot faster because ICC inlines a few levels of it into itself. In ICC, that depends only on which optimizations are enabled. The inline keyword in that situation has no effect on whether (or how much) the function is inlined.
I expect GCC can also speed up a small recursive function with inlining. But I don't know how to predict the details short of trying it and looking at a disassembly of the results.