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

  1. #16
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Thanks a lot, you finally gave me, what I had been asking for.
    That's a golden option. I love g++

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Thanks a lot, you finally gave me, what I had been asking for.
    Actually, matsp gave you several suggestions in post #5 ("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".") and I elaborated on his last suggestion in post #9, but apparently you did not read the documentation I linked to.
    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

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

  4. #19
    Banned
    Join Date
    Nov 2007
    Posts
    678
    laserlight: I would read that later, since i thought you were giving me information regarding optimizations. Thanks.

    Mats: You are a programming master! How the hell you know everything?

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by manav View Post
    laserlight: I would read that later, since i thought you were giving me information regarding optimizations. Thanks.
    Inlining _IS_ an optimization - it will reduce the overhead of calling the function by stuffing it inside the code.
    Mats: You are a programming master! How the hell you know everything?
    I wish I _could_ know EVERYTHING. I don't think ANY person knows EVERYTHING about programming, although I know (of) a few people who know a huge amount.

    --
    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. #21
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Why are you a Kernel Hacker?
    Is kernel hacking fun? What exactly does it mean? Can you (or have done already) hack Vista also?

  7. #22
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by manav View Post
    Why are you a Kernel Hacker?
    Is kernel hacking fun? What exactly does it mean? Can you (or have done already) hack Vista also?
    I use the term Hacker in it's original meaning - someone who understands and works inside the kernel of the OS (same meaning as for example Linus Torvalds would be considered a Kernel Hacker). I have worked on Windows graphics drivers, which are kernel drivers. I have worked on the Xen virtualization project, which is "super-kernel" (as in, manage multiple OS kernels running concurrently in the same machine - so you can have Windows and Linux running at exactly the same time on separate cores, or sharing the same core just like applications do inside Windows or Linux).

    Edit: And yes, working on kernel code _IS_ fun - in the same way as riding a motorbike is more fun than driving a car - there's a bit more danger, but when you do it right, it's so much more rewarding.

    --
    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. #23
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Don't use -fno-default-inline. Just use -Os, and let the compiler handle this stuff. Really. -Os means optimize for code size, so the compiler will not use optimizations that increase performance at the cost of code size. This includes the inlining heuristics - at -Os, newer GCCs will only make the early-inlining pass, the one that inlines functions smaller than the call overhead. (Older GCCs - pre 4.1, I think - don't have an early-inlining pass, but the effect is still similar, I think.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #24
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by CornedBee View Post
    Don't use -fno-default-inline. Just use -Os, and let the compiler handle this stuff. Really. -Os means optimize for code size, so the compiler will not use optimizations that increase performance at the cost of code size. This includes the inlining heuristics - at -Os, newer GCCs will only make the early-inlining pass, the one that inlines functions smaller than the call overhead. (Older GCCs - pre 4.1, I think - don't have an early-inlining pass, but the effect is still similar, I think.)
    This obviously depends on what you want to achieve. -Os produces the smallest possible code, which may well inline a fairly large function if it's only called once. -fno-default-inline does
    Do not make member functions inline by default merely because they are defined inside the class scope (C++ only). Otherwise, when you specify -O, member functions defined inside class scope are compiled inline by default; i.e., you don't need to add `inline' in front of the member function name.
    --
    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.

  10. #25
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Yes, indeed. Of course, making the smallest possible code is a sensible goal. Not inlining functions because you don't like inlining is not.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #26
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Thanks CornedBee. I should have been thinking on this track. Its better to use -Os.
    Another golden option!

  12. #27
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by CornedBee View Post
    Yes, indeed. Of course, making the smallest possible code is a sensible goal. Not inlining functions because you don't like inlining is not.
    Agreed - there is one exception: When debugging optimized code [where the non-optimized code isn't showing the problem for some reason], and you want to set a breakpoint in some small function - the inliner will potentially inline this, which means that the breakpoint is non-functional.

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

  13. #28
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Depends on how well the debugger copes with compiler transformations, and how well the compiler documents them. In optimized code, there's more things to worry about, like split statements, coalesced statements, statements completely computed at compile time, and so on.

    Or you have screwed-up behaviour even with debug builds. For example, if you set a breakpoint to a code line in gdb, and that code line is in a template implementation, the breakpoint is set for one of the instantiations, and you have neither clue nor control over which one that is.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #29
    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!

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

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