3

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.

Sebastian Nielsen
  • 3,835
  • 5
  • 27
  • 43
  • 1
    If you think you can outsmart the compiler, [sure](https://stackoverflow.com/questions/6176667/what-jvm-assemblers-are-there) (not sure the mentioned projects are still actively developed, my point is that if your motivation is "performance" than that's probably not the route). – Federico klez Culloca Jun 17 '21 at 15:22
  • @FedericoklezCulloca But hypothetically, are you saying that it *is* possible to improve performance in this way? (however minor that performance-improvement might be) – Sebastian Nielsen Jun 17 '21 at 15:28
  • Everything is possible, you can compile a JVM yourself, OpenJDK is open source. Then you can put and leave out whatever you want. Also, remember/read about JNI and JNA. – tevemadar Jun 17 '21 at 15:30
  • @SebastianNielsen I'm saying that no, I'm (almost) positive the compiler will always generate more performant code (of course I can't *prove* that). If you really want a performance improvement and you think you can do better than the compiler, write the performance-intensive parts in C or, for even more gains, in your machine's native machine language and link to them via the JNI. – Federico klez Culloca Jun 17 '21 at 15:32
  • 1
    The phrase “implement … directly in jvm” makes no sense. The JVM is not a programming language. – Holger Jun 18 '21 at 09:56

1 Answers1

6

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216