RFR: 8351140: RISC-V: Intrinsify Unsafe::setMemory

Anjian Wen duke at openjdk.org
Tue Mar 11 19:20:51 UTC 2025


On Tue, 4 Mar 2025 09:46:53 GMT, Anjian Wen <duke at openjdk.org> wrote:

> From [JDK-8329331](https://bugs.openjdk.org/browse/JDK-8329331), add riscv unsafe::setMemory intrinsic’s generator generate_unsafe_setmemory. This intrinsic optimizes about 15%-20% unsafe setmemory time

// Add benchmark Test

import sun.misc.Unsafe;
import java.lang.reflect.Field;

public class UnsafeMemoryTest {
  private static Unsafe getUnsafe() throws Exception {
    Field f = Unsafe.class.getDeclaredField("theUnsafe");
    f.setAccessible(true);
    return (Unsafe) f.get(null);
  }

  public static void main(String[] args) {
    try {
      Unsafe unsafe = getUnsafe();
      long size = 9999L;
      long address = unsafe.allocateMemory(size);
      byte initialValue = 0x4E;
      long totalElapsedTime = 0;
      long start = System.nanoTime();
      for (int i = 0; i < 100000000; i++)
              unsafe.setMemory(address, size, initialValue);
      long end = System.nanoTime();
      totalElapsedTime = end - start;
      System.out.println("elapsed time: " + totalElapsedTime + " ns");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}


This test seems can reduce about 15% time consumption

I have run the test above on my riscv musebook and here is my result

Origin
91142737523 ns
81256485532 ns
81935426870 ns
77492514654 ns
81042094789 ns
85066289810 ns

After optimize
73503057373 ns
75953174376 ns
75200200198 ns
73717004138 ns
72682417915 ns
73935123648 ns

The result shows that
1)origin data seems to fluctuate greatly and optimized version seems more stable
2)The average optimization of the optimized data is 11%, the maximum optimization is 20%, and the minimum optimization is 3%.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/23890#issuecomment-2700047363
PR Comment: https://git.openjdk.org/jdk/pull/23890#issuecomment-2703215881


More information about the hotspot-compiler-dev mailing list