Change "reserve called with a capacity smaller than the current size" error message to a verbose message.

(cherry picked from commit c6f57c7a55)
This commit is contained in:
Lukas Tenbrink 2025-09-23 17:47:28 +02:00 committed by Thaddeus Crews
parent fb9d73438d
commit 28f4d5d6a7
No known key found for this signature in database
GPG key ID: 8C6E5FEB5FC03CCC
5 changed files with 18 additions and 5 deletions

View file

@ -30,6 +30,7 @@
#pragma once
#include "core/string/print_string.h"
#include "core/string/ustring.h"
template <int SHORT_BUFFER_SIZE = 64>
@ -117,8 +118,10 @@ 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) {
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()) {
if (p_size < length()) {
WARN_VERBOSE("reserve() called with a capacity smaller than the current size. This is likely a mistake.");
}
return *this;
}

View file

@ -410,13 +410,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 < 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()) {
if (p_new_capacity < size()) {
WARN_VERBOSE("reserve() called with a capacity smaller than the current size. This is likely a mistake.");
}
return;
}
_resize_and_rehash(p_new_capacity);

View file

@ -31,6 +31,7 @@
#pragma once
#include "core/os/memory.h"
#include "core/string/print_string.h"
#include "core/templates/hashfuncs.h"
#include "core/templates/pair.h"
#include "core/templates/sort_list.h"
@ -398,7 +399,6 @@ 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) {
@ -407,6 +407,9 @@ public:
}
if (new_index == capacity_index) {
if (p_new_capacity < size()) {
WARN_VERBOSE("reserve() called with a capacity smaller than the current size. This is likely a mistake.");
}
return;
}

View file

@ -31,6 +31,7 @@
#pragma once
#include "core/os/memory.h"
#include "core/string/print_string.h"
#include "core/templates/hashfuncs.h"
/**
@ -298,7 +299,6 @@ 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) {
@ -307,6 +307,9 @@ public:
}
if (new_index == capacity_index) {
if (p_new_capacity < size()) {
WARN_VERBOSE("reserve() called with a capacity smaller than the current size. This is likely a mistake.");
}
return;
}

View file

@ -32,6 +32,7 @@
#include "core/error/error_macros.h"
#include "core/os/memory.h"
#include "core/string/print_string.h"
#include "core/templates/sort_array.h"
#include "core/templates/vector.h"
@ -161,7 +162,6 @@ 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;
@ -173,6 +173,8 @@ public:
}
data = (T *)memrealloc(data, capacity * sizeof(T));
CRASH_COND_MSG(!data, "Out of memory");
} else if (p_size < count) {
WARN_VERBOSE("reserve() called with a capacity smaller than the current size. This is likely a mistake.");
}
}