1
0
Fork 0

Improve `ResourceUID::id_to_text` performance

This commit is contained in:
Kiro 2024-12-12 20:05:35 +01:00
parent 19e003bc08
commit 5f401e63b5
1 changed files with 31 additions and 9 deletions

View File

@ -45,23 +45,45 @@ String ResourceUID::get_cache_file() {
return ProjectSettings::get_singleton()->get_project_data_path().path_join("uid_cache.bin"); 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 { String ResourceUID::id_to_text(ID p_id) const {
if (p_id < 0) { if (p_id < 0) {
return "uid://<invalid>"; return "uid://<invalid>";
} }
String txt;
char32_t tmp[max_uuid_number_length];
uint32_t tmp_size = 0;
while (p_id) { while (p_id) {
uint32_t c = p_id % base; uint32_t c = p_id % uuid_characters_element_count;
if (c < char_count) { tmp[tmp_size] = uuid_characters[c];
txt = String::chr('a' + c) + txt; p_id /= uuid_characters_element_count;
} else { ++tmp_size;
txt = String::chr('0' + (c - char_count)) + txt;
}
p_id /= base;
} }
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 { ResourceUID::ID ResourceUID::text_to_id(const String &p_text) const {