提交 c62889dc authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Add some protection to ValueHashMap against hashes with the same less significant bits

上级 5365e664
...@@ -54,7 +54,12 @@ public class ValueHashMap<V> extends HashBase { ...@@ -54,7 +54,12 @@ public class ValueHashMap<V> extends HashBase {
} }
private int getIndex(Value key) { private int getIndex(Value key) {
return key.hashCode() & mask; int h = key.hashCode();
/*
* Add some protection against hashes with the same less significant bits
* (ValueDouble with integer values, for example).
*/
return (h ^ h >>> 16) & mask;
} }
/** /**
......
...@@ -133,8 +133,12 @@ public class ValueDouble extends Value { ...@@ -133,8 +133,12 @@ public class ValueDouble extends Value {
@Override @Override
public int hashCode() { public int hashCode() {
long hash = Double.doubleToLongBits(value); /*
return (int) (hash ^ (hash >> 32)); * NaNs are normalized in get() method, so it's safe to use
* doubleToRawLongBits() instead of doubleToLongBits() here.
*/
long hash = Double.doubleToRawLongBits(value);
return (int) (hash ^ (hash >>> 32));
} }
@Override @Override
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论