Is it possible to implement a part of a java application directly in jvm?

126 views Asked by At

I was taught about ijvm (a subset of jvm) at my university and it prompted the question: Is it possible to implement a part of a java application directly in jvm for performance improvements? ... In the same way that you can implement a part of a C-program in assembly.

If it's possible, I would greatly appreciate a snippet of code showcasing it.

1

There are 1 answers

0
Stephen C On BEST ANSWER

Is it possible to implement a part of a java application directly in jvm?

I presume that you mean programming in JVM bytecodes.

Yes it is possible. However, it is unlikely that you get significant improvements by doing this.

Why?

Because a modern JVM1 actually compiles bytecodes to native code, and the bytecode to native code compiler typically does some pretty serious optimization.

So:

  • Any insights that you have about writing better (faster) bytecodes are probably wrong.

  • By optimizing the bytecodes, you may actually be interfering with the bytecode to native code compiler's ability to optimize.

In addition, JVM bytecodes need to follow some fairly strict rules that are designed to avoid runtime type errors. If your hand written bytecodes break these rules, they will (should!) be rejected by the verifier at class load time.

But if you want to try, there are tools for writing bytecode assembly language; see What JVM assemblers are there?.


1 - JIT compilation was first supported in mainstream (Sun) Java in JDK 1.2. Prior to that, JVM's only interpreted bytecodes, and hand-coding bytecodes was more likely to have made a difference. But in those days the way to get better performance was to code the performance critical parts of your Java application in C and calling the C via JNI.