Pārlūkot izejas kodu

Merge pull request #105278 from Ivorforce/reserve-smoke-test

Smoke test: Log an error if `reserve()` is called with fewer elements than `size()`
Thaddeus Crews 3 mēneši atpakaļ
vecāks
revīzija
931820d33c

+ 0 - 1
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);
 }
 

+ 2 - 1
core/string/string_buffer.h

@@ -117,7 +117,8 @@ StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::append(const c
 
 template <int SHORT_BUFFER_SIZE>
 StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::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;
 	}
 

+ 7 - 10
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<TKey, TValue> &p_other) {
-		if (p_other.size() > get_capacity()) {
-			reserve(p_other.size());
-		}
+		reserve(p_other.size());
 		for (const KeyValue<TKey, TValue> &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<TKey, TValue> &p_other) {
 		reset();
-		if (p_other.size() > get_capacity()) {
-			reserve(p_other.size());
-		}
+		reserve(p_other.size());
 		for (const KeyValue<TKey, TValue> &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<KeyValue<TKey, TValue>> p_init) {
-		if (p_init.size() > get_capacity()) {
-			reserve(p_init.size());
-		}
+		reserve(p_init.size());
 		for (const KeyValue<TKey, TValue> &E : p_init) {
 			insert(E.key, E.value);
 		}

+ 1 - 0
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) {

+ 1 - 0
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) {

+ 1 - 0
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;

+ 4 - 1
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);
 	}