java - JVM/JAVA 中的预取指令

标签 java jvm jit

有没有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/

相关文章:

java - GlassFish JSF : IndexOutOfBoundsException no personalized classes in trace

java - 难以理解计数和平均值

java - 为什么Java类文件 "typically (but not necessarily)"存储在一个文件中?

java - 在JVM指令中,有些指令的前缀不同,但功能却非常相似。我们真的需要它们吗?如果是,为什么?

c# - JVM 和 CLR 如何知道何时启动

java - 在单元测试中与 KafkaEmbedded 一起使用时,@DirtiesContext 的行为是什么?

java - 保存或检索以前的 Camel 交换体

java - 接口(interface)的默认方法存储在内存中的什么位置?

javascript - 为什么 JavaScript 会被编译成机器码?

使用 fortran 包装的右侧时,Julia 的 DifferentialEquations 包失败