Why is hashCode slower than a similar method?

后端 未结 5 549
星月不相逢
星月不相逢 2020-12-13 04:09

Normally, Java optimizes the virtual calls based on the number of implementations encountered on a given call side. This can be easily seen in the results of my benchmark, w

相关标签:
5条回答
  • 2020-12-13 04:35

    I was looking at your invariants for your test. It has scenario.vmSpec.options.hashCode set to 0. According to this slideshow (slide 37) that means Object.hashCode will use a random number generator. That might be why the JIT compiler is less interested in optimising calls to hashCode as it considers it likely it may have to resort to an expensive method call, which would offset any performance gains from avoiding a vtable lookup.

    This may also be why setting Base to have its own hash code method improves performance as it prevents the possibility of falling through to Object.hashCode.

    http://www.slideshare.net/DmitriyDumanskiy/jvm-performance-options-how-it-works

    0 讨论(0)
  • 2020-12-13 04:35

    The semantics of hashCode() are more complex than regular methods, so the JVM and the JIT compiler must do more work when you call hashCode() than when you call a regular virtual method.

    One specificity has an negative impact on performance : calling hashCode() on a null object is valid and returns zero. This requires one more branching than on a regular call which in itself can explain the performance difference you have constated.

    Note that is is true it seems only from Java 7 due to the introduction of Object.hashCode(target) which has this semantic. It would be interesting to know on which version you tested this issue and if you would have the same on Java6 for instance.

    Another specificity has a positive impact on performance : if you do not provide your own hasCode() implementation, the JIT compiler will use an inline hashcode computation code which is faster than a regular compiled Object.hashCode call.

    E.

    0 讨论(0)
  • 2020-12-13 04:38

    This is a known performance issue: https://bugs.openjdk.java.net/browse/JDK-8014447
    It has been fixed in JDK 8.

    0 讨论(0)
  • 2020-12-13 04:43

    hashCode is defined in java.lang.Object, so defining it in your own class doesn't do much at all. (still it's a defined method but it makes no difference)

    JIT has several ways to optimize call sites (in this case hashCode()):

    • no overrides - static call (no virtual at all) - best case scenario with full optimizations
    • 2 sites - ByteBuffer for instance: exact type check and then static dispatch. The type check is very simple but depending on the usage it may or may not be predicted by the hardware.
    • inline caches - when few different class instances have been used in the caller body, it's possible to keep them inlined too - that's it some methods might be inlined, some may be called via virtual tables. Inline budget is not very high. This is exactly the case in the question - a different method not named hashCode() would feature the inline caches as there are only four implementations, instead of the v-table
    • Adding more classes going through that caller body results in real virtual call as the compiler gives up.

    The virtual calls are not inlined and require an indirection through the table of virtual methods and virtually ensured cache miss. The lack of inlining actually requires full function stubs with parameters passed through the stack. Overall when the real performance killer is the inability to inline and apply optimizations.

    Please note: calling hashCode() of any class extending Base is the same as calling Object.hashCode() and this is how it compiles in the bytecode, if you add an explicit hashCode in Base that would limit the potential call targets invoking Base.hashCode().

    Way too many classes (in JDK itself) have hashCode() overridden so in cases on not inlined HashMap alike structures the invocation is performed via vtable - i.e. slow.

    As extra bonus: While loading new classes the JIT has to deoptimize existing call sites.


    I may try to look up some sources, if anyone is interested in further reading

    0 讨论(0)
  • 2020-12-13 04:51

    I can confirm the findings. See these results (recompilations omitted):

    $ /extra/JDK8u5/jdk1.8.0_05/bin/java Main
    overCode :    14.135000000s
    hashCode :    14.097000000s
    
    $ /extra/JDK7u21/jdk1.7.0_21/bin/java Main
    overCode :    14.282000000s
    hashCode :    54.210000000s
    
    $ /extra/JDK6u23/jdk1.6.0_23/bin/java Main
    overCode :    14.415000000s
    hashCode :   104.746000000s
    

    The results are obtained by calling methods of class SubA extends Base repeatedly. Method overCode() is identical to hashCode(), both of which just return an int field.

    Now, the interesting part: If the following method is added to class Base

    @Override
    public int hashCode(){
        return super.hashCode();
    }
    

    execution times for hashCode aren't different from those for overCode any more.

    Base.java:

    public class Base {
    private int code;
    public Base( int x ){
        code = x;
    }
    public int overCode(){
    return code;
    }
    }
    

    SubA.java:

    public class SubA extends Base {
    private int code;
    public SubA( int x ){
    super( 2*x );
        code = x;
    }
    
    @Override
    public int overCode(){
    return code;
    }
    
    @Override
    public int hashCode(){
        return super.hashCode();
    }
    }
    
    0 讨论(0)
提交回复
热议问题