Skip to content

Commit 931820d

Browse files
committed
Merge pull request #105278 from Ivorforce/reserve-smoke-test
Smoke test: Log an error if `reserve()` is called with fewer elements than `size()`
2 parents c00cee3 + 7c37188 commit 931820d

File tree

7 files changed

+16
-13
lines changed

7 files changed

+16
-13
lines changed

core/math/a_star.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,6 @@ int64_t AStar3D::get_point_capacity() const {
260260

261261
void AStar3D::reserve_space(int64_t p_num_nodes) {
262262
ERR_FAIL_COND_MSG(p_num_nodes <= 0, vformat("New capacity must be greater than 0, new was: %d.", p_num_nodes));
263-
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));
264263
points.reserve(p_num_nodes);
265264
}
266265

core/string/string_buffer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::append(const c
117117

118118
template <int SHORT_BUFFER_SIZE>
119119
StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::reserve(int p_size) {
120-
if (p_size < SHORT_BUFFER_SIZE || p_size < buffer.size() || !p_size) {
120+
ERR_FAIL_COND_V_MSG(p_size < length(), *this, "reserve() called with a capacity smaller than the current size. This is likely a mistake.");
121+
if (p_size <= SHORT_BUFFER_SIZE || p_size <= buffer.size()) {
121122
return *this;
122123
}
123124

core/templates/a_hash_map.h

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -414,12 +414,15 @@ class AHashMap {
414414
// Reserves space for a number of elements, useful to avoid many resizes and rehashes.
415415
// If adding a known (possibly large) number of elements at once, must be larger than old capacity.
416416
void reserve(uint32_t p_new_capacity) {
417-
ERR_FAIL_COND_MSG(p_new_capacity < get_capacity(), "It is impossible to reserve less capacity than is currently available.");
417+
ERR_FAIL_COND_MSG(p_new_capacity < size(), "reserve() called with a capacity smaller than the current size. This is likely a mistake.");
418418
if (elements == nullptr) {
419419
capacity = MAX(4u, p_new_capacity);
420420
capacity = next_power_of_2(capacity) - 1;
421421
return; // Unallocated yet.
422422
}
423+
if (p_new_capacity <= get_capacity()) {
424+
return;
425+
}
423426
_resize_and_rehash(p_new_capacity);
424427
}
425428

@@ -665,9 +668,7 @@ class AHashMap {
665668
}
666669

667670
AHashMap(const HashMap<TKey, TValue> &p_other) {
668-
if (p_other.size() > get_capacity()) {
669-
reserve(p_other.size());
670-
}
671+
reserve(p_other.size());
671672
for (const KeyValue<TKey, TValue> &E : p_other) {
672673
uint32_t hash = _hash(E.key);
673674
_insert_element(E.key, E.value, hash);
@@ -686,9 +687,7 @@ class AHashMap {
686687

687688
void operator=(const HashMap<TKey, TValue> &p_other) {
688689
reset();
689-
if (p_other.size() > get_capacity()) {
690-
reserve(p_other.size());
691-
}
690+
reserve(p_other.size());
692691
for (const KeyValue<TKey, TValue> &E : p_other) {
693692
uint32_t hash = _hash(E.key);
694693
_insert_element(E.key, E.value, hash);
@@ -705,9 +704,7 @@ class AHashMap {
705704
}
706705

707706
AHashMap(std::initializer_list<KeyValue<TKey, TValue>> p_init) {
708-
if (p_init.size() > get_capacity()) {
709-
reserve(p_init.size());
710-
}
707+
reserve(p_init.size());
711708
for (const KeyValue<TKey, TValue> &E : p_init) {
712709
insert(E.key, E.value);
713710
}

core/templates/hash_map.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ class HashMap {
432432
// Reserves space for a number of elements, useful to avoid many resizes and rehashes.
433433
// If adding a known (possibly large) number of elements at once, must be larger than old capacity.
434434
void reserve(uint32_t p_new_capacity) {
435+
ERR_FAIL_COND_MSG(p_new_capacity < size(), "reserve() called with a capacity smaller than the current size. This is likely a mistake.");
435436
uint32_t new_index = capacity_index;
436437

437438
while (hash_table_size_primes[new_index] < p_new_capacity) {

core/templates/hash_set.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ class HashSet {
294294
// Reserves space for a number of elements, useful to avoid many resizes and rehashes.
295295
// If adding a known (possibly large) number of elements at once, must be larger than old capacity.
296296
void reserve(uint32_t p_new_capacity) {
297+
ERR_FAIL_COND_MSG(p_new_capacity < size(), "reserve() called with a capacity smaller than the current size. This is likely a mistake.");
297298
uint32_t new_index = capacity_index;
298299

299300
while (hash_table_size_primes[new_index] < p_new_capacity) {

core/templates/local_vector.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ class LocalVector {
144144
_FORCE_INLINE_ bool is_empty() const { return count == 0; }
145145
_FORCE_INLINE_ U get_capacity() const { return capacity; }
146146
void reserve(U p_size) {
147+
ERR_FAIL_COND_MSG(p_size < size(), "reserve() called with a capacity smaller than the current size. This is likely a mistake.");
147148
if (p_size > capacity) {
148149
if (tight) {
149150
capacity = p_size;

core/templates/oa_hash_map.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,10 @@ class OAHashMap {
301301
* capacity.
302302
**/
303303
void reserve(uint32_t p_new_capacity) {
304-
ERR_FAIL_COND(p_new_capacity < capacity);
304+
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.");
305+
if (p_new_capacity <= capacity) {
306+
return;
307+
}
305308
_resize_and_rehash(p_new_capacity);
306309
}
307310

0 commit comments

Comments
 (0)