有没有Java语言或者JVM的软件预取指令,比如__builtin_prefetch在 GCC 中可用
最佳答案
一件有趣的事是 Hotspot JVM 实际上确实支持预取!
它将 Unsafe.prefetchRead()
和 Unsafe.prefetchWrite()
方法视为内部函数,并将它们编译成相应的 CPU 指令。
不幸的是,sun.misc.Unsafe
没有声明这样的方法。但是,如果您将以下方法添加到 Unsafe.java,重新编译它并替换 rt.jar 中的 Unsafe.class(或仅添加 -Xbootclasspath/p
JVM 参数),您将能够使用 prefetch intrinsics在您的应用程序中。
public native void prefetchRead(Object o, long offset);
public native void prefetchWrite(Object o, long offset);
public static native void prefetchReadStatic(Object o, long offset);
public static native void prefetchWriteStatic(Object o, long offset);
我怀疑这对实际应用有多大帮助,但如果您想尝试一下,我可以提供更多详细信息。
这是一个已编译的 JDK 8 补丁,支持预取方法:download
使用示例:
long[] array = new long[100*1024*1024];
// ...
sun.misc.Unsafe.prefetchReadStatic(array, 50*1024*1024);
更新
Unsafe.prefetch*
内在函数完全是 removed在 JDK 9 中:
Note read/write prefetch support was implemented as an experiment to see if JDK library code could use it for performance advantages. However, the results of the experiment did not indicate this was worthwhile. As a consequence there are no corresponding prefetch native method declarations in sun.misc.Unsafe.
关于java - JVM/JAVA 中的预取指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22689712/