Java在LittleEndianCPU架构上运行时如何处理Endianess?

Java 是 Big Endian 它如何在保持性能的同时处理 Little Endian CPU?JVM(OpenJDK、OpenJ9 等)是否进行了任何特殊优化以保持性能,例如在 Little Endian 平台的特殊情况下仅选择性地成为 Big Endian?访问 ByteBuffers 或调用本机代码或写入 IO 或访问 volatile 变量时是否有特殊的字节序处理?Java 如何改变 Little Endian 架构中的字节序?在什么点或操作(加载、存储、计算、寄存器、缓存、内存等)改变了字节序?这会带来什么样的性能损失?

回答

Java 是 Big Endian 它如何在保持性能的同时处理 Little Endian CPU?

Java 不是大端。在 Java 运行时库中Endianness甚至是一个问题的少数几个地方,API 使用 Big Endian,但它总是有很好的文档记录,并且一些 API 允许您指定所需的 Endianness。

JVM(OpenJDK、OpenJ9 等)是否进行了任何特殊优化以保持性能,例如在 Little Endian 平台的特殊情况下仅选择性地成为 Big Endian?

不,JVM 使用本机字节顺序。

访问 ByteBuffers 或调用本机代码或写入 IO 或访问 volatile 变量时是否有特殊的字节序处理?

是的,不,是的,没有。

由于 JVM 使用本机字节顺序,因此无需处理调用本机代码或访问 volatile 变量。字节顺序仅在(反)序列化到/从字节时才重要,例如在访问 ByteBuffers 或写入 IO 时。

Java 如何改变 Little Endian 架构中的字节序?

您可以在任何地方更改 Endianness 的方式相同,它会交换字节,或以适当的顺序读取/写入字节。

在什么点或操作(加载、存储、计算、寄存器、缓存、内存等)改变了字节序?

不是,因为 JVM 使用本机字节序。Endianness 仅在本机值转换为字节或从字节转换时应用。在任何其他时间点,字节序都无关紧要。

这会带来什么样的性能损失?

没有,因为它什么都不做。

  • The *class-file* is always Big-Endian (it had to choose 1 option, because it has to work unaltered on all platforms); but the *JVM* uses the *platform endiannes*, and only needs to perform conversions when reading bytes from sources (such as class-files) that may or may not have different endiannes
  • @SumindaSirinathS.Dharmasena There's a big difference between the ***bytes*** stored in a bytecode file (`.class`), vs. how data is stored in *memory* in a running JVM. All your comments, as well as the accepted answer in the first link you commented, are all about the bytecode file. Your question is about the JVM (the Java Virtual *Machine*), and that is what this answer addresses.
  • @SumindaSirinathS.Dharmasena Correct, except: 2) "Byte-Code/Opcodes in memory" is incomplete, since they may be converted to native CPU instructions by JIT, for better performance. Even without JIT, the JVM might store the Byte-Code/Opcodes in a different way from how it's stored in the bytecode file. It's all internal and the JVM can do whatever it wants.

以上是Java在LittleEndianCPU架构上运行时如何处理Endianess?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>