https://github.com/mrjameshamilton/simple-java-class-file-api-optimizer
A simple Java bytecode optimiser
https://github.com/mrjameshamilton/simple-java-class-file-api-optimizer
bytecode class-file-api java jep457 jep466 jep484 jvm optimizer peephole-optimization
Last synced: 3 months ago
JSON representation
A simple Java bytecode optimiser
- Host: GitHub
- URL: https://github.com/mrjameshamilton/simple-java-class-file-api-optimizer
- Owner: mrjameshamilton
- License: mit
- Created: 2024-11-23T13:52:15.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-15T14:13:53.000Z (about 1 year ago)
- Last Synced: 2025-04-11T21:52:07.141Z (about 1 year ago)
- Topics: bytecode, class-file-api, java, jep457, jep466, jep484, jvm, optimizer, peephole-optimization
- Language: Java
- Homepage: https://jameshamilton.eu/programming/peering-through-peephole-build-peephole-optimiser-using-new-java-24-class-file-api
- Size: 17.6 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Simple Java Class File Optimiser
Peephole optimisation is a simple technique that can yield surprisingly good results with little complexity.
Optimizer.java implements a simple peephole optimiser using the Java Class-File API.
Two optimisations are implemented, as an example:
* Removal of redundant addition/subtraction of zero e.g. int x = y + 0;
* Merging of consecutive StringBuilder.append calls with constant strings
e.g. sb.append("foo").append("bar") -> sb.append("foobar");
The optimiser uses the Java Class-File API (JEP 457 / 466 / 484) released
in final form in Java 24. The Optimizer.java source file is self-contained
and does not require any external dependencies apart from the Java 24 standard library.
Further details can be found in [the accompanying blog post](https://jameshamilton.eu/programming/peering-through-peephole-build-peephole-optimiser-using-new-java-24-class-file-api).
# Run
You'll need JDK 24, the easiest way to install this, on Linux, is with [SDK man](https://sdkman.io/):
```shell
sdk install java 24.ea.36-open
```
You can run the optimizer directly from source using the java command:
```shell
$ java Optimizer.java input.jar output.jar
```
# Test case
The TestJarGenerator.java can generate a test jar to showcase the two optimisations
that are applied:
```shell
$ java TestJarGenerator.java test.jar
$ java Optimizer.java test.jar optimized.jar
$ java -jar test.jar
$ java -jar optimized.jar
```
The script `run.sh` runs the test jar generator and the optimizer in one go.