跪拜 Guibai
← All articles
Backend · Java · JVM

An empty for loop's bytecode: 8 instructions, 56 steps, and why javac won't delete it

By 金銀銅鐵 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Most Java developers never read `javap` output, yet every loop they write is being laid out by `javac` with real trade-offs: the spec's layout is not what modern compilers emit, and an empty loop survives compilation because `javac` does no loop elimination, leaving that to the JIT. Knowing how the frame's two sizes, slot allocation, and branch forms are actually produced makes bytecode-level debugging and performance reasoning far less guesswork.

Summary

The JVM specification's classic `spin` example, an empty `for (i = 0; i < 10; i++)` loop, compiles to just 8 bytecode instructions: `iconst_0`, `istore_1`, `iload_1`, `bipush 10`, `if_icmpge`, `iinc`, `goto`, and `return`. The whole method executes in exactly 56 steps, with `locals=2` (slot 0 is `this`, slot 1 is `i`) and `stack=2` (the peak depth after `bipush 10` pushes the bound alongside `i`).

`javac 21` does not match the spec's illustrative listing. The spec places the condition test at the loop's bottom, using `goto 8` on entry and a self-looping `if_icmplt`, giving 4 instructions per iteration. `javac 21` tests first, using `if_icmpge 14` to exit and a separate `goto 2` to jump back, giving 5 per iteration. Both produce identical behavior — the spec trades one extra instruction on loop entry for one fewer per iteration, a trade-off Section 3.5 explains.

The article also walks through type variants: a `double` loop variable occupies two slots, has no `dinc` so `i++` needs four instructions, and has no `if_dcmplt` so comparisons go through `dcmpg` plus `iflt`; a `short` variable must be promoted to `int`, incremented, then truncated with `i2s`. A static version of `spin` drops `locals` to 1, moves `i` to slot 0, and sets `args_size` to 0. A Swing-based step debugger accompanying the post lets readers watch each instruction's effect on the frame.

Takeaways
— The empty loop `for (i = 0; i < 10; i++)` compiles to 8 instructions: iconst_0, istore_1, iload_1, bipush 10, if_icmpge, iinc, goto, return.
— `locals=2` means slot 0 is `this` and slot 1 is `i`; `stack=2` is the peak operand stack depth, reached after `bipush 10` when both `i` and the bound are on the stack.
— The JVMS spec's example places the condition test at the loop's bottom (4 instructions per iteration, with a `goto 8` on entry); `javac 21` tests first (5 per iteration, with `if_icmpge 14` to exit and `goto 2` to loop back). Both are behaviorally equivalent.
— Constants -1 through 5 are pushed with `iconst_<i>`, value implicit in the opcode; 10 needs `bipush` with a one-byte immediate, so the constant occupies two bytes instead of one.
— `iinc 1 1` increments local variable 1 (the `i` in slot 1) directly, never touching the operand stack; its one-byte signed increment range is -128 to 127.
— The `double` variant (`dspin`) has no `dinc` instruction, so `i++` becomes dload_1, dconst_1, dadd, dstore_1; it also has no `if_dcmplt`, so comparison uses `dcmpg` then `iflt`.
— The `short` variant (`sspin`) has no dedicated load/store/add instructions; it promotes `i` to int, does `iadd`, then truncates with `i2s`, replacing one `iinc` with five instructions.
— Making `spin` static changes `locals` to 1, moves `i` to slot 0, sets `args_size` to 0, and rewrites the instructions to `istore_0`, `iload_0`, `iinc 0 1`.
— The loop body executes 10 times and the condition test 11 times; total execution is 2 + 10×5 + 4 = 56 steps.
— `javac` does no loop-level elimination, so an empty loop is compiled faithfully and survives; only the runtime JIT may recognize and remove it.
Conclusions

The gap between the JVMS spec's illustrative code and `javac 21`'s output is a reminder that the spec describes semantics, not a compiler mandate; readers following the spec literally will mispredict real bytecode.

The `javac 21` layout (test-first, 5 instructions per iteration) is the opposite trade-off from the spec's (test-last, 4 per iteration): the spec optimizes for many iterations, while `javac` optimizes for simpler code generation at the cost of one extra instruction per round.

The `double` and `short` variants show that the JVM's instruction set is not type-uniform: missing instructions like `dinc` and `if_dcmplt` force longer instruction sequences, and `short` only exists through int truncation, making narrower types costlier than `int` itself.

An empty loop is the clearest proof that `javac` performs no semantic analysis of control flow; it translates source structure faithfully, delegating all optimization to the JIT, so bytecode payload size reflects source structure, not runtime cost.

Concepts & terms
Operand stack
A per-frame LIFO stack in the JVM where most bytecode instructions pop their operands and push results; its maximum depth is recorded in the `stack` attribute of a method's Code section.
iinc
A JVM bytecode instruction that increments a local variable directly, bypassing the operand stack entirely; its increment operand is a one-byte signed value in the range -128 to 127.
bipush
An instruction that pushes a byte-sized immediate constant onto the operand stack; used for int constants outside the range -1 through 5, which are instead handled by the dedicated iconst_<i> instructions.
dcmpg
An instruction that compares two doubles and pushes -1, 0, or 1 onto the operand stack; the JVM lacks an if_dcmplt-style conditional jump for doubles, so a separate iflt or similar test follows.
i2s
An instruction that truncates an int to a short; the JVM has no dedicated short load, store, or arithmetic instructions, so short values are computed as int and then narrowed with i2s.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗