Does Java optimize method calls via an interface which has a single implementor marked as final?

后端 未结 4 1115
名媛妹妹
名媛妹妹 2021-02-08 15:03

If I have a reference to a class and invoke a method on it, and the class or the method is final, my understanding is that the compiler or the JVM would replace the dynamic disp

4条回答
  •  野性不改
    2021-02-08 15:59

    (Insert Knuth quote here about optimization.)

    See Wikis Home > HotSpot Internals for OpenJDK > PerformanceTechniques.

    • Methods are often inlined. This increases the compiler's "horizon" of optimization.
    • Static, private, final, and/or "special" invocations are easy to inline.
    • Virtual (and interface) invocations are often demoted to "special" invocations, if the class hierarchy permits it. A dependency is registered in case further class loading spoils things.
    • Virtual (and interface) invocations with a lopsided type profile are compiled with an optimistic check in favor of the historically common type (or two types).

    There are some interesting links from Inlining.

提交回复
热议问题