|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @author Michal Sedlak <[email protected]> |
| 4 | + * @brief Efficient hash table implementation |
| 5 | + */ |
| 6 | + |
| 7 | +#define XXH_INLINE_ALL |
| 8 | + |
| 9 | +#include <xmmintrin.h> |
| 10 | + |
| 11 | +#include "hashTable.hpp" |
| 12 | +#include "3rd_party/xxhash/xxhash.h" |
| 13 | + |
| 14 | +static constexpr double EXPAND_WHEN_THIS_FULL = 0.95; |
| 15 | +static constexpr unsigned int EXPAND_WITH_FACTOR_OF = 2; |
| 16 | +static constexpr uint8_t EMPTY_BIT = 0x80; |
| 17 | + |
| 18 | +HashTable::HashTable(std::size_t key_size, std::size_t value_size) : |
| 19 | + m_key_size(key_size), m_value_size(value_size) |
| 20 | +{ |
| 21 | + init_blocks(); |
| 22 | +} |
| 23 | + |
| 24 | +void |
| 25 | +HashTable::init_blocks() |
| 26 | +{ |
| 27 | + HashTableBlock zeroed_block; |
| 28 | + |
| 29 | + memset(&zeroed_block, 0, sizeof(zeroed_block)); |
| 30 | + for (int i = 0; i < 16; i++) { |
| 31 | + zeroed_block.tags[i] |= EMPTY_BIT; // Indicate that the spot is empty |
| 32 | + } |
| 33 | + |
| 34 | + m_blocks.resize(m_block_count); |
| 35 | + for (auto &block : m_blocks) { |
| 36 | + block = zeroed_block; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +bool |
| 41 | +HashTable::lookup(uint8_t *key, uint8_t *&item, bool create_if_not_found) |
| 42 | +{ |
| 43 | + uint64_t hash = XXH3_64bits(key, m_key_size); // The hash of the key |
| 44 | + uint64_t index = (hash >> 7) & (m_block_count - 1); // The starting block index |
| 45 | + |
| 46 | + for (;;) { |
| 47 | + HashTableBlock &block = m_blocks[index]; |
| 48 | + |
| 49 | + uint8_t item_tag = (hash & 0xFF) & ~EMPTY_BIT; // Get item tag from part of the hash with the empty bit cleared |
| 50 | + auto block_tags = _mm_load_si128(reinterpret_cast<__m128i *>(block.tags)); // Load the current block metadata (16B) |
| 51 | + auto hash_mask = _mm_set1_epi8(item_tag); // Repeat the item tag 16 times |
| 52 | + auto empty_mask = _mm_set1_epi8(EMPTY_BIT); // Repeat the empty tag 16 times |
| 53 | + |
| 54 | + auto hash_match = _mm_movemask_epi8(_mm_cmpeq_epi8(block_tags, hash_mask)); // Check if any of the metadata matched our item tag |
| 55 | + auto empty_match = _mm_movemask_epi8(_mm_cmpeq_epi8(block_tags, empty_mask)); // Check if any of the metadata matched an empty spot |
| 56 | + |
| 57 | + int item_index = 0; |
| 58 | + |
| 59 | + while (hash_match) { // While there are any set bits indicating that the tag of the item we're looking for matched |
| 60 | + auto one_index = __builtin_ctz(hash_match); // Get index of the set bit, i.e. the index of the item in the block |
| 61 | + item_index += one_index; |
| 62 | + |
| 63 | + uint8_t *record = block.items[item_index]; // The record whose item tag matched |
| 64 | + if (memcmp(record, key, m_key_size) == 0) { // Does the key match as well or was it just a hash collision? |
| 65 | + item = record; |
| 66 | + return true; // We found the item |
| 67 | + } |
| 68 | + |
| 69 | + // Move on to the next set bit |
| 70 | + hash_match >>= one_index + 1; |
| 71 | + item_index += 1; |
| 72 | + } |
| 73 | + |
| 74 | + // If we got here we didn't match, but we found an empty spot in the block which |
| 75 | + // indicates that we're done with the search. The item cannot be in the next block |
| 76 | + // if the current block contains an empty spot |
| 77 | + if (empty_match) { |
| 78 | + |
| 79 | + if (!create_if_not_found) { |
| 80 | + // If we're just looking for the item and we haven't found it, we're done |
| 81 | + return false; |
| 82 | + } |
| 83 | + |
| 84 | + // Create a new record |
| 85 | + auto empty_index = __builtin_ctz(empty_match); |
| 86 | + block.tags[empty_index] = item_tag; |
| 87 | + |
| 88 | + uint8_t *record = m_allocator.allocate(m_key_size + m_value_size); |
| 89 | + block.items[empty_index] = record; |
| 90 | + m_items.push_back(record); |
| 91 | + m_record_count++; |
| 92 | + |
| 93 | + memcpy(record, key, m_key_size); // Copy the key, leave the value part uninitialized |
| 94 | + item = record; |
| 95 | + |
| 96 | + // If the hash table has reached a specified percentage of fullness, expand the hash table |
| 97 | + if (double(m_record_count) / (16 * double(m_block_count)) >= EXPAND_WHEN_THIS_FULL) { |
| 98 | + expand(); |
| 99 | + } |
| 100 | + |
| 101 | + return false; |
| 102 | + } |
| 103 | + |
| 104 | + index = (index + 1) & (m_block_count - 1); // Move on to the next block |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +void |
| 109 | +HashTable::expand() |
| 110 | +{ |
| 111 | + // Grow the amount of blocks by a specified factor |
| 112 | + m_block_count *= EXPAND_WITH_FACTOR_OF; |
| 113 | + |
| 114 | + // Reinitialize the blocks |
| 115 | + init_blocks(); |
| 116 | + |
| 117 | + // Reassign all the items to the newly initialized blocks |
| 118 | + for (uint8_t *item : m_items) { |
| 119 | + uint64_t hash = XXH3_64bits(item, m_key_size); |
| 120 | + uint64_t index = (hash >> 7) & (m_block_count - 1); |
| 121 | + uint8_t item_tag = (hash & 0xFF) & ~EMPTY_BIT; |
| 122 | + |
| 123 | + // Find a spot for the item and insert it |
| 124 | + for (;;) { |
| 125 | + HashTableBlock &block = m_blocks[index]; |
| 126 | + |
| 127 | + auto block_tags = _mm_load_si128(reinterpret_cast<__m128i *>(block.tags)); |
| 128 | + auto empty_mask = _mm_set1_epi8(EMPTY_BIT); |
| 129 | + auto empty_match = _mm_movemask_epi8(_mm_cmpeq_epi8(block_tags, empty_mask)); |
| 130 | + if (empty_match) { // Does this black have an empty spot for our item? |
| 131 | + auto empty_index = __builtin_ctz(empty_match); |
| 132 | + block.tags[empty_index] = item_tag; |
| 133 | + block.items[empty_index] = item; |
| 134 | + break; |
| 135 | + } |
| 136 | + |
| 137 | + index = (index + 1) & (m_block_count - 1); |
| 138 | + } |
| 139 | + |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +bool |
| 144 | +HashTable::find(uint8_t *key, uint8_t *&item) |
| 145 | +{ |
| 146 | + return lookup(key, item, false); |
| 147 | +} |
| 148 | + |
| 149 | +bool |
| 150 | +HashTable::find_or_create(uint8_t *key, uint8_t *&item) |
| 151 | +{ |
| 152 | + return lookup(key, item, true); |
| 153 | +} |
0 commit comments