Why defining class as final improves JVM performance?

后端 未结 5 1503
情书的邮戳
情书的邮戳 2020-11-29 01:41

Quoting from http://sites.google.com/site/gson/gson-design-document:

Why are most classes in Gson marked as final?

While Gson provides a

相关标签:
5条回答
  • 2020-11-29 02:01

    An old, apparently no longer but still largely relevant, article on this from IBM developerWorks, which states:

    The common perception is that declaring classes or methods final makes it easier for the compiler to inline method calls, but this perception is incorrect (or at the very least, greatly overstated).

    final classes and methods can be a significant inconvenience when programming -- they limit your options for reusing existing code and extending the functionality of existing classes. While sometimes a class is made final for a good reason, such as to enforce immutability, the benefits of using final should outweigh the inconvenience. Performance enhancement is almost always a bad reason to compromise good object-oriented design principles, and when the performance enhancement is small or nonexistent, this is a bad trade-off indeed.

    Also see this related answer on another question. There's also the equivalent question for .Net, discussed here. SO discussion, "Are final methods inlined?" On a question titled "What optimizations are going to be useless tomorrow," this one appears on the list.

    Here is someone trying to characterize the effect on HotSpot inlining from static and final classes.

    Note also that there is an entangling of the effects of final classes vs. final methods. You may get some performance benefit (again, I don't have a good reference) for final methods for sure, as it could cue the JIT to do inlining it couldn't otherwise do (or not so simply). You get the same effect when you mark the class final, which means that all the methods are suddenly final as well. Note that the Sun/Oracle folks claim that HotSpot can usually do this with or without the final keyword. Are there any additional effects from having the class itself final?

    For reference, links to the JLS on final methods and final classes.

    0 讨论(0)
  • 2020-11-29 02:03

    Not knowing the implementation of every particular JVM, I would theoretically say that if a JVM knows that a pointer to an object is a pointer to a type that is final, it can do non-virtual function calls (i.e., direct vs. indirect) to a member functions (i.e., no indirection through a function pointer), which may result in faster execution. This may also in turn lead to inlinining possibilities.

    0 讨论(0)
  • 2020-11-29 02:04

    No difference, that's just speculation. The only situation where it has sense are classes like String, etc where jvm treat them differently.

    0 讨论(0)
  • 2020-11-29 02:09

    Virtual (overridden) methods generally are implemented via some sort of table (vtable) that is ultimately a function pointer. Each method call has the overhead of having to go through that pointer. When classes are marked final then all of the methods cannot be overridden and the use of a table is not needed anymore - this it is faster.

    Some VMs (like HotSpot) may do things more intelligently and know when methods are/are not overridden and generate faster code as appropriate.

    Here is some more specific info on HotSpot. And some general info too.

    0 讨论(0)
  • 2020-11-29 02:12

    Marking classes as final allows further optimizations to be applied during the JIT stage.

    If you are calling a virtual method on a non-final class, you don't know whether the proper implementation is the one defined in that class, or some sub-class that you don't know about.

    However, if you have a reference to a final class, you know the specific implementation that is required.

    Consider:

    A extends B
    B extends C
    
    B myInstance = null;
    if(someCondition)
       myInstance = new B();
    else
       myInstance = new C();
    myInstance.toString();
    

    In this case, the JIT can't know whether C's implementation of toString() or B's implementation of toString() will be called. However, if B is marked as final, it is impossible for any implementation other than B's to be the proper implementation

    0 讨论(0)
提交回复
热议问题