Add reverse UID cache

This commit is contained in:
kobewi 2025-09-12 20:18:44 +02:00
parent dec5a373d9
commit 60591dc7e8
4 changed files with 38 additions and 2 deletions

View File

@ -1184,7 +1184,10 @@ String ResourceLoader::get_resource_script_class(const String &p_path) {
}
ResourceUID::ID ResourceLoader::get_resource_uid(const String &p_path) {
String local_path = _validate_local_path(p_path);
const String local_path = _validate_local_path(p_path);
if (!Engine::get_singleton()->is_editor_hint()) {
return ResourceUID::get_singleton()->get_path_id(local_path);
}
for (int i = 0; i < loader_count; i++) {
ResourceUID::ID id = loader[i]->get_resource_uid(local_path);

View File

@ -160,6 +160,9 @@ void ResourceUID::add_id(ID p_id, const String &p_path) {
Cache c;
c.cs = p_path.utf8();
unique_ids[p_id] = c;
if (use_reverse_cache) {
reverse_cache[c.cs] = p_id;
}
changed = true;
}
@ -175,6 +178,9 @@ void ResourceUID::set_id(ID p_id, const String &p_path) {
if ((update_ptr == nullptr) != (cached_ptr == nullptr) || strcmp(update_ptr, cached_ptr) != 0) {
unique_ids[p_id].cs = cs;
unique_ids[p_id].saved_to_cache = false; //changed
if (use_reverse_cache) {
reverse_cache[cs] = p_id;
}
changed = true;
}
}
@ -201,9 +207,20 @@ String ResourceUID::get_id_path(ID p_id) const {
return String::utf8(cs.ptr());
}
ResourceUID::ID ResourceUID::get_path_id(const String &p_path) const {
const ID *id = reverse_cache.getptr(p_path.utf8());
if (id) {
return *id;
}
return INVALID_ID;
}
void ResourceUID::remove_id(ID p_id) {
MutexLock l(mutex);
ERR_FAIL_COND(!unique_ids.has(p_id));
if (use_reverse_cache) {
reverse_cache.erase(unique_ids[p_id].cs);
}
unique_ids.erase(p_id);
}
@ -265,6 +282,9 @@ Error ResourceUID::load_from_cache(bool p_reset) {
MutexLock l(mutex);
if (p_reset) {
if (use_reverse_cache) {
reverse_cache.clear();
}
unique_ids.clear();
}
@ -281,6 +301,9 @@ Error ResourceUID::load_from_cache(bool p_reset) {
c.saved_to_cache = true;
unique_ids[id] = c;
if (use_reverse_cache) {
reverse_cache[c.cs] = id;
}
}
cache_entries = entry_count;
@ -351,6 +374,9 @@ String ResourceUID::get_path_from_cache(Ref<FileAccess> &p_cache_file, const Str
void ResourceUID::clear() {
cache_entries = 0;
if (use_reverse_cache) {
reverse_cache.clear();
}
unique_ids.clear();
changed = false;
}

View File

@ -54,7 +54,9 @@ private:
bool saved_to_cache = false;
};
HashMap<ID, Cache> unique_ids; //unique IDs and utf8 paths (less memory used)
HashMap<ID, Cache> unique_ids; // Unique IDs and utf8 paths (less memory used).
bool use_reverse_cache = false;
HashMap<CharString, ID> reverse_cache; // Used at runtime.
static ResourceUID *singleton;
uint32_t cache_entries = 0;
@ -75,6 +77,7 @@ public:
void add_id(ID p_id, const String &p_path);
void set_id(ID p_id, const String &p_path);
String get_id_path(ID p_id) const;
ID get_path_id(const String &p_path) const;
void remove_id(ID p_id);
static String uid_to_path(const String &p_uid);
@ -86,6 +89,7 @@ public:
Error update_cache();
static String get_path_from_cache(Ref<FileAccess> &p_cache_file, const String &p_uid_string);
void enable_reverse_cache() { use_reverse_cache = true; }
void clear();
static ResourceUID *get_singleton() { return singleton; }

View File

@ -2201,6 +2201,9 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph
initialize_modules(MODULE_INITIALIZATION_LEVEL_CORE);
register_core_extensions(); // core extensions must be registered after globals setup and before display
if (!editor) {
ResourceUID::get_singleton()->enable_reverse_cache();
}
ResourceUID::get_singleton()->load_from_cache(true); // Load UUIDs from cache.
ProjectSettings::get_singleton()->fix_autoload_paths(); // Handles autoloads saved as UID.