1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-05 12:10:55 +00:00

Merge pull request #105760 from Ivorforce/string-name-encapsulate

Remove unnecessary `StringName` `idx` cache in `_Data` to reduce its size.
This commit is contained in:
Thaddeus Crews
2025-04-27 19:21:15 -05:00
2 changed files with 52 additions and 76 deletions

View File

@@ -33,38 +33,31 @@
#include "core/os/os.h" #include "core/os/os.h"
#include "core/string/print_string.h" #include "core/string/print_string.h"
bool StringName::_Data::operator==(const String &p_name) const { struct StringName::Table {
return name == p_name; constexpr static uint32_t TABLE_BITS = 16;
} constexpr static uint32_t TABLE_LEN = 1 << TABLE_BITS;
constexpr static uint32_t TABLE_MASK = TABLE_LEN - 1;
bool StringName::_Data::operator!=(const String &p_name) const { static inline _Data *table[TABLE_LEN];
return !operator==(p_name); static inline Mutex mutex;
} };
bool StringName::_Data::operator==(const char *p_name) const {
return name == p_name;
}
bool StringName::_Data::operator!=(const char *p_name) const {
return !operator==(p_name);
}
void StringName::setup() { void StringName::setup() {
ERR_FAIL_COND(configured); ERR_FAIL_COND(configured);
for (int i = 0; i < STRING_TABLE_LEN; i++) { for (uint32_t i = 0; i < Table::TABLE_LEN; i++) {
_table[i] = nullptr; Table::table[i] = nullptr;
} }
configured = true; configured = true;
} }
void StringName::cleanup() { void StringName::cleanup() {
MutexLock lock(mutex); MutexLock lock(Table::mutex);
#ifdef DEBUG_ENABLED #ifdef DEBUG_ENABLED
if (unlikely(debug_stringname)) { if (unlikely(debug_stringname)) {
Vector<_Data *> data; Vector<_Data *> data;
for (int i = 0; i < STRING_TABLE_LEN; i++) { for (uint32_t i = 0; i < Table::TABLE_LEN; i++) {
_Data *d = _table[i]; _Data *d = Table::table[i];
while (d) { while (d) {
data.push_back(d); data.push_back(d);
d = d->next; d = d->next;
@@ -77,7 +70,7 @@ void StringName::cleanup() {
int unreferenced_stringnames = 0; int unreferenced_stringnames = 0;
int rarely_referenced_stringnames = 0; int rarely_referenced_stringnames = 0;
for (int i = 0; i < data.size(); i++) { for (int i = 0; i < data.size(); i++) {
print_line(itos(i + 1) + ": " + data[i]->get_name() + " - " + itos(data[i]->debug_references)); print_line(itos(i + 1) + ": " + data[i]->name + " - " + itos(data[i]->debug_references));
if (data[i]->debug_references == 0) { if (data[i]->debug_references == 0) {
unreferenced_stringnames += 1; unreferenced_stringnames += 1;
} else if (data[i]->debug_references < 5) { } else if (data[i]->debug_references < 5) {
@@ -90,9 +83,9 @@ void StringName::cleanup() {
} }
#endif #endif
int lost_strings = 0; int lost_strings = 0;
for (int i = 0; i < STRING_TABLE_LEN; i++) { for (uint32_t i = 0; i < Table::TABLE_LEN; i++) {
while (_table[i]) { while (Table::table[i]) {
_Data *d = _table[i]; _Data *d = Table::table[i];
if (d->static_count.get() != d->refcount.get()) { if (d->static_count.get() != d->refcount.get()) {
lost_strings++; lost_strings++;
@@ -101,7 +94,7 @@ void StringName::cleanup() {
} }
} }
_table[i] = _table[i]->next; Table::table[i] = Table::table[i]->next;
memdelete(d); memdelete(d);
} }
} }
@@ -115,7 +108,7 @@ void StringName::unref() {
ERR_FAIL_COND(!configured); ERR_FAIL_COND(!configured);
if (_data && _data->refcount.unref()) { if (_data && _data->refcount.unref()) {
MutexLock lock(mutex); MutexLock lock(Table::mutex);
if (CoreGlobals::leak_reporting_enabled && _data->static_count.get() > 0) { if (CoreGlobals::leak_reporting_enabled && _data->static_count.get() > 0) {
ERR_PRINT("BUG: Unreferenced static string to 0: " + _data->name); ERR_PRINT("BUG: Unreferenced static string to 0: " + _data->name);
@@ -123,10 +116,8 @@ void StringName::unref() {
if (_data->prev) { if (_data->prev) {
_data->prev->next = _data->next; _data->prev->next = _data->next;
} else { } else {
if (_table[_data->idx] != _data) { const uint32_t idx = _data->hash & Table::TABLE_MASK;
ERR_PRINT("BUG!"); Table::table[idx] = _data->next;
}
_table[_data->idx] = _data->next;
} }
if (_data->next) { if (_data->next) {
@@ -145,7 +136,7 @@ uint32_t StringName::get_empty_hash() {
bool StringName::operator==(const String &p_name) const { bool StringName::operator==(const String &p_name) const {
if (_data) { if (_data) {
return _data->operator==(p_name); return _data->name == p_name;
} }
return p_name.is_empty(); return p_name.is_empty();
@@ -153,7 +144,7 @@ bool StringName::operator==(const String &p_name) const {
bool StringName::operator==(const char *p_name) const { bool StringName::operator==(const char *p_name) const {
if (_data) { if (_data) {
return _data->operator==(p_name); return _data->name == p_name;
} }
return p_name[0] == 0; return p_name[0] == 0;
@@ -217,7 +208,7 @@ StringName::StringName(const StringName &p_name) {
} }
void StringName::assign_static_unique_class_name(StringName *ptr, const char *p_name) { void StringName::assign_static_unique_class_name(StringName *ptr, const char *p_name) {
MutexLock lock(mutex); MutexLock lock(Table::mutex);
if (*ptr == StringName()) { if (*ptr == StringName()) {
*ptr = StringName(p_name, true); *ptr = StringName(p_name, true);
} }
@@ -233,14 +224,14 @@ StringName::StringName(const char *p_name, bool p_static) {
} }
const uint32_t hash = String::hash(p_name); const uint32_t hash = String::hash(p_name);
const uint32_t idx = hash & STRING_TABLE_MASK; const uint32_t idx = hash & Table::TABLE_MASK;
MutexLock lock(mutex); MutexLock lock(Table::mutex);
_data = _table[idx]; _data = Table::table[idx];
while (_data) { while (_data) {
// compare hash first // compare hash first
if (_data->hash == hash && _data->operator==(p_name)) { if (_data->hash == hash && _data->name == p_name) {
break; break;
} }
_data = _data->next; _data = _data->next;
@@ -264,8 +255,7 @@ StringName::StringName(const char *p_name, bool p_static) {
_data->refcount.init(); _data->refcount.init();
_data->static_count.set(p_static ? 1 : 0); _data->static_count.set(p_static ? 1 : 0);
_data->hash = hash; _data->hash = hash;
_data->idx = idx; _data->next = Table::table[idx];
_data->next = _table[idx];
_data->prev = nullptr; _data->prev = nullptr;
#ifdef DEBUG_ENABLED #ifdef DEBUG_ENABLED
@@ -275,10 +265,10 @@ StringName::StringName(const char *p_name, bool p_static) {
_data->static_count.increment(); _data->static_count.increment();
} }
#endif #endif
if (_table[idx]) { if (Table::table[idx]) {
_table[idx]->prev = _data; Table::table[idx]->prev = _data;
} }
_table[idx] = _data; Table::table[idx] = _data;
} }
StringName::StringName(const String &p_name, bool p_static) { StringName::StringName(const String &p_name, bool p_static) {
@@ -291,13 +281,13 @@ StringName::StringName(const String &p_name, bool p_static) {
} }
const uint32_t hash = p_name.hash(); const uint32_t hash = p_name.hash();
const uint32_t idx = hash & STRING_TABLE_MASK; const uint32_t idx = hash & Table::TABLE_MASK;
MutexLock lock(mutex); MutexLock lock(Table::mutex);
_data = _table[idx]; _data = Table::table[idx];
while (_data) { while (_data) {
if (_data->hash == hash && _data->operator==(p_name)) { if (_data->hash == hash && _data->name == p_name) {
break; break;
} }
_data = _data->next; _data = _data->next;
@@ -321,8 +311,7 @@ StringName::StringName(const String &p_name, bool p_static) {
_data->refcount.init(); _data->refcount.init();
_data->static_count.set(p_static ? 1 : 0); _data->static_count.set(p_static ? 1 : 0);
_data->hash = hash; _data->hash = hash;
_data->idx = idx; _data->next = Table::table[idx];
_data->next = _table[idx];
_data->prev = nullptr; _data->prev = nullptr;
#ifdef DEBUG_ENABLED #ifdef DEBUG_ENABLED
if (unlikely(debug_stringname)) { if (unlikely(debug_stringname)) {
@@ -332,10 +321,10 @@ StringName::StringName(const String &p_name, bool p_static) {
} }
#endif #endif
if (_table[idx]) { if (Table::table[idx]) {
_table[idx]->prev = _data; Table::table[idx]->prev = _data;
} }
_table[idx] = _data; Table::table[idx] = _data;
} }
StringName StringName::search(const char *p_name) { StringName StringName::search(const char *p_name) {
@@ -347,14 +336,14 @@ StringName StringName::search(const char *p_name) {
} }
const uint32_t hash = String::hash(p_name); const uint32_t hash = String::hash(p_name);
const uint32_t idx = hash & STRING_TABLE_MASK; const uint32_t idx = hash & Table::TABLE_MASK;
MutexLock lock(mutex); MutexLock lock(Table::mutex);
_Data *_data = _table[idx]; _Data *_data = Table::table[idx];
while (_data) { while (_data) {
// compare hash first // compare hash first
if (_data->hash == hash && _data->operator==(p_name)) { if (_data->hash == hash && _data->name == p_name) {
break; break;
} }
_data = _data->next; _data = _data->next;
@@ -382,14 +371,14 @@ StringName StringName::search(const char32_t *p_name) {
} }
const uint32_t hash = String::hash(p_name); const uint32_t hash = String::hash(p_name);
const uint32_t idx = hash & STRING_TABLE_MASK; const uint32_t idx = hash & Table::TABLE_MASK;
MutexLock lock(mutex); MutexLock lock(Table::mutex);
_Data *_data = _table[idx]; _Data *_data = Table::table[idx];
while (_data) { while (_data) {
// compare hash first // compare hash first
if (_data->hash == hash && _data->operator==(p_name)) { if (_data->hash == hash && _data->name == p_name) {
break; break;
} }
_data = _data->next; _data = _data->next;
@@ -406,14 +395,14 @@ StringName StringName::search(const String &p_name) {
ERR_FAIL_COND_V(p_name.is_empty(), StringName()); ERR_FAIL_COND_V(p_name.is_empty(), StringName());
const uint32_t hash = p_name.hash(); const uint32_t hash = p_name.hash();
const uint32_t idx = hash & STRING_TABLE_MASK; const uint32_t idx = hash & Table::TABLE_MASK;
MutexLock lock(mutex); MutexLock lock(Table::mutex);
_Data *_data = _table[idx]; _Data *_data = Table::table[idx];
while (_data) { while (_data) {
// compare hash first // compare hash first
if (_data->hash == hash && _data->operator==(p_name)) { if (_data->hash == hash && _data->name == p_name) {
break; break;
} }
_data = _data->next; _data = _data->next;

View File

@@ -39,11 +39,7 @@
class Main; class Main;
class StringName { class StringName {
enum { struct Table;
STRING_TABLE_BITS = 16,
STRING_TABLE_LEN = 1 << STRING_TABLE_BITS,
STRING_TABLE_MASK = STRING_TABLE_LEN - 1
};
struct _Data { struct _Data {
SafeRefCount refcount; SafeRefCount refcount;
@@ -52,28 +48,19 @@ class StringName {
#ifdef DEBUG_ENABLED #ifdef DEBUG_ENABLED
uint32_t debug_references = 0; uint32_t debug_references = 0;
#endif #endif
const String &get_name() const { return name; }
bool operator==(const String &p_name) const;
bool operator!=(const String &p_name) const;
bool operator==(const char *p_name) const;
bool operator!=(const char *p_name) const;
int idx = 0;
uint32_t hash = 0; uint32_t hash = 0;
_Data *prev = nullptr; _Data *prev = nullptr;
_Data *next = nullptr; _Data *next = nullptr;
_Data() {} _Data() {}
}; };
static inline _Data *_table[STRING_TABLE_LEN];
_Data *_data = nullptr; _Data *_data = nullptr;
void unref(); void unref();
friend void register_core_types(); friend void register_core_types();
friend void unregister_core_types(); friend void unregister_core_types();
friend class Main; friend class Main;
static inline Mutex mutex;
static void setup(); static void setup();
static void cleanup(); static void cleanup();
static uint32_t get_empty_hash(); static uint32_t get_empty_hash();