为什么堆栈帧中局部变量数组的每个插槽都是4个字节,而不是JVM中的1个字节? [英] Why each slot of the local variable array in a stack frame is of 4 bytes, and not 1 byte in the JVM?

查看:302
本文介绍了为什么堆栈帧中局部变量数组的每个插槽都是4个字节,而不是JVM中的1个字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

局部变量数组的每个插槽均为4字节.因此,为了存储字符,短变量或字节变量,使用了一个插槽.意味着所有较小的数据类型都会在内部转换为int数据类型.

Each slot of local variable array is of 4-bytes. So to store a character, short or byte variables one slot is used. Means all smaller data-types internally gets converted into int data type.

我的疑问是:

1).如果内部只有4个字节,较小的数据类型是否会变得无用?,如果是,为什么不从语言中删除此类数据类型? 2).如果每个插槽均为1字节,则不会浪费内存.为什么每个插槽都不是1字节?

1). Is it not making smaller data types useless, if internally they are of 4-bytes?, If yes, Why not remove such data-types from the language? 2). If each slot is of 1-byte then there will be no wastage of memory. Why not each slot is of 1-byte?

推荐答案

1).如果内部只有4个字节,较小的数据类型是否会变得无用?

1). Is it not making smaller data types useless, if internally they are of 4-bytes?

当涉及局部变量或算术时,是的,使用较小的数据类型可能毫无用处,甚至由于需要进行值集转换而浪费性能.

When it comes to local variables or arithmetic, then yes, using smaller data types can be useless and even waste performance due to required value set conversions.

但是,除了局部变量外,还有一个 heap ,对于字段和数组,它可以(实现特定)有所不同.

However, besides local variables, there’s a heap and for fields and arrays it can make (implementation specific) differences.

如果是,为什么不从语言中删除此类数据类型?

If yes, Why not remove such data-types from the language?

首先,如上所述,这里有堆.其次,有I/O.您正在与外界以字节或字符或当今的代码点交换数据.因此,除了存储大小外,这些数据类型还带有语义.不管您调用StringBuilder.append(char)还是StringBuilder.append(byte),这都产生了巨大的差异,尽管在两种情况下,实际上都会传递int值.

First, as said above, there’s the heap. Second, there’s I/O. You are exchanging data with the outside world in term s of bytes or char’s or, nowadays, codepoints. So besides storage sizes, these data types carry semantics. It makes a huge difference whether you invoke StringBuilder.append(char) or StringBuilder.append(byte), though in both cases, an int value is actually passed.

2).如果每个插槽均为1字节,则不会浪费内存.为什么每个插槽都不是1字节?

2). If each slot is of 1-byte then there will be no wastage of memory. Why not each slot is of 1-byte?

如果每个插槽都是一个字节,则不可能在其中存储任何其他内容.您正在使堆栈帧插槽(用于局部变量和操作数堆栈)与实际存储混淆.插槽旨在保存任意数据类型的值,并且可以以与实现相关的方式映射到实际存储器或CPU寄存器. longdouble占用两个插槽的事实是历史上的折衷,这源于Java是在上世纪90年代初设计的.

If each slot is one byte, it would be impossible to store anything else into it. You are confusing stack frame slots (used for local variables and the operand stack) with actual storage. A slot is intended to hold a value of an arbitrary data type and may be mapped to an actual storage or a CPU register in an implementation-dependent way. The fact that long and double consume two slots is a historical compromise, stemming from the fact the Java was designed in the early nineties of the last century.

您可以肯定,如果今天已经设计好了,就可以定义一个插槽来容纳所有数据类型,包括longdouble.这里的重点是架构的简化.如果将较小的数据类型用于局部变量或将操作数用于特定的基础体系结构有潜在的好处,则取决于JVM实现来检测此情况并生成适当的本机代码.如上所述,在大多数情况下,由于将局部变量和操作数堆栈条目映射到CPU寄存器,所以没有这种好处.

Had it designed today, you can bet, a slot would be defined to be able to hold all data types, including long and double. The key point here is simplification of the architecture. If there is a potential benefit from using smaller data types for a local variable or an operand for a particular underlying architecture, it’s up to the JVM implementation to detect this and generate the appropriate native code. As said, in most cases, due to the local variables and operand stack entries being mapped to CPU registers, there is no such benefit.

这篇关于为什么堆栈帧中局部变量数组的每个插槽都是4个字节,而不是JVM中的1个字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆