简体   繁体   English

方法字节码限制的目的是什么?

[英]What's the purpose of the method bytecode limit?

Following up on this question : there is a 64kb bytecode limit on Java methods. 关注这个问题 :Java方法有64kb字节码限制。

What benefit does this provide to the compiler? 这为编译器带来了什么好处? GCC will happily compile multi-megabyte C methods -- what's the reasoning behind this design decision? GCC很乐意编译多兆字节的C方法 - 这个设计决策背后的原因是什么? Is it just, as suggested in this question , to limit poor programming practices, since 64kb methods are massive? 正如这个问题所建议的那样,只是限制糟糕的编程实践,因为64kb方法是巨大的吗? Or is there some specific benefit to the limit being 64kb rather than some other number? 或者是否有一些特定的好处,限制是64kb而不是其他一些数字?

Offsets in the method bytecode are 2 bytes long (called "u2" in the class file format specification). 方法字节码中的偏移量为2个字节长(在类文件格式规范中称为“u2”)。 The maximum offset that can be expressed with u2 is 64kB. 可用u2表示的最大偏移量为64kB。

The offsets appear in the actual instructions, such as the if* bytecodes, that are followed by two bytes containing the offset delta of the branch. 偏移量出现在实际指令中,例如if*字节码,后跟两个包含分支偏移量增量的字节。 Also, other class file attributes like the StackMapTable , LocalVariableTable , Exceptions and others contain offsets into the bytecode. 此外,其他类文件属性(如StackMapTableLocalVariableTableExceptions等)包含字节码的偏移量。

If offsets were u4, then methods could be longer, but all class files would also be larger. 如果偏移是u4,那么方法可能会更长,但所有类文件也会更大。 It's a trade-off. 这是一种权衡。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM