Skip to content

[Java] Support graalvm native image#1143

Merged
theweipeng merged 20 commits intoapache:mainfrom
chaokunyang:support_graalvm_native_image
Nov 28, 2023
Merged

[Java] Support graalvm native image#1143
theweipeng merged 20 commits intoapache:mainfrom
chaokunyang:support_graalvm_native_image

Conversation

@chaokunyang
Copy link
Collaborator

@chaokunyang chaokunyang commented Nov 25, 2023

What do these changes do?

This PR supported run fury on graalvm native image by generate all serializer code and class at graalvm build time. The main changes includes:

  • Make all Unsafe.objectOffset adapt to graalvm auto replace pattern
  • Generate unsafe offset in serializer code following this pattern too: Generate all unsafe offsets as static final variable.
  • Avoid creating thread and other resources at graalvm build time
  • Support MethodHandle invokeDynamic and java.lang.invoke.LambdaMetafactory codegen by cache the generated functions at build time

Usage Example:

import io.fury.Fury;
import io.fury.util.Preconditions;

import java.util.List;
import java.util.Map;

public class RecordExample {
  public record Record (
    int f1,
    String f2,
    List<String> f3,
    Map<String, Long> f4) {
  }

  static Fury fury;

  static {
    fury = Fury.builder().requireClassRegistration(true).build();
    // register and generate serializer code.
    fury.register(Record.class, true);
  }

  public static void main(String[] args) {
    Record record = new Record(10, "abc", List.of("str1", "str2"), Map.of("k1", 10L, "k2", 20L));
    System.out.println(record);
    byte[] bytes = fury.serialize(record);
    Object o = fury.deserialize(bytes);
    System.out.println(o);
    Preconditions.checkArgument(record.equals(o));
  }
}

The Fury instance must be hold as an static variable and the enclosing class must be init at graalvm build time.
Neither graalvm reflect config nor serialization config as described in https://www.graalvm.org/jdk17/reference-manual/native-image/metadata/#serialization are needed.

The thread safe fury support and detailed user document will be provided in following up PRs.

Related issue number

Closes #1115 #678

Check code requirements

  • tests added / passed (if needed)
  • Ensure all linting tests pass, see here for how to run them

@chaokunyang chaokunyang force-pushed the support_graalvm_native_image branch from 3d01903 to 8057fa0 Compare November 25, 2023 19:01
@chaokunyang chaokunyang force-pushed the support_graalvm_native_image branch 2 times, most recently from 2d7acd1 to f528c69 Compare November 27, 2023 03:02
@chaokunyang chaokunyang force-pushed the support_graalvm_native_image branch from d7c2c60 to 3fd9a21 Compare November 28, 2023 06:31
@chaokunyang chaokunyang changed the title [WIP] Support graalvm native image [Java] Support graalvm native image Nov 28, 2023
@chaokunyang
Copy link
Collaborator Author

chaokunyang commented Nov 28, 2023

Hi @aigens @geoand @ennerf @Ran-Mewo , I just supported fury in graalvm native image in this pr, it would be really great if you can help review this PR? You have more knowlege and experience in this field. Your inputs and comments would be invalueable to us.

Copy link

@geoand geoand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The GraalVM stuff looks good!

@theweipeng theweipeng merged commit e8ff75a into apache:main Nov 28, 2023
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.

[Java] Graal Native Image Support

3 participants