zhaommmmomo commented on code in PR #1701:
URL: https://github.com/apache/fury/pull/1701#discussion_r1671760842
##########
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:
This looks like changing `instanceof` to `Class#isAssignableFrom`, maybe we
can do this:
```java
Class<?> cls = obj.getClass();
if (FuryCopyable.class.isAssignableFrom(cls)) {
return (T) ((FuryCopyable<?>) obj).copy(this);
} else {
return (T)
classResolver.getOrUpdateClassInfo(cls).getSerializer().copy(obj);
}
```
The benefits of doing this are:
1. No need to instantiate an additional `FuryCopyableSerializer` object
2. For `write/read`, do not call `FuryCopyableSerializer`, but use the
original `Serializer`
--
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]