【发布时间】:2016-01-07 00:44:05
【问题描述】:
我想使用字节数组作为concurentDictionary 中的查找键。
目前我通过使用自定义 EqualityComparer<byte[]> 来解决这个问题。
这很好用,但我确实意识到我的哈希码生成器会产生很多重叠,最终会出现在同一个哈希桶中。
public class ByteArrayEqualityComparer : EqualityComparer<byte[]>
{
public override bool Equals(byte[] x, byte[] y)
{
//fast buffer compare
return UnsafeCompare(x, y);
}
public override int GetHashCode(byte[] obj)
{
int hash = 0;
for (int i = 0; i < obj.Length; i += 2)
{
hash += obj[i]; //xor? shift? black magic?
}
return hash;
}
}
什么是从字节数组创建相对快速的哈希的好公式?
我的想法是,我可以通过跳过每 x 个字节来计算哈希码以提高速度。 由于最终的比较仍然是在整个数据集上完成的,所以多次比较所有字节似乎没有意义。
我想一些 xor 魔法和对 hash var 的转移会让事情变得更好。
这对性能至关重要,因此也欢迎任何可以使用的快捷方式。
[编辑] 我最终使用了这个解决方案。 我使用一个结构来包装字节数组,这样我就可以为它使用缓存的哈希码,而不是为每次比较计算它。 这带来了非常好的性能提升。
public struct ByteArrayKey
{
public readonly byte[] Bytes;
private readonly int _hashCode;
public override bool Equals(object obj)
{
var other = (ByteArrayKey) obj;
return Compare(Bytes, other.Bytes);
}
public override int GetHashCode()
{
return _hashCode;
}
private static int GetHashCode([NotNull] byte[] bytes)
{
unchecked
{
var hash = 17;
for (var i = 0; i < bytes.Length; i++)
{
hash = hash*23 + bytes[i];
}
return hash;
}
}
public ByteArrayKey(byte[] bytes)
{
Bytes = bytes;
_hashCode = GetHashCode(bytes);
}
public static ByteArrayKey Create(byte[] bytes)
{
return new ByteArrayKey(bytes);
}
public static unsafe bool Compare(byte[] a1, byte[] a2)
{
if (a1 == null || a2 == null || a1.Length != a2.Length)
return false;
fixed (byte* p1 = a1, p2 = a2)
{
byte* x1 = p1, x2 = p2;
var l = a1.Length;
for (var i = 0; i < l/8; i++, x1 += 8, x2 += 8)
if (*(long*) x1 != *(long*) x2) return false;
if ((l & 4) != 0)
{
if (*(int*) x1 != *(int*) x2) return false;
x1 += 4;
x2 += 4;
}
if ((l & 2) != 0)
{
if (*(short*) x1 != *(short*) x2) return false;
x1 += 2;
x2 += 2;
}
if ((l & 1) != 0) if (*x1 != *x2) return false;
return true;
}
}
}
【问题讨论】:
-
标准模式是例如here。如果这对您有用,我将关闭此问题作为重复。
-
这正是我所需要的,谢谢!
-
我真的不明白这是怎么重复的,因为问题的核心是字节数组的比较
标签: c# dictionary equality