chaokunyang commented on code in PR #1701:
URL: https://github.com/apache/fury/pull/1701#discussion_r1670419093


##########
java/fury-core/src/main/java/org/apache/fury/Fury.java:
##########
@@ -1221,15 +1240,55 @@ public Object 
deserializeJavaObjectAndClass(FuryReadableChannel channel) {
     return deserializeJavaObjectAndClass(buf);
   }
 
+  @SuppressWarnings("unchecked")
   @Override
   public <T> T copy(T obj) {
     if (Objects.isNull(obj)) {
       return null;
     }
-    if (obj instanceof FuryCopyable) {
-      return (T) ((FuryCopyable) obj).copy(this);
-    } else {
-      return (T) 
classResolver.getOrUpdateClassInfo(obj.getClass()).getSerializer().copy(obj);
+    try {
+      copyDepth++;
+      if (Objects.isNull(originToCopyMap)) {
+        originToCopyMap = new IdentityMap();
+      }
+      Object newObj = originToCopyMap.get(obj);
+      if (Objects.nonNull(newObj)) {
+        return (T) newObj;
+      }
+      if (copyRefTracking) {
+        originObjRef = obj;
+      }
+      T copy;
+      if (obj instanceof FuryCopyable) {

Review Comment:
   We'd better to create a new Serializer like FuryCopyableSerializer for this 
instead of using isntanceof here. It's very slow.
   
   The code could looks like in getSerializer:
   ```java
   serializer = XXX;
   if (FuryCopyable.class.isAssignableFrom(cls)) {
     serializer = new FuryCopyableSerializer(serializer);
   }
   
   class FuryCopyableSerializer {
    private Serializer serializer;
   
     write(...) {
     serializer.write(...);
     }
   
     read(...) {
     serializer.read(...);
     }
   
     T copy(T x) {
             return (T) ((FuryCopyable<?>) obj).copy(x);
     }
   
   }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to