diff --git a/core/math/a_star.cpp b/core/math/a_star.cpp index 9b6717c693d..3e537ae1809 100644 --- a/core/math/a_star.cpp +++ b/core/math/a_star.cpp @@ -260,7 +260,6 @@ int64_t AStar3D::get_point_capacity() const { void AStar3D::reserve_space(int64_t p_num_nodes) { ERR_FAIL_COND_MSG(p_num_nodes <= 0, vformat("New capacity must be greater than 0, new was: %d.", p_num_nodes)); - ERR_FAIL_COND_MSG((uint32_t)p_num_nodes < points.get_capacity(), vformat("New capacity must be greater than current capacity: %d, new was: %d.", points.get_capacity(), p_num_nodes)); points.reserve(p_num_nodes); } diff --git a/core/string/string_buffer.h b/core/string/string_buffer.h index 5a4e66f51ea..6f648b3eae6 100644 --- a/core/string/string_buffer.h +++ b/core/string/string_buffer.h @@ -117,7 +117,8 @@ StringBuffer &StringBuffer::append(const c template StringBuffer &StringBuffer::reserve(int p_size) { - if (p_size < SHORT_BUFFER_SIZE || p_size < buffer.size() || !p_size) { + ERR_FAIL_COND_V_MSG(p_size < length(), *this, "reserve() called with a capacity smaller than the current size. This is likely a mistake."); + if (p_size <= SHORT_BUFFER_SIZE || p_size <= buffer.size()) { return *this; } diff --git a/core/templates/a_hash_map.h b/core/templates/a_hash_map.h index b71db3dc4cf..e603d0aa5f2 100644 --- a/core/templates/a_hash_map.h +++ b/core/templates/a_hash_map.h @@ -414,12 +414,15 @@ public: // Reserves space for a number of elements, useful to avoid many resizes and rehashes. // If adding a known (possibly large) number of elements at once, must be larger than old capacity. void reserve(uint32_t p_new_capacity) { - ERR_FAIL_COND_MSG(p_new_capacity < get_capacity(), "It is impossible to reserve less capacity than is currently available."); + ERR_FAIL_COND_MSG(p_new_capacity < size(), "reserve() called with a capacity smaller than the current size. This is likely a mistake."); if (elements == nullptr) { capacity = MAX(4u, p_new_capacity); capacity = next_power_of_2(capacity) - 1; return; // Unallocated yet. } + if (p_new_capacity <= get_capacity()) { + return; + } _resize_and_rehash(p_new_capacity); } @@ -665,9 +668,7 @@ public: } AHashMap(const HashMap &p_other) { - if (p_other.size() > get_capacity()) { - reserve(p_other.size()); - } + reserve(p_other.size()); for (const KeyValue &E : p_other) { uint32_t hash = _hash(E.key); _insert_element(E.key, E.value, hash); @@ -686,9 +687,7 @@ public: void operator=(const HashMap &p_other) { reset(); - if (p_other.size() > get_capacity()) { - reserve(p_other.size()); - } + reserve(p_other.size()); for (const KeyValue &E : p_other) { uint32_t hash = _hash(E.key); _insert_element(E.key, E.value, hash); @@ -705,9 +704,7 @@ public: } AHashMap(std::initializer_list> p_init) { - if (p_init.size() > get_capacity()) { - reserve(p_init.size()); - } + reserve(p_init.size()); for (const KeyValue &E : p_init) { insert(E.key, E.value); } diff --git a/core/templates/hash_map.h b/core/templates/hash_map.h index 4dc6baf47b1..eaf58594875 100644 --- a/core/templates/hash_map.h +++ b/core/templates/hash_map.h @@ -432,6 +432,7 @@ public: // Reserves space for a number of elements, useful to avoid many resizes and rehashes. // If adding a known (possibly large) number of elements at once, must be larger than old capacity. void reserve(uint32_t p_new_capacity) { + ERR_FAIL_COND_MSG(p_new_capacity < size(), "reserve() called with a capacity smaller than the current size. This is likely a mistake."); uint32_t new_index = capacity_index; while (hash_table_size_primes[new_index] < p_new_capacity) { diff --git a/core/templates/hash_set.h b/core/templates/hash_set.h index 14632a3b88d..b2982f68600 100644 --- a/core/templates/hash_set.h +++ b/core/templates/hash_set.h @@ -294,6 +294,7 @@ public: // Reserves space for a number of elements, useful to avoid many resizes and rehashes. // If adding a known (possibly large) number of elements at once, must be larger than old capacity. void reserve(uint32_t p_new_capacity) { + ERR_FAIL_COND_MSG(p_new_capacity < size(), "reserve() called with a capacity smaller than the current size. This is likely a mistake."); uint32_t new_index = capacity_index; while (hash_table_size_primes[new_index] < p_new_capacity) { diff --git a/core/templates/local_vector.h b/core/templates/local_vector.h index 3983deea886..547006d1915 100644 --- a/core/templates/local_vector.h +++ b/core/templates/local_vector.h @@ -144,6 +144,7 @@ public: _FORCE_INLINE_ bool is_empty() const { return count == 0; } _FORCE_INLINE_ U get_capacity() const { return capacity; } void reserve(U p_size) { + ERR_FAIL_COND_MSG(p_size < size(), "reserve() called with a capacity smaller than the current size. This is likely a mistake."); if (p_size > capacity) { if (tight) { capacity = p_size; diff --git a/core/templates/oa_hash_map.h b/core/templates/oa_hash_map.h index 3aedeb695e4..18487e107fa 100644 --- a/core/templates/oa_hash_map.h +++ b/core/templates/oa_hash_map.h @@ -301,7 +301,10 @@ public: * capacity. **/ void reserve(uint32_t p_new_capacity) { - ERR_FAIL_COND(p_new_capacity < capacity); + ERR_FAIL_COND_MSG(p_new_capacity < get_num_elements(), "reserve() called with a capacity smaller than the current size. This is likely a mistake."); + if (p_new_capacity <= capacity) { + return; + } _resize_and_rehash(p_new_capacity); }