17

I'm currently writing a toy compiler targeting Java bytecode in the translation.

I would like to know if there is some kind of catalog, maybe a summary, of various simple peephole optimizations that can be made in the emitted bytecode before writing the .class file. I actually am aware of some libraries that have this functionality, but I'd like to implement that myself.

2
  • 2
    Most of the optimization are not done in the Java compiler but in the runtime. Is your target performance results or performance result without runtime optimizations? Commented Nov 5, 2009 at 11:57
  • The real target would be compiler learning. I guess that makes optimizations in compile-time more interesting to me, so I can see what's going on and implement it myself, rather than leaving it to another software. Commented Nov 5, 2009 at 11:59

1 Answer 1

23

You are aware of Proguard? http://proguard.sourceforge.net/

This is a great bytecode optimizer which implements a lot of optimizations. See the FAQ for a list: http://proguard.sourceforge.net/FAQ.html

  • Evaluate constant expressions.
  • Remove unnecessary field accesses and method calls.
  • Remove unnecessary branches.
  • Remove unnecessary comparisons and instanceof tests.
  • Remove unused code blocks.
  • Merge identical code blocks.
  • Reduce variable allocation.
  • Remove write-only fields and unused method parameters.
  • Inline constant fields, method parameters, and return values.
  • Inline methods that are short or only called once.
  • Simplify tail recursion calls.
  • Merge classes and interfaces.
  • Make methods private, static, and final when possible.
  • Make classes static and final when possible.
  • Replace interfaces that have single implementations.
  • Perform over 200 peephole optimizations, like replacing ...*2 by ...<<1.
  • Optionally remove logging code.

I'm sure you can further look into the source code to understand how they are implemented.

Sign up to request clarification or add additional context in comments.

7 Comments

IS there a .NET version perhaps?
Merge classes and interfaces: How does the compiler know that it can collapse a type hierarchy? It can only be sure about private interfaces.
In Java, compilation "really" happens at runtime: this is what the JIT does inside the JVM. The javac compiler is just a source to bytecode translator, and is required to emit certain bytecode for given source. So Proguard does part of what JIT does, not what javac does, in the Java ecosystem.
Do any of the standard Java tools, seven years on, do any of these optimizations?
Yes, the JIT. It always did, and is constantly being optimized.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.