From d36d7e2a1c83141a3051d2c24b01c9a9e6104f89 Mon Sep 17 00:00:00 2001 From: George Marques Date: Sun, 23 Aug 2020 11:19:35 -0300 Subject: [PATCH] GDScript: Use pointer instead of references in cache They are not supposed to be kept alive and this is cleaner and less error-prone than unreferencing the elements. --- modules/gdscript/gdscript.cpp | 4 +--- modules/gdscript/gdscript_cache.cpp | 14 +++++--------- modules/gdscript/gdscript_cache.h | 6 +++--- 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/modules/gdscript/gdscript.cpp b/modules/gdscript/gdscript.cpp index 541d46a2af9..0263e32c5b7 100644 --- a/modules/gdscript/gdscript.cpp +++ b/modules/gdscript/gdscript.cpp @@ -603,10 +603,8 @@ Error GDScript::reload(bool p_keep_state) { } if (!source_path.empty()) { MutexLock lock(GDScriptCache::singleton->lock); - Ref self(this); if (!GDScriptCache::singleton->shallow_gdscript_cache.has(source_path)) { - GDScriptCache::singleton->shallow_gdscript_cache[source_path] = self; - self->unreference(); + GDScriptCache::singleton->shallow_gdscript_cache[source_path] = this; } } } diff --git a/modules/gdscript/gdscript_cache.cpp b/modules/gdscript/gdscript_cache.cpp index cdb14d62816..992f8f4b580 100644 --- a/modules/gdscript/gdscript_cache.cpp +++ b/modules/gdscript/gdscript_cache.cpp @@ -127,7 +127,7 @@ Ref GDScriptCache::get_parser(const String &p_path, GDScriptP singleton->dependencies[p_owner].insert(p_path); } if (singleton->parser_map.has(p_path)) { - ref = singleton->parser_map[p_path]; + ref = Ref(singleton->parser_map[p_path]); } else { if (!FileAccess::exists(p_path)) { r_error = ERR_FILE_NOT_FOUND; @@ -137,8 +137,7 @@ Ref GDScriptCache::get_parser(const String &p_path, GDScriptP ref.instance(); ref->parser = parser; ref->path = p_path; - singleton->parser_map[p_path] = ref; - ref->unreference(); + singleton->parser_map[p_path] = ref.ptr(); } r_error = ref->raise_status(p_status); @@ -186,10 +185,7 @@ Ref GDScriptCache::get_shallow_script(const String &p_path, const Stri script->set_script_path(p_path); script->load_source_code(p_path); - singleton->shallow_gdscript_cache[p_path] = script; - // The one in cache is not a hard reference: if the script dies somewhere else it's fine. - // Scripts remove themselves from cache when they die. - script->unreference(); + singleton->shallow_gdscript_cache[p_path] = script.ptr(); return script; } @@ -217,7 +213,7 @@ Ref GDScriptCache::get_full_script(const String &p_path, Error &r_erro return script; } - singleton->full_gdscript_cache[p_path] = script; + singleton->full_gdscript_cache[p_path] = script.ptr(); singleton->shallow_gdscript_cache.erase(p_path); return script; @@ -226,7 +222,7 @@ Ref GDScriptCache::get_full_script(const String &p_path, Error &r_erro Error GDScriptCache::finish_compiling(const String &p_owner) { // Mark this as compiled. Ref script = get_shallow_script(p_owner); - singleton->full_gdscript_cache[p_owner] = script; + singleton->full_gdscript_cache[p_owner] = script.ptr(); singleton->shallow_gdscript_cache.erase(p_owner); Set depends = singleton->dependencies[p_owner]; diff --git a/modules/gdscript/gdscript_cache.h b/modules/gdscript/gdscript_cache.h index 770704d6eb0..865df340516 100644 --- a/modules/gdscript/gdscript_cache.h +++ b/modules/gdscript/gdscript_cache.h @@ -70,9 +70,9 @@ public: class GDScriptCache { // String key is full path. - HashMap> parser_map; - HashMap> shallow_gdscript_cache; - HashMap> full_gdscript_cache; + HashMap parser_map; + HashMap shallow_gdscript_cache; + HashMap full_gdscript_cache; HashMap> dependencies; friend class GDScript;