cover of episode “The Panama Effect” with Jorn Vernee

“The Panama Effect” with Jorn Vernee

2024/1/8
logo of podcast Inside Java

Inside Java

People
J
Jorn Vernee
Topics
Jorn Vernee: 本次访谈主要介绍了JDK 22中最终确定的外部函数和内存API。外部内存访问API旨在替代Unsafe和ByteBuffer API,它使用long型偏移量支持更大的内存段,并提供更多访问模式和内存布局描述功能,还支持确定性内存分配和释放,解决了ByteBuffer API在内存大小和访问模式上的限制。外部函数API旨在替代JNI,它允许开发者使用纯Java代码访问本地库,无需编写中间层代码,简化开发流程并提高性能,避免了学习其他编程语言和设置额外构建流程的麻烦。在孵化和预览阶段,API经历了多次迭代,主要改进包括完善API功能、改进编程模型(引入arena机制管理内存生命周期)、实现更多函数调用形式以及添加基于libffi库的回退实现,最终支持所有平台。与JNI相比,外部函数和内存API具有性能优势,被称为“Panama效应”。由于中间层代码迁移到Java端,JIT编译器可以更好地优化代码,提高性能。在性能方面还有其他改进,例如针对使用long型变量的循环进行了优化,以及将本地库回调Java代码的性能提升了三倍。多个大型项目(例如Lucene、Netty、Tomcat)已经采用了外部函数和内存API,并取得了性能提升和代码安全性的改进。如果项目使用Unsafe或JNI,或者需要访问本地库,那么外部函数和内存API是理想的选择。该API也可以用于访问Java堆内存。可以使用Jextract工具来简化将本地库集成到Java代码的过程,该工具可以自动生成Java包装器函数。外部函数和内存API已在JDK 22中最终确定,并对Jextract工具进行持续改进。 Ana Maria Michelchano: 作为主持人,Ana Maria Michelchano引导访谈,提出问题,并对Jorn Vernee的回答进行总结和补充。她对Project Panama的演进和外部函数及内存API的应用场景和性能提升表现出浓厚的兴趣,并积极与Jorn Vernee进行互动,确保听众能够清晰地理解这些新技术的细节和优势。

Deep Dive

Key Insights

What is the Foreign Memory Access API and what are its main goals?

The Foreign Memory Access API is designed to replace the Unsafe and ByteBuffer APIs, addressing their limitations such as size constraints and limited access modes. It supports long offsets, enabling larger memory segments, and includes advanced access modes like volatile and compare-and-swap. It also introduces deterministic memory allocation, allowing developers to explicitly free memory when needed.

How does the Foreign Function API improve upon JNI?

The Foreign Function API eliminates the need for an intermediate glue code layer written in C or C++. Instead, the entire intermediate layer can be written in pure Java, simplifying the process and reducing the need for additional build pipelines or learning new languages. This also improves performance due to better visibility and optimization by the JIT compiler.

What significant changes were made to the Foreign Function and Memory API during its incubation and preview phases?

The API went through several iterations, with significant redesigns of the programming model, particularly around lifetime management of memory resources. The introduction of 'arenas' allows for safer and more deterministic memory management. Additionally, support for more function shapes and platforms was added, and a fallback implementation based on libffi was introduced.

What performance improvements does the Foreign Function and Memory API offer over JNI?

The API benefits from the 'Panama effect,' where the JIT compiler can optimize the glue code written in Java more effectively than the intermediate C/C++ code used in JNI. Additionally, upcalls (native code calling back into Java) are up to three times faster due to the efficient handling of method handles and machine code stubs.

Which Java libraries have already adopted the Foreign Function and Memory API, and what benefits have they observed?

Libraries like Lucene, Netty, and Tomcat have adopted the API, primarily to replace their usage of Unsafe. These libraries have observed performance parity with Unsafe while gaining safer memory management through the API's lifetime management features.

How can developers start integrating a native library into their Java code using the Foreign Function and Memory API?

Developers can use the Jextract tool, which parses a native library's header file and automatically generates Java wrapper functions. This allows developers to call native library functions directly from Java without writing any C/C++ code.

What is the current status of the Foreign Function and Memory API, and what future improvements are planned?

The API is finalized in JDK 22 and no longer requires the --enable-preview flag. Future work includes improving the Jextract tool to generate more self-contained and editable code snippets for easier integration with Java projects.

Chapters
The Foreign Memory Access API replaces the unsafe and byte buffer APIs, offering improvements like long offsets for larger memory segments, various access modes via var handles, and deterministic allocation for explicit memory freeing. This addresses limitations of previous APIs in memory size, access modes, and memory management control.
  • Replaces unsafe and byte buffer APIs
  • Uses long offsets for larger memory segments
  • Supports various access modes (set volatile, compare and exchange, etc.)
  • Offers deterministic allocation and explicit memory freeing

Shownotes Transcript

The Foreign Function and Memory API will be finalised in JDK 22. This API helps you integrate native code within your Java program. 

Using this new API you can efficiently invoke code outside the JVM, safely access memory not managed by the JVM, call native libraries and process native data without the brittleness and danger of JNI.

Jorn Vernee, core contributor and maintainer of the FFM API, is Ana’s guest during this episode. Jorn explains what is the Foreign Memory Access API, its goals and the iterations that this API went through. Jorn also shares a few examples of Java libraries that already adopted the FFM API, the performance improvements they observed. He also explains how you can use this new API to integrate a native library within your own Java code.