2

I always read that inlined functions should be short functions because otherwise the executable gets bloated with too many copies of the same code.

However, I try to refactor my code and write small helper functions, which are often not so small (20-30 lines) and often only used by exactly one other function, e.g. to avoid the do{...}while(false); idiom.

Hence my questions:

  • Wouldn't it be a good idea to inline a long function, if it is only used by exactly one other function regardless of how long it is? The size of the executable would be the same, and one function call would be saved.
  • Would a good compiler consider this? Or is the length a strong criterion to not-inline a function. Does this depend on whether I explicitly write inline, as compilers seem to ignore this mostly?
Fabian
  • 4,001
  • 4
  • 28
  • 59
  • 6
    Modern compilers ignore the `inline` keyword in code. They themselves inline methods when they feel it is required to do so. – CinCout Jun 14 '16 at 10:21
  • 1
    The `do{}while(false)` trick is meant to be used in macros to have intuitive expansion, why would you use it in functions ? – Quentin Jun 14 '16 at 10:23
  • Why do you want functions inlined? Speed, usually. But if the function is "large" for some definition of large, It won't all fit in the instruction pipleine and you'll probably end up with cache misses.... What advantage would one long function being inlined give you? – doctorlove Jun 14 '16 at 10:24
  • @Quentin I guess OP wants to give his compiler no choice by converting the function to a macro. Now the preprocessor must inline, at the expense of having macro-based solution, which is inferior in all respects other than inlining. – Sergey Kalinichenko Jun 14 '16 at 10:27
  • Unless you program for embedded, who cares about size nowadays? Let the compiler decide whether he wants to inline or not, he knows best. – Sebastian Hoffmann Jun 14 '16 at 10:30
  • 1
    When to do inlining is at compiler's discretion (irrespective of `inline` keyword is used or not). It may ignore or assume `inline` depending on how its compilation model is implemented. Yes, a single big function can be trivially inlined by a good compiler when it's used less number of times (again compiler implementation specific, if less = 1 or more). In the linked dupe another post is referred, which discusses this concept further. Here is [one good answer](http://stackoverflow.com/a/1932580/514235) from it. In general, length may not be the only criteria for inlining in modern compilers. – iammilind Jun 14 '16 at 11:04
  • @Quentin: I use normal functions INSTEAD of the do{}while(false) trick. Instead of writin a helper function which I just use once, I could "inline myself" by using the mentioned trick. My question aims at understanding if a compiler inlines a long function if it is only used once, which seems to be sensible IMHO. – Fabian Jun 14 '16 at 11:15
  • @Fabian Oh, you mean you avoid macros, not the trick alone. My bad. – Quentin Jun 14 '16 at 11:49

1 Answers1

0
  • Keep your functions small in 40-60 code lines (excluding comments/new-lines).
  • Pass less number of parameters into functions, if there are many parameters to be passed, pack them into structure, and pass the struct by reference. This reduces call stack space.
  • Don't nest the functions too much. Max 4-5 nesting level is just good (switch, while, if etc.). If it is getting more than that, try to refactor within function by return, changing conditions, flow-path etc.
  • Don't pass large objects by value, pass them by reference.
  • Make methods as static if they aren't using any data members - to avoid this being pushed and popped from call stack.
  • Don't over optimize any code - let the compiler do it for you. Optimized build will take time to optimize the code in best manner for target platform.
  • Do code instrumentation, and use/deploy the instrumented code. Code instrumentation will change the flow of code so that program runs faster. For example if(rarely_true) Dothis(); else DoThat(); will be reversed in code instrumentation if instrumentation finds that raraly_true is mostly false. This is simplest example.
Ajay
  • 18,086
  • 12
  • 59
  • 105
  • 1
    A function of 100 lines of code is large - not small – Ed Heal Jun 14 '16 at 10:43
  • What does it have to do with the question? I ask about compiler behavior. Nevertheless, all good rules! I stick to them in general. Can you recommend some places where I can read about code instrumentation? – Fabian Jun 14 '16 at 11:47