How are java interfaces implemented internally? (vtables?)

痴心易碎 提交于 2019-11-27 17:23:53
Martin v. Löwis

The key feature of the HotSpot JVM is inline caching. This doesn't actually mean that the target method is inlined, but means that an assumption is put into the JIT code that every future call to the virtual or interface method will target the very same implementation (i.e. that the call site is monomorphic). In this case, a check is compiled into the machine code whether the assumption actually holds (i.e. whether the type of the target object is the same as it was last time), and then transfer control directly to the target method - with no virtual tables involved at all. If the assertion fails, an attempt may be made to convert this to a megamorphic call site (i.e. with multiple possible types); if this also fails (or if it is the first call), a regular long-winded lookup is performed, using vtables (for virtual methods) and itables (for interfaces).

Edit: The Hotspot Wiki has more details on the vtable and itable stubs. In the polymorphic case, it still puts an inline cache version into the call site. However, the code actually is a stub that performs a lookup in a vtable, or an itable. There is one vtable stub for each vtable offset (0, 1, 2, ...). Interface calls add a linear search over an array of itables before looking into the itable (if found) at the given offset.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!