What's the purpose of the method bytecode limit?

bytecode, java, jvm

Solution

Offsets in the method bytecode are 2 bytes long (called "u2" in the class file format specification). The maximum offset that can be expressed with u2 is 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. Also, other class file attributes like the `StackMapTable`, `LocalVariableTable`, `Exceptions` and others contain offsets into the bytecode.

If offsets were u4, then methods could be longer, but all class files would also be larger. It's a trade-off.

Problem

Following up on this question: there is a 64kb bytecode limit on Java methods. What benefit does this provide to the compiler? GCC will happily compile multi-megabyte C methods -- what's the reasoning behind this design decision? Is it just, as suggested in this question, to limit poor programming practices, since 64kb methods are massive? Or is there some specific benefit to the limit being 64kb rather than some other number?

Original source

Related problems