Skip to content

Java 25, the Foreign Function & Memory API and removal of Unsafe #259

@at055612

Description

@at055612

@stroomdev66 and I (@at055612) have recently been added as committers on this project to help out @benalexau, having been long time users of LmdbJava and occasional contributors.
We actively use LmdbJava in a number of different use cases in our OSS project and are very grateful to @benalexau, @krisskross and all the other contributors for a great library.

Now that Java 25 and its new Foreign Function and Memory API are in general availability, it seems sensible to incorporate them into LmdbJava.
This has been a long anticipated change (see #42).
We are therefore keen to make changes to LmdbJava to incorporate the FFM API and remove the use of Unsafe.
@stroomdev66 has done some initial experimentation with replacing jnr-ffi with the new FFM API.

Such changes need to be done in a way that does not cause too much pain for the LmdbJava user community.
We have had some initial exploratory conversations with @benalexau about the way forward and this is a current rough plan.


LmdbJava v1.x

Users that are not in a position to move to Java 25 can continue to use v1.x with no change.

LmdbJava v2.x

  • Change the Java compile target to Java 25.
  • Change the internal LmdbJava code to use the FFM API for interaction with LMDB.
  • Change Env to create and hold on to an Arena instance for its life.
    This will be used for creating MemorySegments internally.
  • Change the internal LmdbJava code to use MemorySegments when interacting with LMDB.
  • Change the existing BufferProxy implementations to map the buffers to MemorySegments.
  • Add a new MemorySegmentProxy class so that client code can fully exploit MemorySegment, e.g. Env<MemorySegment>.
  • Remove any use of Unsafe.

This seems like the best way to use the FFM API without causing a lot of breaking change for users of LmdbJava.
It may come at the cost of some performance penalty due to mapping between buffer implementations and MemorySegment.
There may also be performance differences between MemorySegment and Unsafe buffer access.

This work is currently in progress on this branch:
https://github.com/lmdbjava/lmdbjava/tree/java-foreign-function-and-memory-api

and there is a draft PR for the work here #258.

Users that can migrate to Java 25, but have a lot of existing code built around Netty/Agrona/DirectByteBuffer can use this with hopefully no or minimal change.

LmdbJava v3.x

  • Include all of the v2.x work.
  • Remove the generic <T> type from Env/Dbi/Txn, etc.
  • Change the LmdbJava API to exclusively use MemorySegment rather than T.
  • Remove the netty and agrona dependencies.
  • Remove BufferProxy and all its implementations.

We think that ultimately MemorySegment is the natural replacement of the various buffer implementations.
It offers a much richer API for working with ranges of memory.
Therefore it seems sensible for MemorySegment to be the only mechanism for passing data in/out of LMDB.

We assume that for people that use Netty and Agrona in their projects, that Netty/Agrona will in time improve their libraries to integrate with MemorySegments so that LmdbJava users can map back/forth themselves.

Removing the various buffer implementations will make the LmdbJava code simpler and make it easier for the LmdbJava maintainers to maintain the project and support users' issues.

Users who can migrate to Java 25 and have little or no legacy buffer based code can move to this version.


We welcome any comments or suggestions that people have on the Java25/FFM/Unsafe changes and/or the general direction of LmdbJava development.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementAdditional or new functionality

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions