Why java21 is faster?

发布时间 2023-12-04 01:37:46作者: rxh1999

After upgraed from jdk8 to jdk21, we gain ~16% performance improvements, here are some possible valid enhancement.

Reduce JNI Access: ~2x faster

In RandomAccessFile, writeLong requires 8 JNI calls

    public final void writeLong(long v) throws IOException {
        write((int)(v >>> 56) & 0xFF);
        write((int)(v >>> 48) & 0xFF);
        write((int)(v >>> 40) & 0xFF);
        write((int)(v >>> 32) & 0xFF);
        write((int)(v >>> 24) & 0xFF);
        write((int)(v >>> 16) & 0xFF);
        write((int)(v >>>  8) & 0xFF);
        write((int)(v >>>  0) & 0xFF);
    }

by converting long into byte array, we can only invoke JNI once

    public final void writeLong(long v) throws IOException {
        Bits.putLong(buffer, 0, v);
        write(buffer, 0, Long.BYTES);
    }

Constant Folding: ~12%

@Stable tells compiler this field will be updated at most once, Record compiler jvm its fields is trusted.
Both make compiler do constant folding

SIMD: ~2X

String::hashCode using ArraysSupport.verctorizedHashCode

Reference

https://bugs.openjdk.org/browse/JDK-8298639

https://bugs.openjdk.org/browse/JDK-8282664?focusedId=14480694&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel