diff options
author | reduz <reduzio@gmail.com> | 2021-07-20 17:01:18 -0300 |
---|---|---|
committer | reduz <reduzio@gmail.com> | 2021-07-20 17:31:51 -0300 |
commit | cee905f04f453dc7823690bc5c7595a1ee7ef2cd (patch) | |
tree | c512ef255ad4a3b69c868dae07704dd06a3339aa | |
parent | 693f9b4e20f76d56ead91f8a8cad204516d2d953 (diff) |
Adds ability to debug stringnames
* References (which include hash tables) can be profiled with --debug-stringnames
-rw-r--r-- | core/string/string_name.cpp | 72 | ||||
-rw-r--r-- | core/string/string_name.h | 18 | ||||
-rw-r--r-- | main/main.cpp | 4 |
3 files changed, 90 insertions, 4 deletions
diff --git a/core/string/string_name.cpp b/core/string/string_name.cpp index 44ebafc643..9024f60dae 100644 --- a/core/string/string_name.cpp +++ b/core/string/string_name.cpp @@ -48,6 +48,10 @@ StringName _scs_create(const char *p_chr, bool p_static) { bool StringName::configured = false; Mutex StringName::mutex; +#ifdef DEBUG_ENABLED +bool StringName::debug_stringname = false; +#endif + void StringName::setup() { ERR_FAIL_COND(configured); for (int i = 0; i < STRING_TABLE_LEN; i++) { @@ -59,6 +63,23 @@ void StringName::setup() { void StringName::cleanup() { MutexLock lock(mutex); +#ifdef DEBUG_ENABLED + if (unlikely(debug_stringname)) { + Vector<_Data *> data; + for (int i = 0; i < STRING_TABLE_LEN; i++) { + _Data *d = _table[i]; + while (d) { + data.push_back(d); + d = d->next; + } + } + print_line("\nStringName Reference Ranking:\n"); + data.sort_custom<DebugSortReferences>(); + for (int i = 0; i < MIN(100, data.size()); i++) { + print_line(itos(i + 1) + ": " + data[i]->get_name() + " - " + itos(data[i]->debug_references)); + } + } +#endif int lost_strings = 0; for (int i = 0; i < STRING_TABLE_LEN; i++) { while (_table[i]) { @@ -192,8 +213,14 @@ StringName::StringName(const char *p_name, bool p_static) { if (p_static) { _data->static_count.increment(); } - return; +#ifdef DEBUG_ENABLED + if (unlikely(debug_stringname)) { + _data->debug_references++; + } +#endif } + + return; } _data = memnew(_Data); @@ -205,6 +232,13 @@ StringName::StringName(const char *p_name, bool p_static) { _data->cname = nullptr; _data->next = _table[idx]; _data->prev = nullptr; +#ifdef DEBUG_ENABLED + if (unlikely(debug_stringname)) { + // Keep in memory, force static. + _data->refcount.ref(); + _data->static_count.increment(); + } +#endif if (_table[idx]) { _table[idx]->prev = _data; } @@ -240,6 +274,11 @@ StringName::StringName(const StaticCString &p_static_string, bool p_static) { if (p_static) { _data->static_count.increment(); } +#ifdef DEBUG_ENABLED + if (unlikely(debug_stringname)) { + _data->debug_references++; + } +#endif return; } } @@ -253,6 +292,13 @@ StringName::StringName(const StaticCString &p_static_string, bool p_static) { _data->cname = p_static_string.ptr; _data->next = _table[idx]; _data->prev = nullptr; +#ifdef DEBUG_ENABLED + if (unlikely(debug_stringname)) { + // Keep in memory, force static. + _data->refcount.ref(); + _data->static_count.increment(); + } +#endif if (_table[idx]) { _table[idx]->prev = _data; } @@ -288,6 +334,11 @@ StringName::StringName(const String &p_name, bool p_static) { if (p_static) { _data->static_count.increment(); } +#ifdef DEBUG_ENABLED + if (unlikely(debug_stringname)) { + _data->debug_references++; + } +#endif return; } } @@ -301,6 +352,14 @@ StringName::StringName(const String &p_name, bool p_static) { _data->cname = nullptr; _data->next = _table[idx]; _data->prev = nullptr; +#ifdef DEBUG_ENABLED + if (unlikely(debug_stringname)) { + // Keep in memory, force static. + _data->refcount.ref(); + _data->static_count.increment(); + } +#endif + if (_table[idx]) { _table[idx]->prev = _data; } @@ -331,6 +390,12 @@ StringName StringName::search(const char *p_name) { } if (_data && _data->refcount.ref()) { +#ifdef DEBUG_ENABLED + if (unlikely(debug_stringname)) { + _data->debug_references++; + } +#endif + return StringName(_data); } @@ -388,6 +453,11 @@ StringName StringName::search(const String &p_name) { } if (_data && _data->refcount.ref()) { +#ifdef DEBUG_ENABLED + if (unlikely(debug_stringname)) { + _data->debug_references++; + } +#endif return StringName(_data); } diff --git a/core/string/string_name.h b/core/string/string_name.h index f5e7ff509d..ce7988744b 100644 --- a/core/string/string_name.h +++ b/core/string/string_name.h @@ -52,10 +52,11 @@ class StringName { struct _Data { SafeRefCount refcount; SafeNumeric<uint32_t> static_count; - SafeRefCount static_refcount; const char *cname = nullptr; String name; - +#ifdef DEBUG_ENABLED + uint32_t debug_references = 0; +#endif String get_name() const { return cname ? String(cname) : name; } int idx = 0; uint32_t hash = 0; @@ -81,6 +82,15 @@ class StringName { static void setup(); static void cleanup(); static bool configured; +#ifdef DEBUG_ENABLED + struct DebugSortReferences { + bool operator()(const _Data *p_left, const _Data *p_right) const { + return p_left->debug_references > p_right->debug_references; + } + }; + + static bool debug_stringname; +#endif StringName(_Data *p_data) { _data = p_data; } @@ -158,6 +168,10 @@ public: unref(); } } + +#ifdef DEBUG_ENABLED + static void set_debug_stringnames(bool p_enable) { debug_stringname = p_enable; } +#endif }; bool operator==(const String &p_name, const StringName &p_string_name); diff --git a/main/main.cpp b/main/main.cpp index 0651f4b93f..1e5445fd93 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -986,11 +986,13 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph } else if (I->get() == "-d" || I->get() == "--debug") { debug_uri = "local://"; OS::get_singleton()->_debug_stdout = true; -#if defined(DEBUG_ENABLED) && !defined(SERVER_ENABLED) +#if defined(DEBUG_ENABLED) } else if (I->get() == "--debug-collisions") { debug_collisions = true; } else if (I->get() == "--debug-navigation") { debug_navigation = true; + } else if (I->get() == "--debug-stringnames") { + StringName::set_debug_stringnames(true); #endif } else if (I->get() == "--remote-debug") { if (I->next()) { |