Graal 编译器是动态即时 (JIT) 编译技术的根本性飞跃。 JIT 编译在 Java 虚拟机 (JVM) 架构中的作用和功能被誉为 Java 令人印象深刻的性能背后的一个重要因素,由于其复杂且相当不透明的性质,常常让许多从业者感到困惑。
当您执行 javac 命令或使用 IDE 时,您的 Java 程序将从 Java 源代码转换为 JVM 字节码。这个
该过程创建 Java 程序的二进制表示 - 一种比原始源代码更简单、更紧凑的格式。
但是,计算机或服务器中的经典处理器无法直接执行 JVM 字节码。这需要 JVM 来解释字节码。
图 1 – 即时 (JIT) 编译器的工作原理
与在实际处理器上运行的本机代码相比,解释器的性能通常较差,这促使 JVM 在运行时调用另一个编译器 - JIT 编译器。 JIT 编译器将字节码转换为处理器可以直接运行的机器代码。这个复杂的编译器执行一系列高级优化来生成高质量的机器代码。
该字节码充当中间层,使 Java 应用程序能够在具有不同处理器架构的各种操作系统上运行。 JVM本身就是一个软件程序,它逐条指令地解释这个字节码。
JVM 的 OpenJDK 实现包含两个传统的 JIT 编译器 - 客户端编译器 (C1) 和服务器编译器(C2 或 Opto)。客户端编译器针对更快的操作和较少优化的代码输出进行了优化,使其成为桌面应用程序的理想选择,因为长时间的 JIT 编译暂停可能会中断用户体验。相反,服务器编译器旨在花费更多时间来生成高度优化的代码,使其适合长时间运行的服务器应用程序。
两个编译器可以通过“分层编译”串联使用。最初,代码通过 C1 编译,如果执行频率证明额外的编译时间合理,则随后通过 C2 编译。
C2 采用 C++ 开发,尽管具有高性能特性,但也有固有的缺点。 C++是一种不安全的语言;因此,C2 模块中的错误可能会导致整个虚拟机崩溃。继承的C++代码的复杂性和刚性也导致其维护性和可扩展性成为重大挑战。
Graal 独有的,这个 JIT 编译器是用 Java 开发的。编译器的主要要求是接受 JVM 字节码并输出机器代码 - 一种不需要 C 或 C++ 等系统级语言的高级操作。
用 Java 编写的 Graal 有几个优点:
提高安全性:Java 的垃圾收集和托管内存方法消除了 JIT 编译器本身与内存相关的崩溃风险。
更容易维护和扩展:Java 代码库更易于开发人员贡献和扩展 JIT 编译器的功能。
可移植性:Java 的平台独立性意味着 Graal JIT 编译器可以在任何具有 Java 虚拟机的平台上工作。
JVM 编译器接口 (JVMCI) 是 JVM 中的一项创新功能和新接口 (JEP 243:https://openjdk.org/jeps/243)。
与 Java 注释处理 API 非常相似,JVMCI 也允许集成自定义 Java JIT 编译器。
JVMCI 接口包含一个从 byte 到 byte[] 的纯函数:
1 2 3 4 |
|
这并没有捕捉到现实生活场景的全部复杂性。
在实际应用中,我们经常需要额外的信息,例如局部变量的数量、堆栈大小以及从解释器中分析收集的数据,以更好地了解代码的执行情况。因此,该界面需要更复杂的输入。它不仅仅接受字节码,还接受 CompilationRequest:
1 2 3 4 5 6 |
|
JVMCICompiler.java
CompilationRequest 封装了更全面的信息,例如哪个 JavaMethod 用于编译,以及编译器可能需要的更多数据。
CompilationRequest.java
This approach has the benefit of providing all necessary details to the custom JIT-compiler in a more organized and contextual manner. To create a new JIT-compiler for the JVM, one must implement the JVMCICompiler interface.
An aspect where Graal truly shines in terms of performing sophisticated code optimization is in its use of a unique data structure: the program-dependence-graph, or colloquially, an "Ideal Graph".
The program-dependence-graph is a directed graph that presents a visual representation of the dependencies between individual operations, essentially laying out the matrix of dependencies between different parts of your Java code.
Let's illustrate this concept with a simple example of adding two local variables, x and y. The program-dependence-graph for this operation in Graal's context would involve three nodes and two edges:
Nodes:
Edges:
1 2 3 4 5 6 7 8 |
|
In this illustration, the arrows represent the data flow between the nodes. The Load(x) and Load(y) nodes feed their loaded values into the Add node, which performs the addition operation. This visual representation helps Graal identify potential optimizations based on the dependencies between these operations.
This graph-based architecture provides the Graal compiler with a clear visible landscape of dependencies and scheduling in the code it compiles. The program-dependence-graph not only maps the flow of data and relationships between operations but also offers a canvas for Gaal to manipulate these relationships. Each node on the graph is a clear candidate for specific optimizations, while the edges indicate where alterations would propagate changes elsewhere in the code - both aspects influence how Graal optimizes your program's performance.
Visualizing and analyzing this graph can be achieved through a tool called the IdealGraphVisualizer, or IGV. This tool is invaluable in understanding the intricacies of Graal's code optimization capabilities. It allows you to pinpoint how specific parts of your code are being analyzed, modified, and optimized, providing valuable insights for further code enhancements.
Let's consider a simple Java program that performs a complex operation in a loop:
1 2 3 4 5 6 7 8 9 10 11 |
|
When compiled with Graal, the Ideal Graph for this program would look something like this(Figure 2).
Figure 2 – Graal Graphs
Therefore, along with its method level optimizations and overall code performance improvements, this graph-based representation constitutes the key to understanding the power of the Graal compiler in optimizing your Java applications
The Graal JIT compiler represents a significant leap forward in Java performance optimization. Its unique characteristic of being written in Java itself offers a compelling alternative to traditional C-based compilers. This not only enhances safety and maintainability but also paves the way for a more dynamic and adaptable JIT compilation landscape.
The introduction of the JVM Compiler Interface (JVMCI) further amplifies this potential. By allowing the development of custom JIT compilers in Java, JVMCI opens doors for further experimentation and innovation. This could lead to the creation of specialized compilers targeting specific needs or architectures, ultimately pushing the boundaries of Java performance optimization.
In essence, Graal and JVMCI represent a paradigm shift in JIT compilation within the Java ecosystem. They lay the foundation for a future where JIT compilation can be customized, extended, and continuously improved, leading to even more performant and versatile Java applications.
以上是探索 Graal:下一代 Java JIT 编译的详细内容。更多信息请关注PHP中文网其他相关文章!