-
Enhancement
-
Resolution: Fixed
-
P3
-
21, 22
-
b18
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?
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?