Aresxue commented on PR #92:
URL: 
https://github.com/apache/dubbo-hessian-lite/pull/92#issuecomment-3314821131

   > 也许所有的xxxSerializer都需要检查是否存在此类问题,
   > 
   > 1. 不要`addRef`在方法开始时调用`writeObject`,例如,
   > 
   > ```
   > public class LongAdderSerializer extends AbstractSerializer {
   > 
   >     @Override
   >     public void writeObject(Object obj, AbstractHessianOutput out)
   >             throws IOException {
   > 
   >         String replacedClName = 
"java.util.concurrent.atomic.LongAdder$SerializationProxy";
   > 
   >         int ref = out.writeObjectBegin(replacedClName);
   > ```
   > 
   > 每个xxxSerializer都应该调用`addRef`,即使它是一个集合序列化器,就像`CollectionSerializer`一样。
   > 
   > 2. 
直接在处写入一个新的替换对象`writeObject`(不实现`writeReplace`),这会导致引用机制失效,`addRef`此类对象的调用方法总是返回 
false,因为`_refs`基于 key ,每个新对象
   >    都有`identityHashCode`一个几乎唯一的`identityHashCode`(由 JVM 计算,所有重写的 hashCode 
方法都被忽略),偶然发生冲突的概率极低。
   > 
   > ```
   > public class List12Serializer extends AbstractSerializer {
   > 
   >     @Override
   >     public void writeObject(Object obj, AbstractHessianOutput out) throws 
IOException {
   >         if (obj == null) {
   >             out.writeNull();
   >             return;
   >         }
   > 
   >         out.writeObject(new ArrayList<>((List) obj));
   >     }
   > }
   > ```
   > 
   > 3. 在一个 if-else 分支中写入一个新的替换对象`writeObject`,例如,
   > 
   > ```
   > public class LocalDateTimeSerializer<T> extends AbstractSerializer {
   > 
   >     @Override
   >     public void writeObject(Object obj, AbstractHessianOutput out) throws 
IOException {
   >         if (obj == null) {
   >             out.writeNull();
   >             return;
   >         }
   > 
   >         if (SerializationConfig.isCompactMode()) {
   >             ...
   >         } else {
   >             out.writeObject(new LocalDateTimeHandle(obj));
   >         }
   > ```
   > 
   > 
它应该添加重写的`writeReplace()`方法来返回新的LocalDateTimeHandle,并`super.writeObject`在else分支中调用,而不是直接编写新的LocalDateTimeHandle。
   
   I will check all xxxSerializers later, I believe this will reduce CPU and 
memory usage and make memory layout more reasonable.


-- 
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: notifications-unsubscr...@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@dubbo.apache.org
For additional commands, e-mail: notifications-h...@dubbo.apache.org

Reply via email to