فهرست منبع

Merge pull request #100335 from kiroxas/Improve`ResourceUID--id_to_text`Performance

Improve `ResourceUID::id_to_text` performance
Thaddeus Crews 8 ماه پیش
والد
کامیت
24a8c05cbd
1فایلهای تغییر یافته به همراه31 افزوده شده و 9 حذف شده
  1. 31 9
      core/io/resource_uid.cpp

+ 31 - 9
core/io/resource_uid.cpp

@@ -45,23 +45,45 @@ String ResourceUID::get_cache_file() {
 	return ProjectSettings::get_singleton()->get_project_data_path().path_join("uid_cache.bin");
 }
 
+static constexpr uint8_t uuid_characters[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', '0', '1', '2', '3', '4', '5', '6', '7', '8' };
+static constexpr uint32_t uuid_characters_element_count = (sizeof(uuid_characters) / sizeof(*uuid_characters));
+static constexpr uint8_t max_uuid_number_length = 19; // Max 0x7FFFFFFFFFFFFFFF size is 19 digits.
+
 String ResourceUID::id_to_text(ID p_id) const {
 	if (p_id < 0) {
 		return "uid://<invalid>";
 	}
-	String txt;
 
+	char32_t tmp[max_uuid_number_length];
+	uint32_t tmp_size = 0;
 	while (p_id) {
-		uint32_t c = p_id % base;
-		if (c < char_count) {
-			txt = String::chr('a' + c) + txt;
-		} else {
-			txt = String::chr('0' + (c - char_count)) + txt;
-		}
-		p_id /= base;
+		uint32_t c = p_id % uuid_characters_element_count;
+		tmp[tmp_size] = uuid_characters[c];
+		p_id /= uuid_characters_element_count;
+		++tmp_size;
 	}
 
-	return "uid://" + txt;
+	// tmp_size + uid:// (6) + 1 for null.
+	String txt;
+	txt.resize(tmp_size + 7);
+
+	char32_t *p = txt.ptrw();
+	p[0] = 'u';
+	p[1] = 'i';
+	p[2] = 'd';
+	p[3] = ':';
+	p[4] = '/';
+	p[5] = '/';
+	uint32_t size = 6;
+
+	// The above loop give the number backward, recopy it in the string in the correct order.
+	for (uint32_t i = 0; i < tmp_size; ++i) {
+		p[size++] = tmp[tmp_size - i - 1];
+	}
+
+	p[size] = 0;
+
+	return txt;
 }
 
 ResourceUID::ID ResourceUID::text_to_id(const String &p_text) const {