What is wrong with using inline functions?

后端 未结 12 1285
滥情空心
滥情空心 2020-11-29 01:51

While it would be very convenient to use inline functions at some situations,

Are there any drawbacks with inline functions?

Conclusion:

12条回答
  •  无人及你
    2020-11-29 02:14

    Inlining larger functions can make the program larger, resulting in more instruction cache misses and making it slower.

    Deciding when a function is small enough that inlining will increase performance is quite tricky. Google's C++ Style Guide recommends only inlining functions of 10 lines or less.

    (Simplified) Example:

    Imagine a simple program that just calls function "X" 5 times.

    If X is small and all calls are inlined: Potentially all instructions will be prefetched into the instruction cache with a single main memory access - great!

    If X is large, let's say approaching the capacity of the instruction cache:
    Inlining X will potentially result in fetching instructions from memory once for each inline instance of X.
    If X isn't inlined, instructions may be fetched from memory on the first call to X, but could potentially remain in the cache for subsequent calls.

提交回复
热议问题