Skip to content

Call the C API v2 with the Foreign Function and Memory API on Java 22+ - #1018

Draft
Robinho-MR wants to merge 15 commits into
duckdb:mainfrom
Robinho-MR:java-ffm
Draft

Robinho-MR wants to merge 15 commits into
duckdb:mainfrom
Robinho-MR:java-ffm

Conversation

@Robinho-MR

Copy link
Copy Markdown

Calls the C API v2 directly from Java with the Foreign Function and Memory API (java.lang.foreign), without JNI, on Java 22 and later. It is stacked on #1017 (the commits of that PR are included here, the last four are new). Older JDKs keep using the JNI bindings from the same jar.

java --enable-native-access=ALL-UNNAMED -cp duckdb_jdbc.jar MyApp   // JDK 22+: FFM, JDK 8-21: JNI
Details
  • DuckDBBindings and DuckDBLibrary have a Java 22 version in src/main/java22 that is packed into META-INF/versions/22 of the jars (Multi-Release: true). The 228 native entry points are implemented in Java on top of 212 downcall handles (Capi.java, generated by scripts/gen_ffm_native.py from duckdb_v2.h). Handles are passed around as before (zero-length direct ByteBuffers holding the pointer).
  • Callbacks (scalar and table functions, instance init, Arrow stream struct, profiling output) are upcall stubs. Vectors, validity and strings are read directly from the native memory.
  • All 728 duckdb_v2_* functions are exported from the library (previously 620), scripts/update_exports.py regenerates the .map/.exp/.def lists.
  • The Java 22 classes are compiled with the build JDK if it is 22+, otherwise with JAVA22_HOME. Without one the jars are built without them (JNI only), so building on JDK 8 is unchanged.
  • New CI job running the test suite on JDK 25.

Testing (Linux arm64, JDK 25 with FFM and JDK 21 with JNI, from the built duckdb_jdbc.jar): TestDuckDBJDBC passes except test_table_function_multiple_threads, which requires all 96 processors to take part in a 33-chunk scan and fails the same way on the unchanged driver on this loaded machine. TestArrow 35/35 with FFM.

Performance, same JDK 25, FFM vs JNI (seconds, lower is better, loaded machine): 10M ints 0.16 vs 0.20, 2M strings 0.19 vs 0.20, 50k tiny prepared queries 3.11 vs 3.65, 5k prepares 1.10 vs 1.18, decimals/dates/timestamps equal, chunk appender 0.85 vs 0.80 (noise).

Caveats: --enable-native-access=ALL-UNNAMED is needed to avoid the JDK warning (a future JDK will make it an error). Linker.Option.critical is not used yet. The release jars are built in the manylinux docker images, which would need a JDK 22+ (JAVA22_HOME) to include the FFM classes.

@Mytherin

Copy link
Copy Markdown
Member

@staticlibs

Copy link
Copy Markdown
Member

@Mytherin Thanks for the details! Yes, absolutely, I was talking about this with Hannes quite a while ago, in 2025, - the FFM layer in the driver is highly welcome! Just the JDK 8 compatibility part is tricky, I need to look closer how it is done here and what the new generators are doing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants