apache / fury

A blazingly fast multi-language serialization framework powered by JIT and zero-copy.
https://fury.apache.org/
Apache License 2.0
3.02k stars 223 forks source link

[Java] native object copy support #1014

Open DemonJun opened 11 months ago

DemonJun commented 11 months ago

During data stream processing, it is often necessary to deep copy data for multiple times, especially when there are too many class attributes and subclasses, manual deep copying is too much trouble. Similarly:

@SuppressWarnings("unchecked")
public static <T> T clone(T ob) {
  MemoryBuffer memoryBuffer = getAndClearMb();

  Fury fury = get();

  fury.serializeJavaObject(memoryBuffer, ob);

  return (T) fury.deserializeJavaObject(memoryBuffer, ob.getClass());
}

It would be nice to be able to use zero-copy to further improve performance.

chaokunyang commented 11 months ago

One of the solution is adding a copy interface to io.fury.Serializer:

  public T copy(T value) {
    throw new UnsupportedOperationException(String.format("Copy for %s is not supported", value.getClass()));
  }

For immutable types such as String/java.time.*, this implementation just return the passed object. For mutable object such as ArrayList/HashMap/POJO, we copy the data recursive in copy implementation.

caicancai commented 11 months ago

I want to try it,but I may need a little time to learn this

chaokunyang commented 11 months ago

I want to try it,but I may need a little time to learn this

Looking forward to it! FYI, here are my some thoughts, you can take it for inspiration: