When to Inline Functions and When Not to
Inline functions are specifically designed to decrease overhead associated with function calls by substituting the function body at the call location. However, there are situations where inlining should be avoided.
When to Inline:
- Small functions: Inlining small functions results in faster code and reduces executable size.
- Frequently called functions: Inlining frequently called functions reduces overhead due to multiple calls.
When to Avoid Inlining:
- Large functions: Inline functions can bloat executables, diminishing performance.
- I/O bound functions: I/O operations are not affected by inlining, so it's unnecessary.
- Rarely used functions: Inlining rarely used functions is not beneficial.
- Constructors and destructors: Even empty constructors and destructors generate code.
- Library compatibility: Inlining existing library functions or modifying inline functions may break compatibility.
Additional Considerations:
- For extensibility, consider using non-inline virtual destructors and constructors.
- Profile your application to identify bottlenecks before inlining functions.
- The "inline" keyword is a hint to the compiler that may or may not be followed.
References:
- To Inline or Not To Inline
- [9] Inline functions
- Policies/Binary Compatibility Issues With C
- GotW #33: Inline
- Inline Redux
- Effective C - Item 33: Use inlining judiciously
The above is the detailed content of To Inline or Not to Inline: When Should You Optimize with Inline Functions?. For more information, please follow other related articles on the PHP Chinese website!