Question

Sometimes we hear about brave people who understand and write assembly language for performance reasons, as opposed to using a compiler with a high-level language. Can the same be done on the JVM? I've reviewed the JVM instruction set, and it resembles assembly language in some respects, though it's much higher level (I'm assuming that the system-specific implementations of the JVM are extremely efficient).

Is it possible to, say, write JVM instructions and put them into a Java-executable binary?

Était-ce utile?

La solution

Yes. You can do this via the asm library.

In fact, this is typically how people implement non-Java languages on top of the JVM, and how many Java metaprogramming libraries work.

You may very well want to do this for the same kind of metaprogramming capabilities - e.g., generating classes at runtime, or using the InvokeDynamic instruction to generate your own method dispatch rules.

There isn't a whole lot of performance benefit to be gained from using raw Java bytecode rather than writing the corresponding high-level Java (the JIT is your main performance booster, and it's optimized for the sorts of patterns "vanilla" Java code generates) but it does give you flexibility for things that are difficult, verbose, or impossible to express in Java.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top