Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop caching packed scenes in GDScript cache (on preload) #85501

Merged
merged 1 commit into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions modules/gdscript/gdscript_analyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4144,22 +4144,15 @@ void GDScriptAnalyzer::reduce_preload(GDScriptParser::PreloadNode *p_preload) {
} else {
// TODO: Don't load if validating: use completion cache.

// Must load GDScript and PackedScenes separately to permit cyclic references
// as ResourceLoader::load() detect and reject those.
// Must load GDScript separately to permit cyclic references
// as ResourceLoader::load() detects and rejects those.
if (ResourceLoader::get_resource_type(p_preload->resolved_path) == "GDScript") {
Error err = OK;
Ref<GDScript> res = GDScriptCache::get_shallow_script(p_preload->resolved_path, err, parser->script_path);
p_preload->resource = res;
if (err != OK) {
push_error(vformat(R"(Could not preload resource script "%s".)", p_preload->resolved_path), p_preload->path);
}
} else if (ResourceLoader::get_resource_type(p_preload->resolved_path) == "PackedScene") {
Error err = OK;
Ref<PackedScene> res = GDScriptCache::get_packed_scene(p_preload->resolved_path, err, parser->script_path);
p_preload->resource = res;
if (err != OK) {
push_error(vformat(R"(Could not preload resource scene "%s".)", p_preload->resolved_path), p_preload->path);
}
} else {
p_preload->resource = ResourceLoader::load(p_preload->resolved_path);
if (p_preload->resource.is_null()) {
Expand Down
79 changes: 0 additions & 79 deletions modules/gdscript/gdscript_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@

#include "core/io/file_access.h"
#include "core/templates/vector.h"
#include "scene/resources/packed_scene.h"

bool GDScriptParserRef::is_valid() const {
return parser != nullptr;
Expand Down Expand Up @@ -139,13 +138,6 @@ void GDScriptCache::move_script(const String &p_from, const String &p_to) {
return;
}

for (KeyValue<String, HashSet<String>> &E : singleton->packed_scene_dependencies) {
if (E.value.has(p_from)) {
E.value.insert(p_to);
E.value.erase(p_from);
}
}

if (singleton->parser_map.has(p_from) && !p_from.is_empty()) {
singleton->parser_map[p_to] = singleton->parser_map[p_from];
}
Expand Down Expand Up @@ -173,15 +165,6 @@ void GDScriptCache::remove_script(const String &p_path) {
return;
}

for (KeyValue<String, HashSet<String>> &E : singleton->packed_scene_dependencies) {
if (!E.value.has(p_path)) {
continue;
}
E.value.erase(p_path);
}

GDScriptCache::clear_unreferenced_packed_scenes();

if (singleton->parser_map.has(p_path)) {
singleton->parser_map[p_path]->clear();
singleton->parser_map.erase(p_path);
Expand Down Expand Up @@ -361,62 +344,6 @@ void GDScriptCache::remove_static_script(const String &p_fqcn) {
singleton->static_gdscript_cache.erase(p_fqcn);
}

Ref<PackedScene> GDScriptCache::get_packed_scene(const String &p_path, Error &r_error, const String &p_owner) {
MutexLock lock(singleton->mutex);

String path = p_path;
if (path.begins_with("uid://")) {
path = ResourceUID::get_singleton()->get_id_path(ResourceUID::get_singleton()->text_to_id(path));
}

if (singleton->packed_scene_cache.has(path)) {
singleton->packed_scene_dependencies[path].insert(p_owner);
return singleton->packed_scene_cache[path];
}

Ref<PackedScene> scene = ResourceCache::get_ref(path);
if (scene.is_valid()) {
singleton->packed_scene_cache[path] = scene;
singleton->packed_scene_dependencies[path].insert(p_owner);
return scene;
}
scene.instantiate();

r_error = OK;
if (path.is_empty()) {
r_error = ERR_FILE_BAD_PATH;
return scene;
}

scene->set_path(path);
singleton->packed_scene_cache[path] = scene;
singleton->packed_scene_dependencies[path].insert(p_owner);

scene->reload_from_file();
return scene;
}

void GDScriptCache::clear_unreferenced_packed_scenes() {
if (singleton == nullptr) {
return;
}

MutexLock lock(singleton->mutex);

if (singleton->cleared) {
return;
}

for (KeyValue<String, HashSet<String>> &E : singleton->packed_scene_dependencies) {
if (E.value.size() > 0 || !ResourceLoader::is_imported(E.key)) {
continue;
}

singleton->packed_scene_dependencies.erase(E.key);
singleton->packed_scene_cache.erase(E.key);
}
}

void GDScriptCache::clear() {
if (singleton == nullptr) {
return;
Expand All @@ -439,16 +366,10 @@ void GDScriptCache::clear() {
E->clear();
}

singleton->packed_scene_dependencies.clear();
singleton->packed_scene_cache.clear();

parser_map_refs.clear();
singleton->parser_map.clear();
singleton->shallow_gdscript_cache.clear();
singleton->full_gdscript_cache.clear();

singleton->packed_scene_cache.clear();
singleton->packed_scene_dependencies.clear();
}

GDScriptCache::GDScriptCache() {
Expand Down
6 changes: 0 additions & 6 deletions modules/gdscript/gdscript_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
#include "core/os/mutex.h"
#include "core/templates/hash_map.h"
#include "core/templates/hash_set.h"
#include "scene/resources/packed_scene.h"

class GDScriptAnalyzer;
class GDScriptParser;
Expand Down Expand Up @@ -81,8 +80,6 @@ class GDScriptCache {
HashMap<String, Ref<GDScript>> full_gdscript_cache;
HashMap<String, Ref<GDScript>> static_gdscript_cache;
HashMap<String, HashSet<String>> dependencies;
HashMap<String, Ref<PackedScene>> packed_scene_cache;
HashMap<String, HashSet<String>> packed_scene_dependencies;

friend class GDScript;
friend class GDScriptParserRef;
Expand All @@ -106,9 +103,6 @@ class GDScriptCache {
static void add_static_script(Ref<GDScript> p_script);
static void remove_static_script(const String &p_fqcn);

static Ref<PackedScene> get_packed_scene(const String &p_path, Error &r_error, const String &p_owner = "");
static void clear_unreferenced_packed_scenes();

static void clear();

GDScriptCache();
Expand Down
Loading