[3.4.0][Block] 卡顿检测代码性能优化

This commit is contained in:
renwj
2023-07-31 18:23:40 +08:00
parent eb5e150d30
commit bd1e06aee2
4 changed files with 318 additions and 106 deletions

View File

@@ -11,18 +11,41 @@ public class ObjectHashCodeUtils {
private static final ConcurrentHashMap<String, Integer> hashCodes = new ConcurrentHashMap<>();
private static final StringBuilder keyBuilder = new StringBuilder(128);
public static int getHashCodeIfNeed(Object obj) {
if (obj == null) {
return -1;
public static int getHashCodeIfNeed(Object obj1, Object obj2) {
if (obj1 == null && obj2 == null) {
throw new AssertionError();
}
String name = obj.getClass().getName();
Integer value = hashCodes.get(name);
synchronized (keyBuilder) {
keyBuilder.setLength(0);
boolean flag = false;
if (obj1 != null) {
flag = true;
keyBuilder.append(obj1.hashCode());
}
if (obj2 != null) {
if (flag) {
keyBuilder.append("#");
}
keyBuilder.append(obj2.getClass().getName());
}
}
String key = keyBuilder.toString();
Integer value = hashCodes.get(key);
if (value != null) {
return value;
}
int hashCode = - System.identityHashCode(obj);
hashCodes.put(name, hashCode);
Object obj = obj2 == null ? obj1 : obj2;
int hashCode = -System.identityHashCode(obj);
if (hashCodes.containsValue(hashCode)) {
hashCode = -System.identityHashCode(new Object());
}
hashCodes.put(key, hashCode);
return hashCode;
}
public static int getHashCodeIfNeed(Object obj) {
return getHashCodeIfNeed(null, obj);
}
}