Skip to content

Commit

Permalink
Speed up GDScript::get_must_clear_dependencies()
Browse files Browse the repository at this point in the history
get_must_clear_dependencies() has a N^3*log(N) time complexity, and this can very quickly slow down the quitting process as more gdscripts are added in a project.
This change improves it to N^2*log(N).
Instead of using all the inverted dependencies, we do the same with all (non-inverted) dependencies, which is N times faster.

Fixes #85435
  • Loading branch information
eldidou committed Dec 9, 2023
1 parent b94eb58 commit 0d77c3e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 26 deletions.
41 changes: 16 additions & 25 deletions modules/gdscript/gdscript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1156,8 +1156,8 @@ RBSet<GDScript *> GDScript::get_dependencies() {
return dependencies;
}

RBSet<GDScript *> GDScript::get_inverted_dependencies() {
RBSet<GDScript *> inverted_dependencies;
HashMap<GDScript *, RBSet<GDScript *>> GDScript::get_all_dependencies() {
HashMap<GDScript *, RBSet<GDScript *>> all_dependencies;

List<GDScript *> scripts;
{
Expand All @@ -1171,51 +1171,42 @@ RBSet<GDScript *> GDScript::get_inverted_dependencies() {
}

for (GDScript *scr : scripts) {
if (scr == nullptr || scr == this || scr->destructing) {
if (scr == nullptr || scr->destructing) {
continue;
}

RBSet<GDScript *> scr_dependencies = scr->get_dependencies();
if (scr_dependencies.has(this)) {
inverted_dependencies.insert(scr);
}
all_dependencies.insert(scr, scr->get_dependencies());
}

return inverted_dependencies;
return all_dependencies;
}

RBSet<GDScript *> GDScript::get_must_clear_dependencies() {
RBSet<GDScript *> dependencies = get_dependencies();
RBSet<GDScript *> must_clear_dependencies;
HashMap<GDScript *, RBSet<GDScript *>> inverted_dependencies;

for (GDScript *E : dependencies) {
inverted_dependencies.insert(E, E->get_inverted_dependencies());
}
HashMap<GDScript *, RBSet<GDScript *>> all_dependencies = get_all_dependencies();

RBSet<GDScript *> cant_clear;
for (KeyValue<GDScript *, RBSet<GDScript *>> &E : inverted_dependencies) {
for (KeyValue<GDScript *, RBSet<GDScript *>> &E : all_dependencies) {
if (dependencies.has(E.key)) {
continue;
}
for (GDScript *F : E.value) {
if (!dependencies.has(F)) {
cant_clear.insert(E.key);
for (GDScript *G : E.key->get_dependencies()) {
cant_clear.insert(G);
}
break;
if (dependencies.has(F)) {
cant_clear.insert(F);
}
}
}

for (KeyValue<GDScript *, RBSet<GDScript *>> &E : inverted_dependencies) {
if (cant_clear.has(E.key) || ScriptServer::is_global_class(E.key->get_fully_qualified_name())) {
for (GDScript *E : dependencies) {
if (cant_clear.has(E) || ScriptServer::is_global_class(E->get_fully_qualified_name())) {
continue;
}
must_clear_dependencies.insert(E.key);
must_clear_dependencies.insert(E);
}

cant_clear.clear();
dependencies.clear();
inverted_dependencies.clear();
all_dependencies.clear();
return must_clear_dependencies;
}

Expand Down
2 changes: 1 addition & 1 deletion modules/gdscript/gdscript.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ class GDScript : public Script {
const Ref<GDScriptNativeClass> &get_native() const { return native; }

RBSet<GDScript *> get_dependencies();
RBSet<GDScript *> get_inverted_dependencies();
HashMap<GDScript *, RBSet<GDScript *>> get_all_dependencies();
RBSet<GDScript *> get_must_clear_dependencies();

virtual bool has_script_signal(const StringName &p_signal) const override;
Expand Down

0 comments on commit 0d77c3e

Please sign in to comment.