英文:
Why does the format of JVM tableswitch/lookupswitch instruction has between 0 and 3 bytes padding?
问题
我已经理解了tableswitch/lookupswitch的格式。
但是,在0和3个字节之间的填充有什么目的呢?
英文:
I have understanded the format of tableswitch/lookupswitch.
But,what is the purpose of between 0 and 3 bytes padding?
答案1
得分: 4
为了历史原因而存在填充。最早的Java虚拟机没有JIT编译,它们逐条解释字节码指令。为了允许解释器直接从字节码流中读取32位偏移量,这些偏移量是32位对齐的,它们的地址是4字节的精确倍数。
RISC处理器(例如SPARC、ARMv5等)仅允许对齐的内存访问。例如,要用单个CPU指令从内存中读取32位值,地址必须是32位对齐的。如果地址不对齐,则获取32位值需要进行四次8位内存读取,这显然会更慢。
现今,这种优化已不再有用。
英文:
The padding exists for historical reasons. The first Java Virtual Machines did not have JIT compilation, they interpreted bytecode instructions one by one. To allow interpreters read 32-bit offsets directly from the bytecode stream, the offsets are 32-bit aligned - their addresses are exact multiple of 4 bytes.
RISC processors (like SPARC, ARMv5 and earlier, etc.) permitted only aligned memory access. E.g. to read a 32-bit value from memory with a single CPU instruction, the address must be 32-bit aligned. If the address is not aligned, getting a 32-bit value requires four 8-bit memory reads, which is, of course, slower.
Nowadays the optimization is not useful anymore.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论