Cut down memory usage for tables in XORFastHash by factor of four

`pow(2, 16)` is not `2 << 16` but rather `1 << 16`.

With this change we cut memory usage in half for the XORFastHash's two
tables. Adapts XORFastHashStorage, memory usage reduction by factor two.
This commit is contained in:
Daniel J. Hofmann 2016-01-21 16:30:46 +01:00
parent 60ef6070b0
commit 1417d43430
2 changed files with 14 additions and 8 deletions

View File

@ -37,8 +37,8 @@ namespace util
*/ */
class XORFastHash class XORFastHash
{ // 65k entries { // 65k entries
std::array<std::uint16_t, (2u << 16u)> table1; std::array<std::uint16_t, (1u << 16u)> table1;
std::array<std::uint16_t, (2u << 16u)> table2; std::array<std::uint16_t, (1u << 16u)> table2;
public: public:
XORFastHash() XORFastHash()

View File

@ -34,29 +34,35 @@ template <typename NodeID, typename Key> class XORFastHashStorage
XORFastHashStorage() = delete; XORFastHashStorage() = delete;
explicit XORFastHashStorage(size_t) : positions(2 << 16), current_timestamp(0) {} explicit XORFastHashStorage(size_t) : positions(1u << 16u), current_timestamp(0) {}
HashCell &operator[](const NodeID node) HashCell &operator[](const NodeID node)
{ {
unsigned short position = fast_hasher(node); std::uint16_t position = fast_hasher(node);
while ((positions[position].time == current_timestamp) && (positions[position].id != node)) while ((positions[position].time == current_timestamp) && (positions[position].id != node))
{ {
++position %= (2 << 16); ++position %= (1u << 16u);
} }
positions[position].time = current_timestamp; positions[position].time = current_timestamp;
positions[position].id = node; positions[position].id = node;
BOOST_ASSERT(position < positions.size());
return positions[position]; return positions[position];
} }
// peek into table, get key for node, think of it as a read-only operator[] // peek into table, get key for node, think of it as a read-only operator[]
Key peek_index(const NodeID node) const Key peek_index(const NodeID node) const
{ {
unsigned short position = fast_hasher(node); std::uint16_t position = fast_hasher(node);
while ((positions[position].time == current_timestamp) && (positions[position].id != node)) while ((positions[position].time == current_timestamp) && (positions[position].id != node))
{ {
++position %= (2 << 16); ++position %= (1u << 16u);
} }
BOOST_ASSERT(position < positions.size());
return positions[position].key; return positions[position].key;
} }
@ -66,7 +72,7 @@ template <typename NodeID, typename Key> class XORFastHashStorage
if (std::numeric_limits<unsigned>::max() == current_timestamp) if (std::numeric_limits<unsigned>::max() == current_timestamp)
{ {
positions.clear(); positions.clear();
positions.resize(2 << 16); positions.resize(1u << 16u);
} }
} }