Skip to content

Commit

Permalink
Merge pull request godotengine#100335 from kiroxas/Improve`ResourceUI…
Browse files Browse the repository at this point in the history
…D--id_to_text`Performance

Improve `ResourceUID::id_to_text` performance
  • Loading branch information
Repiteo committed Dec 16, 2024
2 parents b1fb0f5 + 5f401e6 commit 24a8c05
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions core/io/resource_uid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 24a8c05

Please sign in to comment.