首页 Java java教程 探索 Graal:下一代 Java JIT 编译

探索 Graal:下一代 Java JIT 编译

Aug 31, 2024 pm 04:36 PM

Graal 编译器是动态即时 (JIT) 编译技术的根本性飞跃。 JIT 编译在 Java 虚拟机 (JVM) 架构中的作用和功能被誉为 Java 令人印象深刻的性能背后的一个重要因素,由于其复杂且相当不透明的性质,常常让许多从业者感到困惑。

什么是 JIT 编译器?

当您执行 javac 命令或使用 IDE 时,您的 Java 程序将从 Java 源代码转换为 JVM 字节码。这个
该过程创建 Java 程序的二进制表示 - 一种比原始源代码更简单、更紧凑的格式。

但是,计算机或服务器中的经典处理器无法直接执行 JVM 字节码。这需要 JVM 来解释字节码。

Exploring Graal: Next-Generation JIT Compilation for Java

图 1 – 即时 (JIT) 编译器的工作原理

与在实际处理器上运行的本机代码相比,解释器的性能通常较差,这促使 JVM 在运行时调用另一个编译器 - JIT 编译器。 JIT 编译器将字节码转换为处理器可以直接运行的机器代码。这个复杂的编译器执行一系列高级优化来生成高质量的机器代码。

该字节码充当中间层,使 Java 应用程序能够在具有不同处理器架构的各种操作系统上运行。 JVM本身就是一个软件程序,它逐条指令地解释这个字节码。

Graal JIT 编译器——用 Java 编写

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 编译器接口 (JVMCI) 是 JVM 中的一项创新功能和新接口 (JEP 243:https://openjdk.org/jeps/243)。
与 Java 注释处理 API 非常相似,JVMCI 也允许集成自定义 Java JIT 编译器。

JVMCI 接口包含一个从 byte 到 byte[] 的纯函数:

interface JVMCICompiler {

  byte[] compileMethod(byte[] bytecode);
}
登录后复制

这并没有捕捉到现实生活场景的全部复杂性。

在实际应用中,我们经常需要额外的信息,例如局部变量的数量、堆栈大小以及从解释器中分析收集的数据,以更好地了解代码的执行情况。因此,该界面需要更复杂的输入。它不仅仅接受字节码,还接受 CompilationRequest:

public interface JVMCICompiler {

  int INVOCATION_ENTRY_BCI = -1;

  CompilationRequestResult compileMethod(CompilationRequest request);
}
登录后复制

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.

Ideal Graph

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:

    • Load(x) and Load(y): These nodes represent the operations of loading the values of variables x and y from memory into registers within the processor.
    • Add: This node embodies the operation of adding the values loaded from x and y.
  • Edges:

    • Two edges would be drawn from the Load(x) and Load(y) nodes to the Add node. These directional paths convey the data flow. They signify that the values loaded from x and y are the inputs to the addition operation.
      +--------->+--------->+
      | Load(x)  | Load(y)  |
      +--------->+--------->+
                 |
                 v
              +-----+
              | Add |
              +-----+
登录后复制

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:

public class Demo {
 public static void main(String[] args) {
        for (int i = 0; i < 1_000_000; i++) {
            System.err.println(complexOperation(i, i + 2));
        }
    }

    public static int complexOperation(int a, int b) {
        return ((a + b)-a) / 2;
    }
}
登录后复制

When compiled with Graal, the Ideal Graph for this program would look something like this(Figure 2).

Exploring Graal: Next-Generation JIT Compilation for Java

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

In Conclusion

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中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

<🎜>:泡泡胶模拟器无穷大 - 如何获取和使用皇家钥匙
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系统,解释
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆树的耳语 - 如何解锁抓钩
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Java教程
1669
14
CakePHP 教程
1428
52
Laravel 教程
1329
25
PHP教程
1273
29
C# 教程
1256
24
公司安全软件导致应用无法运行?如何排查和解决? 公司安全软件导致应用无法运行?如何排查和解决? Apr 19, 2025 pm 04:51 PM

公司安全软件导致部分应用无法正常运行的排查与解决方法许多公司为了保障内部网络安全,会部署安全软件。...

如何将姓名转换为数字以实现排序并保持群组中的一致性? 如何将姓名转换为数字以实现排序并保持群组中的一致性? Apr 19, 2025 pm 11:30 PM

将姓名转换为数字以实现排序的解决方案在许多应用场景中,用户可能需要在群组中进行排序,尤其是在一个用...

如何使用MapStruct简化系统对接中的字段映射问题? 如何使用MapStruct简化系统对接中的字段映射问题? Apr 19, 2025 pm 06:21 PM

系统对接中的字段映射处理在进行系统对接时,常常会遇到一个棘手的问题:如何将A系统的接口字段有效地映�...

IntelliJ IDEA是如何在不输出日志的情况下识别Spring Boot项目的端口号的? IntelliJ IDEA是如何在不输出日志的情况下识别Spring Boot项目的端口号的? Apr 19, 2025 pm 11:45 PM

在使用IntelliJIDEAUltimate版本启动Spring...

如何优雅地获取实体类变量名构建数据库查询条件? 如何优雅地获取实体类变量名构建数据库查询条件? Apr 19, 2025 pm 11:42 PM

在使用MyBatis-Plus或其他ORM框架进行数据库操作时,经常需要根据实体类的属性名构造查询条件。如果每次都手动...

Java对象如何安全地转换为数组? Java对象如何安全地转换为数组? Apr 19, 2025 pm 11:33 PM

Java对象与数组的转换:深入探讨强制类型转换的风险与正确方法很多Java初学者会遇到将一个对象转换成数组的�...

电商平台SKU和SPU数据库设计:如何兼顾用户自定义属性和无属性商品? 电商平台SKU和SPU数据库设计:如何兼顾用户自定义属性和无属性商品? Apr 19, 2025 pm 11:27 PM

电商平台SKU和SPU表设计详解本文将探讨电商平台中SKU和SPU的数据库设计问题,特别是如何处理用户自定义销售属...

如何利用Redis缓存方案高效实现产品排行榜列表的需求? 如何利用Redis缓存方案高效实现产品排行榜列表的需求? Apr 19, 2025 pm 11:36 PM

Redis缓存方案如何实现产品排行榜列表的需求?在开发过程中,我们常常需要处理排行榜的需求,例如展示一个�...

See all articles