Inline Small Functions for Best Perf
Rule
Mark small functions as inline so the compiler can eliminate call overhead and optimize across the call boundary.
- •Prefer
inlinefor: one-liners, trivial getters/setters, small predicates, hot-path helpers. - •In headers: use
inlineon function definitions that are defined in the header (so multiple TUs can include them without ODR issues). - •In .cpp: use
inlineon small functions that are only used in that TU, to encourage inlining.
When to Use
- •Header-defined functions: Any function whose definition lives in a header should be
inline(or in an anonymous namespace in the header) to avoid multiple-definition errors and to allow inlining. - •Small accessors: Getters/setters that return a member or do a simple assignment are good candidates.
- •Hot path: Small functions called in loops or critical paths benefit from inlining.
- •Tiny helpers: Functions that are a few statements and called from few call sites.
Examples
cpp
// Header: inline so definition can appear in header and still inline at call sites
inline int Foo::getValue() const { return value_; }
inline void Foo::setValue(int v) { value_ = v; }
// Small predicate
inline bool isReady() const { return state_ == Ready; }
Caveats
- •Do not force
inlineon large or rarely-called functions; it can increase code size and hurt I-cache. - •The compiler may ignore
inlinefor optimization; use it as a hint. For forced inlining, use compiler-specific attributes (e.g.__attribute__((always_inline))) only when profiling shows a need. - •In C++, member functions defined inside the class body are implicitly inline; no need to repeat
inlinethere.