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

  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
    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"

  5. #5
    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.

  6. #6
    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??

  7. #7
    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.

  8. #8
    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.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    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

  10. #10
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Well, actually even small function are much bigger sometimes when we compare them in assembly.
    In most of the cases a function will be bigger than one simple instruction in assembly, i.e.

    call theFunc

    And if the function is called million of times only then this will be one extra instruction overhead apparent enough to remove.

  11. #11
    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.

  12. #12
    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!

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    But I think inline will be useless if the function happens to be called more than once!
    Not if the function call overhead is greater than the function body itself, as in the case matsp described.
    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

  14. #14
    Banned
    Join Date
    Nov 2007
    Posts
    678
    What if it was setting a double here?
    But I will not go in trivial issues here. My original intent was to define and declare the function at same place and get
    a natural function, not some inline expansion!

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    My original intent was to define and declare the function at same place and get a natural function, not some inline expansion!
    An inline function is a "natural" function, for the purposes of scope and pretty much everything else. Inlining or not inlining functions is a matter of optimisation and will not affect the functional behaviour of your program.

    Now, member functions defined in the class definition are automatically inlined. You can disable this with the -fno-default-inline option.
    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

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