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

Unsafe::allocateMemory and Unsafe::freeMemory are slower than malloc/free

    XMLWordPrintable

Details

    • b18

    Description

      When working with the FFM API we noticed that using malloc/free directly seems to be always faster than using Unsafe::allocateMemory and Unsafe::freeMemory.

      See the performance chart in this PR:

      https://github.com/openjdk/panama-foreign/pull/855#issuecomment-1660112775

      When investigating this gap, we realized that the issue is caused by the double thread state transition (from Java -> Native, then from Native -> VM) which occurs at each Unsafe call from Java code.

      Using UNSAFE_LEAF instead of UNSAFE_ENTRY largely removes this issue.

      Unsafe (UNSAFE_ENTRY): 60.815 ns/op
      Unsafe (UNSAFE_LEAF): 41.771 ns/op
      Malloc: 35.379 ns/op

      Can UNSAFE_LEAF be used for Unsafe::allocateMemory/freeMemory? Or does NativeMemoryTracking (which is used by Unsafe) needs VM mode?

      Attachments

        Issue Links

          Activity

            People

              dholmes David Holmes
              mcimadamore Maurizio Cimadamore
              Votes:
              0 Vote for this issue
              Watchers:
              9 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: