Contents | Prev | Next | IndexThe JavaTM Virtual Machine Specification


CHAPTER 9

An Optimization


This chapter describes an optimization implemented in Sun's version of the Java Virtual Machine. In this optimization, compiled Java Virtual Machine code is modified at run time for better performance.

The optimization takes the form of a set of pseudo-instructions. These are variants of normal Java Virtual Machine instructions that take advantage of information learned at run time to do less work than the original instructions. The pseudo-instructions are distinguishable by the suffix _quick in their mnemonics.

It is important to understand that these pseudo-instructions are not part of the Java Virtual Machine specification or instruction set. They are invisible outside of a Java Virtual Machine implementation. However, inside a Java Virtual Machine implementation they have proven to be an effective optimization.

The technique documented in this chapter is covered by U.S. Patent 5,367,685.


9.1 Dynamic Linking via Rewriting

A compiler targeting the Java Virtual Machine must only emit instructions from the instruction set documented in Chapter 6, "Java Virtual Machine Instruction Set." The optimization described in this chapter works by dynamically replacing occurrences of certain of those instructions, the first time they are executed, by internal, more efficient variants. The new instructions take advantage of loading and linking work done the first time the associated normal instruction is executed.

For instructions that are rewritten, each instance of the instruction is replaced on its first execution by a _quick pseudo-instruction. Subsequent execution of that instruction instance is always the _quick variant. Most instructions with _quick variants have just a single alternative version, although some have several.

In all cases, the instructions with _quick variants reference the constant pool, a fairly costly operation. The _quick pseudo-instructions save time by exploiting the fact that, while the first time an instruction referencing the constant pool must dynamically resolve the constant pool entry, subsequent invocations of that same instruction must reference the same object and need not resolve the entry again. The rewriting process is as follows:

  1. Resolve the specified item in the constant pool.
  2. Throw an exception if the item in the constant pool cannot be resolved.
  3. Overwrite the instruction with the _quick pseudo-instruction and any new operands it requires. The instructions putstatic, getstatic, putfield, and getfield each have two _quick versions, chosen depending on the type of the field being operated upon.
  4. Execute the new _quick pseudo-instruction.
This is the same as the definition of the original instruction, except for the additional step in which the instruction overwrites itself with its _quick variant. The operands of the _quick pseudo-instruction must fit within the space allocated for the original instruction's operands.

The _quick variant of an instruction can assume that the item in the constant pool has already been resolved and that this resolution did not generate any errors. It simply performs the intended operation on the resolved item. A significant amount of time is thus saved on all subsequent invocations of the pseudo-instruction.


9.2 The _quick Pseudo-instructions

The remainder of this chapter specifies the _quick pseudo-instructions used by Sun's Java Virtual Machine implementation. Although they are documented in the same format as the normal Java Virtual Machine instructions, the _quick pseudo- instructions are not part of the Java Virtual Machine specification and do not appear in class files. They are normally an invisible implementation detail, so that decisions such as opcode choices are left up to the implementor.

However, there are exceptions to this rule. Certain tools such as debuggers and just-in-time (JIT) code generators may need to know details about the _quick pseudo-instructions so that they can operate on code that has already been executed. An implementation of the Java Virtual Machine may use techniques similar to but different from Sun's _quick pseudo-instructions, or may use different opcode numbers from Sun's implementation. Tools assuming the details of Sun's _quick pseudo-instructions may not work with these differing implementations.

APIs are being developed for debuggers and JIT code generators. These APIs may provide ways of hiding details of internal pseudo-instructions so that tools that are independent of internal implementation details can be written. However, as of this writing these APIs have not yet been established, so in the meantime we document opcode values together with other details of Sun's _quick instructions. Tools can assume that implementations of the Java Virtual Machine that derive from Sun's, or that are written to be compatible with Sun's implementation, will follow the specification given below.

Contact [email protected] for more information about the status of debugger and JIT code generator APIs.



Contents | Prev | Next | Index

Java Virtual Machine Specification

Copyright © 1996, 1997 Sun Microsystems, Inc. All rights reserved
Please send any comments or corrections to [email protected]