deardeng commented on code in PR #66447:
URL: https://github.com/apache/doris/pull/66447#discussion_r3733878664


##########
fe/fe-core/src/main/java/org/apache/doris/cloud/catalog/CloudTabletRebalancer.java:
##########
@@ -334,7 +339,9 @@ public boolean equals(Object o) {
 
         @Override
         public int hashCode() {
-            return Objects.hash(tabletId, clusterId);
+            int result = 1;

Review Comment:
     ### hashCode() 原理
   
     修改前是:
   
     return Objects.hash(tabletId, clusterId);
   
     JDK 规定 Objects.hash(values...) 等价于把参数放入数组,再调用 
Arrays.hashCode(Object[])。JDK Objects 文档 
(https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Objects.html#hash(java.lang.Object...))
   
     其计算公式是:
   
     int result = 1;
     result = 31 * result + Long.hashCode(tabletId);
     result = 31 * result + clusterId.hashCode();
   
     因此现在的代码:
   
     int result = 1;
     result = 31 * result + Long.hashCode(tabletId);
     return 31 * result + clusterId.hashCode();
   
     与原来的 hash 值逐位完全一致,只是展开写出来了。
   
     原实现实际上类似:
   
     Objects.hash(Long.valueOf(tabletId), clusterId);
   
     热点路径中可能产生:
   
     - 一个 Object[] varargs 数组;
     - 一个 boxed Long;
     - Arrays.hashCode() 的通用循环。
   
     手写版本直接计算,避免这些临时对象。
   
     正确性条件也满足:
   
     - equals() 比较 tabletId 和 clusterId;
     - hashCode() 使用相同两个字段;
     - 两个字段都是 final,放进 ConcurrentHashMap 后 hash 不会变化;
     - 不同 key 即便偶然 hash 冲突,CHM 仍会继续调用 equals() 区分,不会互相覆盖。OpenJDK CHM 查找代码 
(https://github.com/openjdk/jdk17u/blob/master/src/java.base/share/classes/java/util/concurrent/ConcurrentHashMap.java#L1006-L1035)



-- 
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