When is “inline” ineffective? (in C)

前端 未结 12 603
鱼传尺愫
鱼传尺愫 2020-12-09 10:56

Some people love using inline keyword in C, and put big functions in headers. When do you consider this to be ineffective? I consider it s

相关标签:
12条回答
  • 2020-12-09 11:36

    Inline functions should be approximately 10 lines or less, give or take, depending on your compiler of choice.

    You can tell your compiler that you want something inlined .. its up to the compiler to do so. There is no -force-inline option that I know of which the compiler can't ignore. That is why you should look at the assembler output and see if your compiler actually did inline the function, if not, why not? Many compilers just silently say 'screw you!' in that respect.

    so if:

    static inline unsigned int foo(const char *bar)

    .. does not improve things over static int foo() its time to revisit your optimizations (and likely loops) or argue with your compiler. Take special care to argue with your compiler first, not the people who develop it.. or your just in store for lots of unpleasant reading when you open your inbox the next day.

    Meanwhile, when making something (or attempting to make something) inline, does doing so actually justify the bloat? Do you really want that function expanded every time its called? Is the jump so costly?, your compiler is usually correct 9/10 times, check the intermediate output (or asm dumps).

    0 讨论(0)
  • 2020-12-09 11:38

    Inline can be used for small and frequently used functions such as getter or setter method. For big functions it is not advisable to use inline as it increases the exe size. Also for recursive functions, even if you make inline, the compiler will ignore it.

    0 讨论(0)
  • 2020-12-09 11:41

    That's right. Using inline for big functions increases compile time, and brings little extra performance to the application. Inline functions are used to tell the compiler that a function is to be included without a call, and such should be small code repeated many times. In other words: for big functions, the cost of making the call compared to the cost of the own function implementation is negligible.

    0 讨论(0)
  • 2020-12-09 11:41

    Personally I don't think you should ever inline, unless you have first run a profiler on your code and have proven that there is a significant bottleneck on that routine that can be partially alleviated by inlining.

    This is yet another case of the Premature Optimization Knuth warned about.

    0 讨论(0)
  • 2020-12-09 11:43

    Inline is effective in one case: when you've got a performance problem, ran your profiler with real data, and found the function call overhead for some small functions to be significant.

    Outside of that, I can't imagine why you'd use it.

    0 讨论(0)
  • 2020-12-09 11:53

    Another reason why you shouldn't use inline for large functions, is in the case of libraries. Every time you change the inline functions, you might loose ABI compatibility because the application compiled against an older header, has still inlined the old version of the function. If inline functions are used as a typesafe macro, chances are great that the function never needs to be changed in the life cycle of the library. But for big functions this is hard to guarantee.

    Of course, this argument only applies if the function is part of your public API.

    0 讨论(0)
提交回复
热议问题