Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8311594

Avoid GlobalSession liveness check

XMLWordPrintable

      Consider the following benchmark:

          static final MemorySegment ALL = MemorySegment.NULL.reinterpret(Long.MAX_VALUE);
          long unsafe_addr = unsafe.allocateMemory(4);

          @Benchmark
          public int segment_ALL() {
              return ALL.get(JAVA_INT, unsafe_addr);
          }

      Even though the ALL segment is constant, and backed by the global scope, the hot path will still contain a liveness check [1].

      This is likely the result of `state` in MemorySessionImpl being mutable, even though it is never changed for the GLOBAL session.

      We can avoid this liveness check by overriding checkValidStateRaw in GlobalSession to do nothing.

      [1]:
                 0x00000245b9eeabf9: movabsq $0x43f9d7728, %r11 ; {oop(a 'jdk/internal/foreign/GlobalSession'{0x000000043f9d7728})}
                 0x00000245b9eeac03: movl 0xc(%r11), %ebp ;*getfield state {reexecute=0 rethrow=0 return_oop=0}
                                                                           ; - jdk.internal.foreign.MemorySessionImpl::checkValidStateRaw@22 (line 205)
                                                                           ; - jdk.internal.misc.ScopedMemoryAccess::getIntUnalignedInternal@5 (line 1907)
                                                                           ; - jdk.internal.misc.ScopedMemoryAccess::getIntUnaligned@6 (line 1897)
                                                                           ; - java.lang.invoke.VarHandleSegmentAsInts::get@48 (line 109)
                                                                           ; - java.lang.invoke.VarHandleGuards::guard_LJ_I@49 (line 999)
                                                                           ; - java.lang.foreign.MemorySegment::get@9 (line 1517)
                                                                           ; - org.openjdk.bench.java.lang.foreign.MemorySegmentGet::segment_ALL@10 (line 117)
        3.42% 0x00000245b9eeac07: testl %ebp, %ebp
                 0x00000245b9eeac09: jl 0x245b9eeac64 ;*ifge {reexecute=0 rethrow=0 return_oop=0}
                                                                           ; - jdk.internal.foreign.MemorySessionImpl::checkValidStateRaw@25 (line 205)
                                                                           ; - jdk.internal.misc.ScopedMemoryAccess::getIntUnalignedInternal@5 (line 1907)
                                                                           ; - jdk.internal.misc.ScopedMemoryAccess::getIntUnaligned@6 (line 1897)
                                                                           ; - java.lang.invoke.VarHandleSegmentAsInts::get@48 (line 109)
                                                                           ; - java.lang.invoke.VarHandleGuards::guard_LJ_I@49 (line 999)
                                                                           ; - java.lang.foreign.MemorySegment::get@9 (line 1517)
                                                                           ; - org.openjdk.bench.java.lang.foreign.MemorySegmentGet::segment_ALL@10 (line 117)

            jvernee Jorn Vernee
            jvernee Jorn Vernee
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: