Thread: how to make sure g++ will not inline this?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Banned
    Join Date
    Nov 2007
    Posts
    678

    how to make sure g++ will not inline this?

    see this:
    Code:
    class Series
    {
    	int from, to, delta;
    public:
    	Gamma(int f, int t, int d)
    	{
    		from = f;
    		to = t;
    		delta = d;
    	}
    	
    	void run() const
    	{
    		for (int f=from; f!=to; f+=delta)
    		{
    			cout << "Stepping: " << f << endl;
    		}
    	}
    };
    I wrote the code in this way because it's more convenient. Will now G++ make the functions inline? Can I avoid this?
    Last edited by manav; 03-28-2008 at 03:17 AM. Reason: Error corrected.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    It will only suggest the compiler to inline it but it may discard this suggestion as it judges better.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's better to break up definition and implementation for several reasons...
    Plus there's no real easy way to tell a compiler not to inline. It inlines at its own discretion, if it thinks your program might benefit from it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There is simple ways to avoid inlining. There are no sure-fire ways to ensure that something is inlined.

    The don't inline this way is to put the function in a different source file. Or call it through a function pointer. Or use some compiler option to say "don't inline".

    If you want the best chance of something being inlined:
    1. Make sure the function is visible to the compiler - which usually means put it in a header file.
    2. Keep it short.
    3. Make sure it's not called from too many places.
    4. Don't take the address of the function (as in using it with a function pointer).
    5. Don't put static variables in the function.

    gcc/g++ is actually very good at inlining even large functions, as long as they are only called from a few places - even really big functions can be inlined in this case.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Banned
    Join Date
    Nov 2007
    Posts
    678
    I think splitting my code in .h and .cpp files is good if I am writing reusable stuff.
    I was writing a one short program. But any way, I actually like the idea of separate .h and .cpp files.
    But I found this method to be more convenient, except the risk that g++ will inline it all.

    iMalc: Sorry about the typo. I just wrote it to show some example. Real point is to declare functions and define them in place and still avoid inline.

    matsp: Thanks for nice tips about how to get a function inlined!

    Can I safely assume that if a function is small, it will be inlined if defined this way.
    But if the function is not so small then g++ will not inline, even when defined in the way I have shown??

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Hi,

    Quote Originally Posted by matsp View Post
    1. Make sure the function is visible to the compiler - which usually means put it in a header file.
    are you sure this is a good idea? AFAIK putting implementation in header files is a time bomb, because it may lead to linker errors in the future (if the header suddenly gets included in more than one compilation unit and this units gets linked together after compilation - or better don't get linked, because of multiple definition error). Of course it will work, if the function actually gets inlined (because then no definition is involved). But if the compiler decides it doesn't, the problem will emerge I guess.

    A related question would be: Does the inline keyword even suggest the compiler to inline, if the implementation is given outside the header file? There do I have to put the keyword then, in the header/decleration, in the .cpp/implementation or both?

    Thank you!

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Most modern compilers (such as gcc and MS VC++) in default settings completely ignore "inline" as a keyword when it comes to what to inline and what to not inline. I know MSVC has a switch to say "inline by inline keyword only", but it's not the default setting.

    However, if you put functions in a header file, they should be marked static, or part of a class.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Quote Originally Posted by matsp View Post
    However, if you put functions in a header file, they should be marked static, or part of a class.

    If the inline keyword gets ignored and definitions for non-static functions inside header files will cause linker problems, a non-static function can never be inlined. Is this correct?

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by manav View Post
    see this:
    Code:
    class Series
    {
    	int from, to, delta;
    public:
    	Gamma(int f, int t, int d)
    	{
    		from = f;
    		to = t;
    		delta = t;
    	}
    	
    	void run() const
    	{
    		for (int f=from; f!=to; f+=delta)
    		{
    			cout << "Stepping: " << f << endl;
    		}
    	}
    };
    I wrote the code in this way because it's more convenient. Will now G++ make the functions inline? Can I avoid this?
    You obviously have a misunderstanding. Inlining will in no way screw up your program, only your bugs will do that. In this case it looks to me like your bug is here:
    Code:
    		delta = t;
    shouldn't that be d instead of t!

    You also aren't using initialisation lists, but you should learn to do that.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Does it really matter? What's so horrible about inlining?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Of course it matters. If you care about speed, size.
    Inlined function will be a few cycles faster but a big blow on memory.

    And I just want to make sure that I will get a function when I wrote a function and not a compiler decided macro.
    But, for really small functions it is ok for me.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Can I safely assume that if a function is small, it will be inlined if defined this way.
    But if the function is not so small then g++ will not inline, even when defined in the way I have shown??
    Read the GCC docs on optimization (or how not to optimize).

    And I just want to make sure that I will get a function when I wrote a function and not a compiler decided macro.
    But, for really small functions it is ok for me.
    An inline function is not a macro. Besides small functions, inlining is also good if the function is only called a few times*.

    EDIT:
    * Then again, maybe not, if it is called from a complex function. But yeah, that's why I rely on the compiler to decide if it likes my suggestion of inlining or not.
    Last edited by laserlight; 03-28-2008 at 03:30 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm with iMalc here - I think you have some sort of misconceptions about inlining.

    Correct use of inlining [which to a large extent is enforced by the compiler] leads to compact and efficient code.

    Obviously, excessive inlining can be bad, and calling tiny functions (like getters and setters) often cause bigger code than inlining the function, e.g.
    Code:
    class X
    {
       int x;
    public:
       void setx(int _x) { x = _x }
    };
    Inlined, this would be one mov instruction, without, it will be at the very least three instructions (call, mov, ret), but more likely five or six (mov, push, call, mov, ret, cleanup) [cleanup is either an add instruction or a pop instruction].

    In bytes, the simple three instruciton case will take up 5 bytes per call to setx(), whilst the inline mov instruction takes 3 bytes or so [it may be 5, but then that should really be compared to the longer sequence, which again takes up nearly around a dozen bytes].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Mats: Yeah, but this holds only if the function is called just once or may be twice or even thrice, may be!
    Inline the function will always save a few instructions, like saving registers, getting parameters from stack, clearing stack etc.
    But I think inline will be useless if the function happens to be called more than once!

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by manav View Post
    Mats: Yeah, but this holds only if the function is called just once or may be twice or even thrice, may be!
    Inline the function will always save a few instructions, like saving registers, getting parameters from stack, clearing stack etc.
    But I think inline will be useless if the function happens to be called more than once!
    It doesn't make much difference what the data type is - a double passed will be (fld, fst) inlined, and (sub sp, fld, fst, call, fld, fst, ret, add sp) as a direct call - so it's still a 5 instructions EXTRA for the call compared to the inlined version.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inline Definitions and Declarations?
    By legit in forum C++ Programming
    Replies: 1
    Last Post: 06-15-2009, 01:59 PM
  2. Win32 Common Controls in C++, how do i make and use them?
    By C+noob in forum Windows Programming
    Replies: 6
    Last Post: 01-09-2006, 11:53 AM
  3. Inline functions and inheritance
    By hpy_gilmore8 in forum C++ Programming
    Replies: 3
    Last Post: 01-14-2004, 06:46 PM
  4. make all rule
    By duffy in forum C Programming
    Replies: 9
    Last Post: 09-11-2003, 01:05 PM
  5. Question about atheists
    By gcn_zelda in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 08-11-2003, 11:50 AM