Browse Source

Merge pull request #50663 from reduz/debug-stringnames

Rémi Verschelde 4 years ago
parent
commit
b5e5eed7ae
3 changed files with 90 additions and 4 deletions
  1. 71 1
      core/string/string_name.cpp
  2. 16 2
      core/string/string_name.h
  3. 3 1
      main/main.cpp

+ 71 - 1
core/string/string_name.cpp

@@ -48,6 +48,10 @@ StringName _scs_create(const char *p_chr, bool p_static) {
 bool StringName::configured = false;
 bool StringName::configured = false;
 Mutex StringName::mutex;
 Mutex StringName::mutex;
 
 
+#ifdef DEBUG_ENABLED
+bool StringName::debug_stringname = false;
+#endif
+
 void StringName::setup() {
 void StringName::setup() {
 	ERR_FAIL_COND(configured);
 	ERR_FAIL_COND(configured);
 	for (int i = 0; i < STRING_TABLE_LEN; i++) {
 	for (int i = 0; i < STRING_TABLE_LEN; i++) {
@@ -59,6 +63,23 @@ void StringName::setup() {
 void StringName::cleanup() {
 void StringName::cleanup() {
 	MutexLock lock(mutex);
 	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;
 	int lost_strings = 0;
 	for (int i = 0; i < STRING_TABLE_LEN; i++) {
 	for (int i = 0; i < STRING_TABLE_LEN; i++) {
 		while (_table[i]) {
 		while (_table[i]) {
@@ -192,8 +213,14 @@ StringName::StringName(const char *p_name, bool p_static) {
 			if (p_static) {
 			if (p_static) {
 				_data->static_count.increment();
 				_data->static_count.increment();
 			}
 			}
-			return;
+#ifdef DEBUG_ENABLED
+			if (unlikely(debug_stringname)) {
+				_data->debug_references++;
+			}
+#endif
 		}
 		}
+
+		return;
 	}
 	}
 
 
 	_data = memnew(_Data);
 	_data = memnew(_Data);
@@ -205,6 +232,13 @@ StringName::StringName(const char *p_name, bool p_static) {
 	_data->cname = nullptr;
 	_data->cname = nullptr;
 	_data->next = _table[idx];
 	_data->next = _table[idx];
 	_data->prev = nullptr;
 	_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]) {
 	if (_table[idx]) {
 		_table[idx]->prev = _data;
 		_table[idx]->prev = _data;
 	}
 	}
@@ -240,6 +274,11 @@ StringName::StringName(const StaticCString &p_static_string, bool p_static) {
 			if (p_static) {
 			if (p_static) {
 				_data->static_count.increment();
 				_data->static_count.increment();
 			}
 			}
+#ifdef DEBUG_ENABLED
+			if (unlikely(debug_stringname)) {
+				_data->debug_references++;
+			}
+#endif
 			return;
 			return;
 		}
 		}
 	}
 	}
@@ -253,6 +292,13 @@ StringName::StringName(const StaticCString &p_static_string, bool p_static) {
 	_data->cname = p_static_string.ptr;
 	_data->cname = p_static_string.ptr;
 	_data->next = _table[idx];
 	_data->next = _table[idx];
 	_data->prev = nullptr;
 	_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]) {
 	if (_table[idx]) {
 		_table[idx]->prev = _data;
 		_table[idx]->prev = _data;
 	}
 	}
@@ -288,6 +334,11 @@ StringName::StringName(const String &p_name, bool p_static) {
 			if (p_static) {
 			if (p_static) {
 				_data->static_count.increment();
 				_data->static_count.increment();
 			}
 			}
+#ifdef DEBUG_ENABLED
+			if (unlikely(debug_stringname)) {
+				_data->debug_references++;
+			}
+#endif
 			return;
 			return;
 		}
 		}
 	}
 	}
@@ -301,6 +352,14 @@ StringName::StringName(const String &p_name, bool p_static) {
 	_data->cname = nullptr;
 	_data->cname = nullptr;
 	_data->next = _table[idx];
 	_data->next = _table[idx];
 	_data->prev = nullptr;
 	_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]) {
 	if (_table[idx]) {
 		_table[idx]->prev = _data;
 		_table[idx]->prev = _data;
 	}
 	}
@@ -331,6 +390,12 @@ StringName StringName::search(const char *p_name) {
 	}
 	}
 
 
 	if (_data && _data->refcount.ref()) {
 	if (_data && _data->refcount.ref()) {
+#ifdef DEBUG_ENABLED
+		if (unlikely(debug_stringname)) {
+			_data->debug_references++;
+		}
+#endif
+
 		return StringName(_data);
 		return StringName(_data);
 	}
 	}
 
 
@@ -388,6 +453,11 @@ StringName StringName::search(const String &p_name) {
 	}
 	}
 
 
 	if (_data && _data->refcount.ref()) {
 	if (_data && _data->refcount.ref()) {
+#ifdef DEBUG_ENABLED
+		if (unlikely(debug_stringname)) {
+			_data->debug_references++;
+		}
+#endif
 		return StringName(_data);
 		return StringName(_data);
 	}
 	}
 
 

+ 16 - 2
core/string/string_name.h

@@ -52,10 +52,11 @@ class StringName {
 	struct _Data {
 	struct _Data {
 		SafeRefCount refcount;
 		SafeRefCount refcount;
 		SafeNumeric<uint32_t> static_count;
 		SafeNumeric<uint32_t> static_count;
-		SafeRefCount static_refcount;
 		const char *cname = nullptr;
 		const char *cname = nullptr;
 		String name;
 		String name;
-
+#ifdef DEBUG_ENABLED
+		uint32_t debug_references = 0;
+#endif
 		String get_name() const { return cname ? String(cname) : name; }
 		String get_name() const { return cname ? String(cname) : name; }
 		int idx = 0;
 		int idx = 0;
 		uint32_t hash = 0;
 		uint32_t hash = 0;
@@ -81,6 +82,15 @@ class StringName {
 	static void setup();
 	static void setup();
 	static void cleanup();
 	static void cleanup();
 	static bool configured;
 	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; }
 	StringName(_Data *p_data) { _data = p_data; }
 
 
@@ -158,6 +168,10 @@ public:
 			unref();
 			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);
 bool operator==(const String &p_name, const StringName &p_string_name);

+ 3 - 1
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") {
 		} else if (I->get() == "-d" || I->get() == "--debug") {
 			debug_uri = "local://";
 			debug_uri = "local://";
 			OS::get_singleton()->_debug_stdout = true;
 			OS::get_singleton()->_debug_stdout = true;
-#if defined(DEBUG_ENABLED) && !defined(SERVER_ENABLED)
+#if defined(DEBUG_ENABLED)
 		} else if (I->get() == "--debug-collisions") {
 		} else if (I->get() == "--debug-collisions") {
 			debug_collisions = true;
 			debug_collisions = true;
 		} else if (I->get() == "--debug-navigation") {
 		} else if (I->get() == "--debug-navigation") {
 			debug_navigation = true;
 			debug_navigation = true;
+		} else if (I->get() == "--debug-stringnames") {
+			StringName::set_debug_stringnames(true);
 #endif
 #endif
 		} else if (I->get() == "--remote-debug") {
 		} else if (I->get() == "--remote-debug") {
 			if (I->next()) {
 			if (I->next()) {