Hacker News new | ask | show | jobs
by Coding_Cat 3252 days ago
The call itself is not very expensive, but the effect of having to jump through vtables can prevent other optimizations in a way similar to calling a function in a DLL.

Not a huge issue for business logic bits or if your virtual functions are quite large, but using virtual functions for small often used calls such as specific getters or some mathematical function can cause a big performance hit.

1 comments

Exactly. The cost is not the overhead from the actual call - it's the fact that it inhibits other optimizations. The same applies to normal function calls when link time optimization is disabled.

This only becomes relevant in hot loops, but inlining can give big wins when combined with other optimizations.

I've seen some speculative inlining happening even without LTO (so obviously, in the same TU) with GCC.
Absolutely. Seems like modern compilers inline everything they possibly can, regardless whether you mark your functions inline or not.