Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

Core: Optimize HashMap lookup, erase, insert

aurpine6b76476b96d4
diff --git a/core/templates/hash_map.h b/core/templates/hash_map.h
index 40b060d853b..6f9d6612620 100644
--- a/core/templates/hash_map.h
+++ b/core/templates/hash_map.h
@@ -95,11 +95,12 @@ private:
}
}
- static _FORCE_INLINE_ uint32_t _get_probe_length(const uint32_t p_idx, const uint32_t p_hash, const uint32_t p_capacity, const uint64_t p_capacity_inv) {
- const uint32_t original_idx = fastmod(p_hash, p_capacity_inv, p_capacity);
- const uint32_t distance_idx = p_idx - original_idx + p_capacity;
- // At most p_capacity over 0, so we can use an if (faster than fastmod).
- return distance_idx >= p_capacity ? distance_idx - p_capacity : distance_idx;
+ // Returns true when the probe length from `a` to `idx` is LESS THAN the probe length from `b` to `idx`.
+ _FORCE_INLINE_ static constexpr bool _probe_length_cmp(const uint32_t p_a, const uint32_t p_b, const uint32_t p_idx) {
+ if (unlikely(p_idx < p_b)) {
+ return likely(p_idx >= p_a) || p_b < p_a;
+ }
+ return p_b < p_a && likely(p_idx >= p_a);
}
bool _lookup_idx(const TKey &p_key, uint32_t &r_idx) const {
@@ -110,56 +111,70 @@ private:
bool _lookup_idx_unchecked(const TKey &p_key, uint32_t p_hash, uint32_t &r_idx) const {
const uint32_t capacity = hash_table_size_primes[_capacity_idx];
const uint64_t capacity_inv = hash_table_size_primes_inv[_capacity_idx];
- uint32_t idx = fastmod(p_hash, capacity_inv, capacity);
- uint32_t distance = 0;
+ const uint32_t start_idx = fastmod(p_hash, capacity_inv, capacity);
+ uint32_t idx = start_idx;
+ if (_hashes[idx] == EMPTY_HASH) {
+ return false;
+ }
+ if (_hashes[idx] == p_hash && Comparator::compare(_elements[idx]->data.key, p_key)) {
+ r_idx = idx;
+ return true;
+ }
while (true) {
+ _increment_mod(idx, capacity);
if (_hashes[idx] == EMPTY_HASH) {
return false;
}
-
- if (distance > _get_probe_length(idx, _hashes[idx], capacity, capacity_inv)) {
- return false;
- }
-
- if (_hashes[idx] == p_hash && Comparator::compare(_elements[idx]->data.key, p_key)) {
+ if (_hashes[idx] != p_hash) {
+ // Stop search if we probed further than this element.
+ if (_probe_length_cmp(fastmod(_hashes[idx], capacity_inv, capacity), start_idx, idx)) {
+ return false;
+ }
+ } else if (Comparator::compare(_elements[idx]->data.key, p_key)) {
r_idx = idx;
return true;
}
-
- _increment_mod(idx, capacity);
- distance++;
}
}
+ // Small helper for _insert_element
+ _FORCE_INLINE_ void _insert_empty_at(const uint32_t p_idx, const uint32_t p_hash, HashMapElement<TKey, TValue> *p_value) {
+ _elements[p_idx] = p_value;
+ _hashes[p_idx] = p_hash;
+
+ _size++;
+ }
+
void _insert_element(uint32_t p_hash, HashMapElement<TKey, TValue> *p_value) {
const uint32_t capacity = hash_table_size_primes[_capacity_idx];
const uint64_t capacity_inv = hash_table_size_primes_inv[_capacity_idx];
uint32_t hash = p_hash;
HashMapElement<TKey, TValue> *value = p_value;
- uint32_t distance = 0;
- uint32_t idx = fastmod(hash, capacity_inv, capacity);
- while (true) {
- if (_hashes[idx] == EMPTY_HASH) {
- _elements[idx] = value;
- _hashes[idx] = hash;
+ uint32_t start_idx = fastmod(hash, capacity_inv, capacity);
+ uint32_t idx = start_idx;
- _size++;
+ if (_hashes[idx] == EMPTY_HASH) {
+ _insert_empty_at(idx, hash, value);
+ return;
+ }
+ // Keep going until empty slot found.
+ while (true) {
+ _increment_mod(idx, capacity);
+ if (_hashes[idx] == EMPTY_HASH) {
+ _insert_empty_at(idx, hash, value);
return;
}
// Not an empty slot, let's check the probing length of the existing one.
- uint32_t existing_probe_len = _get_probe_length(idx, _hashes[idx], capacity, capacity_inv);
- if (existing_probe_len < distance) {
+ uint32_t new_start_idx = fastmod(_hashes[idx], capacity_inv, capacity);
+ if (_probe_length_cmp(new_start_idx, start_idx, idx)) {
SWAP(hash, _hashes[idx]);
SWAP(value, _elements[idx]);
- distance = existing_probe_len;
+ start_idx = new_start_idx;
}
-
- _increment_mod(idx, capacity);
- distance++;
}
}
@@ -327,8 +342,10 @@ public:
const uint32_t capacity = hash_table_size_primes[_capacity_idx];
const uint64_t capacity_inv = hash_table_size_primes_inv[_capacity_idx];
- uint32_t next_idx = fastmod((idx + 1), capacity_inv, capacity);
- while (_hashes[next_idx] != EMPTY_HASH && _get_probe_length(next_idx, _hashes[next_idx], capacity, capacity_inv) != 0) {
+
+ uint32_t next_idx = idx;
+ _increment_mod(next_idx, capacity);
+ while (_hashes[next_idx] != EMPTY_HASH && next_idx != fastmod(_hashes[next_idx], capacity_inv, capacity)) {
SWAP(_hashes[next_idx], _hashes[idx]);
SWAP(_elements[next_idx], _elements[idx]);
idx = next_idx;
@@ -375,8 +392,10 @@ public:
// Delete the old entries in _hashes and _elements.
const uint32_t capacity = hash_table_size_primes[_capacity_idx];
const uint64_t capacity_inv = hash_table_size_primes_inv[_capacity_idx];
- uint32_t next_idx = fastmod((idx + 1), capacity_inv, capacity);
- while (_hashes[next_idx] != EMPTY_HASH && _get_probe_length(next_idx, _hashes[next_idx], capacity, capacity_inv) != 0) {
+
+ uint32_t next_idx = idx;
+ _increment_mod(next_idx, capacity);
+ while (_hashes[next_idx] != EMPTY_HASH && next_idx != fastmod(_hashes[next_idx], capacity_inv, capacity)) {
SWAP(_hashes[next_idx], _hashes[idx]);
SWAP(_elements[next_idx], _elements[idx]);
idx = next_idx;