This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: a question about function inlining
- From: LLeweLLyn Reese <llewelly at lifesupport dot shutdown dot com>
- To: "Kollschen, Chester (Logica)" <extern dot chester dot kollschen at volkswagen dot de>
- Cc: "'gcc-help at gcc dot gnu dot org'" <gcc-help at gcc dot gnu dot org>
- Date: 23 Jul 2003 07:50:10 -0700
- Subject: Re: a question about function inlining
- References: <EC13018F24FAD611A91C0008C791F364022AD61C@devwagwodx0009.wob.vw.de>
"Kollschen, Chester (Logica)" <extern.chester.kollschen@volkswagen.de> writes:
> Hello.
>
> I know the GCC C++ compiler can inline often-called functions automatically
> for performance reasons. I have to set the option -O3 for that. OK, but does
> it do inlining even across libraries?
Not as far as I know. You need to make the function's definition
availible in the translation unit of the call site.
>
> Say, I have a class like that:
>
> class Test {
> public:
> Test();
> ~Test();
> void called_very_often();
inline void called_very_often();
> };
>
> Test::Test() {
> }
>
> Test::~Test() {
> }
>
> void Test::called_very_often() {
inline void Test::called_very_often() {
and put into header file.
> // one or two statements are placed here
> }
>
> This class is compiled into a static library called Test.lib or libtest.a.
> When I have a C++-Code like this:
>
> int main(int argc, char** argv) {
> Test * t = new Test();
> [...]
> t->called_very_often();
Calling the function with a pointer to class type makes no difference
unless the function is virtual (which may prevent inlining).
> [...]
> delete t;
> }
>
> When compiled with -O3, are there chances that the GCC C++ compiler will
> inline the member method called_very_often() of library class Test in main()
> even though it's compiled externally and called via a pointer? If not, what
> could I do to force that?
I don't know, except to write the code inline directly. You can also
use -Winline, which will generate a warning if a function is not
inlined. -finline-limit=some_large_number will tell gcc to inline
larger functions. See:
http://gcc.gnu.org/onlinedocs/gcc-3.3/gcc/Optimize-Options.html#Optimize%20Options
and search for -finline-limit
http://gcc.gnu.org/onlinedocs/gcc-3.3/gcc/Warning-Options.html#Warning%20Options
and search for -Winline