3 ms·
As others have mentioned in the thread, the issue is that virtual calls are more expensive because you have to resolve the correct function in the related vtabl
by tmccrary55 6y ago
As others have mentioned in the thread, the issue is that virtual calls are more expensive because you have to resolve the correct function in the related vtable.
So instead of just setting up and executing the call, you have to traipse through memory a bunch of times, affecting caches and making things slower.
But the thing is, Java's compiler works at runtime, so it can either inline the function (basically dump the code into the call site instead of invoking) or devirtualize it (if the same object is called over and over, the JIT can just remember the last lookup and add a guard if the target changes).
In say C++, making everything virtual would be crazy because the compiler can only safely inline in certain situations and can rarely devirtualize a call.
- ncmncm 6y agoPerformance is irrelevant to the principle involved.
- tmccrary55 6y ago!?!