Changeset - r22864:621b56e45870
[Not reviewed]
master
0 1 0
PeterN - 6 years ago 2018-05-19 21:04:25
peter@fuzzle.org
Change [#6689]: Tweak HashTable hash calculation to reduce collisions. (kernigh2) (#6786)
1 file changed with 4 insertions and 6 deletions:
0 comments (0 inline, 0 general)
src/misc/hashtable.hpp
Show inline comments
 
@@ -140,54 +140,52 @@ public:
 
	typedef typename Titem_::Key Tkey;            // make Titem_::Key a property of HashTable
 
	static const int Thash_bits = Thash_bits_;    // publish num of hash bits
 
	static const int Tcapacity = 1 << Thash_bits; // and num of slots 2^bits
 

	
 
protected:
 
	/**
 
	 * each slot contains pointer to the first item in the list,
 
	 *  Titem contains pointer to the next item - GetHashNext(), SetHashNext()
 
	 */
 
	typedef CHashTableSlotT<Titem_> Slot;
 

	
 
	Slot  m_slots[Tcapacity]; // here we store our data (array of blobs)
 
	int   m_num_items;        // item counter
 

	
 
public:
 
	/* default constructor */
 
	inline CHashTableT() : m_num_items(0)
 
	{
 
	}
 

	
 
protected:
 
	/** static helper - return hash for the given key modulo number of slots */
 
	inline static int CalcHash(const Tkey &key)
 
	{
 
		int32 hash = key.CalcHash();
 
		if ((8 * Thash_bits) < 32) hash ^= hash >> (min(8 * Thash_bits, 31));
 
		if ((4 * Thash_bits) < 32) hash ^= hash >> (min(4 * Thash_bits, 31));
 
		if ((2 * Thash_bits) < 32) hash ^= hash >> (min(2 * Thash_bits, 31));
 
		if ((1 * Thash_bits) < 32) hash ^= hash >> (min(1 * Thash_bits, 31));
 
		hash &= (1 << Thash_bits) - 1;
 
		uint32 hash = key.CalcHash();
 
		hash -= (hash >> 17);          // hash * 131071 / 131072
 
		hash -= (hash >> 5);           //   * 31 / 32
 
		hash &= (1 << Thash_bits) - 1; //   modulo slots
 
		return hash;
 
	}
 

	
 
	/** static helper - return hash for the given item modulo number of slots */
 
	inline static int CalcHash(const Titem_ &item)
 
	{
 
		return CalcHash(item.GetKey());
 
	}
 

	
 
public:
 
	/** item count */
 
	inline int Count() const
 
	{
 
		return m_num_items;
 
	}
 

	
 
	/** simple clear - forget all items - used by CSegmentCostCacheT.Flush() */
 
	inline void Clear()
 
	{
 
		for (int i = 0; i < Tcapacity; i++) m_slots[i].Clear();
 
	}
 

	
 
	/** const item search */
 
	const Titem_ *Find(const Tkey &key) const
0 comments (0 inline, 0 general)