首頁 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[] 的純函數:

雷雷

這並沒有捕捉到現實生活場景的全部複雜性。

在實際應用中,我們經常需要額外的信息,例如局部變數的數量、堆疊大小以及從解釋器中分析收集的數據,以更好地了解程式碼的執行情況。因此,該介面需要更複雜的輸入。它不只接受字節碼,也接受 CompilationRequest:

雷雷

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

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++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教學
1677
14
CakePHP 教程
1431
52
Laravel 教程
1333
25
PHP教程
1279
29
C# 教程
1257
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系統的接口字段有效地映�...

如何優雅地獲取實體類變量名構建數據庫查詢條件? 如何優雅地獲取實體類變量名構建數據庫查詢條件? Apr 19, 2025 pm 11:42 PM

在使用MyBatis-Plus或其他ORM框架進行數據庫操作時,經常需要根據實體類的屬性名構造查詢條件。如果每次都手動...

IntelliJ IDEA是如何在不輸出日誌的情況下識別Spring Boot項目的端口號的? IntelliJ IDEA是如何在不輸出日誌的情況下識別Spring Boot項目的端口號的? Apr 19, 2025 pm 11:45 PM

在使用IntelliJIDEAUltimate版本啟動Spring...

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