简体   繁体   English

是否可以直接在 jvm 中实现 java 应用程序的一部分?

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

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?我在我的大学学习了ijvm (jvm 的一个子集),它提出了一个问题:是否可以直接在 jvm 中实现 java 应用程序的一部分以提高性能? ... In the same way that you can implement a part of a C-program in assembly. ... 就像您可以在汇编中实现 C 程序的一部分一样。

If it's possible, I would greatly appreciate a snippet of code showcasing it.如果可能,我将非常感谢展示它的代码片段。

Is it possible to implement a part of a java application directly in jvm?是否可以直接在 jvm 中实现 java 应用程序的一部分?

I presume that you mean programming in JVM bytecodes.我认为您的意思是在 JVM 字节码中编程。

Yes it is possible.对的,这是可能的。 However, it is unlikely that you get significant improvements by doing this.但是,您不太可能通过这样做获得显着的改进。

Why?为什么?

Because a modern JVM 1 actually compiles bytecodes to native code, and the bytecode to native code compiler typically does some pretty serious optimization.因为现代 JVM 1实际上将字节码编译为本机代码,而字节码到本机代码编译器通常会进行一些非常认真的优化。

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.此外,JVM 字节码需要遵循一些相当严格的规则,这些规则旨在避免运行时类型错误。 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?请参阅有哪些 JVM 汇编程序? . .


1 - JIT compilation was first supported in mainstream (Sun) Java in JDK 1.2. 1 - JIT 编译首先在 JDK 1.2 中的主流 (Sun) Java 中得到支持。 Prior to that, JVM's only interpreted bytecodes, and hand-coding bytecodes was more likely to have made a difference.在此之前,JVM 仅解释字节码,而手动编码字节码更有可能产生影响。 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.但在那些日子里,获得更好性能的方法是用 C 编写 Java 应用程序的性能关键部分,并通过 JNI 调用 C。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM