2 ms·
No, Java does Class hierarchy analysis and has multiple way not to use v-table calls. Single site (no class found overriding a method) are static and can be in
by xxs 1y ago
No, Java does Class hierarchy analysis and has multiple way not to use v-table calls.
Single site (no class found overriding a method) are static and can be inlined directly. Dual call sites use a class check (which is a simple equality), can be inlined, no v-table. 3-5 call sites use inline caches (e.g. the compiler records what class have been used) that are similar and some can be inlined, usually plus a guard check.
Only high polymorphic calls use v-table and in practice is a very rare occasion, even with Java totally embracing inheritance (or polymorphic interfaces)
Note: CHA is dynamic and happens at runtime, depending which classes have been loaded. Loading new classes causes CHA to be performed again and if there are affected sites, the latter are to be deoptimized (and re-JIT again)
- nine_k 1y agoThanks, TIL!
- josephg 1y agoThats very cool. Does C++ have any of these optimisations?
- gpderetta 1y agoSome compilers do some of these optimizations, at least those that not require a JIT and code patching at runtime. But these optimizations are vastly less critical in C++ where only are minority of functions are virtual.