Skip to content

armgit / godot

Public Imported from GitHub
0

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

Out-line the `configured` check of `~StringName()`, to save binary size.

Lukas Tenbrink04f132dbc5b2
diff --git a/core/string/string_name.cpp b/core/string/string_name.cpp
index b5be87e90cb..fb11f514d83 100644
--- a/core/string/string_name.cpp
+++ b/core/string/string_name.cpp
@@ -43,6 +43,7 @@ struct StringName::Table {
static _Data *table[TABLE_LEN];
static BinaryMutex mutex;
static PagedAllocator<_Data> allocator;
+ static bool configured;
};
// Defined out-of-class instead of using static inline so the compiler can
@@ -52,13 +53,14 @@ struct StringName::Table {
StringName::_Data *StringName::Table::table[TABLE_LEN];
BinaryMutex StringName::Table::mutex;
PagedAllocator<StringName::_Data> StringName::Table::allocator;
+bool StringName::Table::configured = false;
void StringName::setup() {
- ERR_FAIL_COND(configured);
+ ERR_FAIL_COND(Table::configured);
for (uint32_t i = 0; i < Table::TABLE_LEN; i++) {
Table::table[i] = nullptr;
}
- configured = true;
+ Table::configured = true;
}
void StringName::cleanup() {
@@ -113,11 +115,16 @@ void StringName::cleanup() {
if (lost_strings) {
print_verbose(vformat("StringName: %d unclaimed string names at exit.", lost_strings));
}
- configured = false;
+ Table::configured = false;
}
void StringName::unref() {
- ERR_FAIL_COND(!configured);
+ if (unlikely(!Table::configured)) {
+ // Most likely tearing down. It's possible someone is genuinely
+ // misusing StringName, but they'll hopefully know soon enough
+ // without us printing because something down the line breaks.
+ return;
+ }
if (_data && _data->refcount.unref()) {
MutexLock lock(Table::mutex);
@@ -204,7 +211,7 @@ StringName &StringName::operator=(const StringName &p_name) {
StringName::StringName(const StringName &p_name) {
_data = nullptr;
- ERR_FAIL_COND(!configured);
+ ERR_FAIL_COND(!Table::configured);
if (p_name._data && p_name._data->refcount.ref()) {
_data = p_name._data;
@@ -214,7 +221,7 @@ StringName::StringName(const StringName &p_name) {
StringName::StringName(const char *p_name, bool p_static) {
_data = nullptr;
- ERR_FAIL_COND(!configured);
+ ERR_FAIL_COND(!Table::configured);
if (!p_name || p_name[0] == 0) {
return; //empty, ignore
@@ -271,7 +278,7 @@ StringName::StringName(const char *p_name, bool p_static) {
StringName::StringName(const String &p_name, bool p_static) {
_data = nullptr;
- ERR_FAIL_COND(!configured);
+ ERR_FAIL_COND(!Table::configured);
if (p_name.is_empty()) {
return;
diff --git a/core/string/string_name.h b/core/string/string_name.h
index f2555664d62..1f86ee8ab2e 100644
--- a/core/string/string_name.h
+++ b/core/string/string_name.h
@@ -71,7 +71,6 @@ class [[nodiscard]] _WARN_UNUSED_ StringName {
static void setup();
static void cleanup();
static uint32_t get_empty_hash();
- static inline bool configured = false;
#ifdef DEBUG_ENABLED
struct DebugSortReferences {
bool operator()(const _Data *p_left, const _Data *p_right) const {
@@ -188,7 +187,8 @@ public:
_FORCE_INLINE_
#endif
~StringName() {
- if (likely(configured) && _data) { //only free if configured
+ // Using `likely` here leads to huge binary size benefits.
+ if (likely(_data)) {
unref();
}
}