Skip to content

Commit

Permalink
Fix slow editor load on large projects (v2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hilderin committed Aug 17, 2024
1 parent 1bd740d commit 2ab3d17
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 34 deletions.
8 changes: 3 additions & 5 deletions core/io/file_access.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,9 @@ bool FileAccess::exists(const String &p_name) {
return true;
}

Ref<FileAccess> f = open(p_name, READ);
if (f.is_null()) {
return false;
}
return true;
// Using file_exists because it's faster than trying to open the file.
Ref<FileAccess> ret = create_for_path(p_name);
return ret->file_exists(p_name);
}

void FileAccess::_set_access_type(AccessType p_access) {
Expand Down
2 changes: 1 addition & 1 deletion core/io/resource_importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ bool ResourceFormatImporter::are_import_settings_valid(const String &p_path) con

for (int i = 0; i < importers.size(); i++) {
if (importers[i]->get_importer_name() == pat.importer) {
if (!importers[i]->are_import_settings_valid(p_path)) { //importer thinks this is not valid
if (!importers[i]->are_import_settings_valid(p_path, pat.metadata)) { //importer thinks this is not valid
return false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/io/resource_importer.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class ResourceImporter : public RefCounted {
virtual void import_threaded_end() {}

virtual Error import_group_file(const String &p_group_file, const HashMap<String, HashMap<StringName, Variant>> &p_source_file_options, const HashMap<String, String> &p_base_paths) { return ERR_UNAVAILABLE; }
virtual bool are_import_settings_valid(const String &p_path) const { return true; }
virtual bool are_import_settings_valid(const String &p_path, const Dictionary &p_meta) const { return true; }
virtual String get_import_settings_string() const { return String(); }
};

Expand Down
32 changes: 23 additions & 9 deletions editor/editor_file_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,21 @@ void EditorFileSystem::_first_scan_process_scripts(const ScannedDirectory *p_sca
}

for (const String &scan_file : p_scan_dir->files) {
// Optimization to skip the ResourceLoader::get_resource_type for files
// that are not scripts. Some loader get_resource_type methods read the file
// which can be very slow on large projects.
String ext = scan_file.get_extension().to_lower();
bool is_script = false;
for (int i = 0; i < ScriptServer::get_language_count(); i++) {
if (ScriptServer::get_language(i)->get_extension() == ext) {
is_script = true;
break;
}
}
if (!is_script) {
continue; // Not a script.
}

String path = p_scan_dir->full_path.path_join(scan_file);
String type = ResourceLoader::get_resource_type(path);

Expand Down Expand Up @@ -431,10 +446,6 @@ bool EditorFileSystem::_test_for_reimport(const String &p_path, bool p_only_impo
return false;
}

if (!FileAccess::exists(p_path + ".import")) {
return true;
}

Error err;
Ref<FileAccess> f = FileAccess::open(p_path + ".import", FileAccess::READ, &err);

Expand All @@ -461,6 +472,7 @@ bool EditorFileSystem::_test_for_reimport(const String &p_path, bool p_only_impo
String dest_md5 = "";
int version = 0;
bool found_uid = false;
Variant meta;

while (true) {
assign = Variant();
Expand Down Expand Up @@ -500,18 +512,15 @@ bool EditorFileSystem::_test_for_reimport(const String &p_path, bool p_only_impo
} else if (assign == "dest_files") {
dest_files = value;
}
} else if (assign == "metadata") {
meta = value;
}

} else if (next_tag.name != "remap" && next_tag.name != "deps") {
break;
}
}

if (!ResourceFormatImporter::get_singleton()->are_import_settings_valid(p_path)) {
// Reimport settings are out of sync with project settings, reimport.
return true;
}

if (importer_name == "keep" || importer_name == "skip") {
return false; //keep mode, do not reimport
}
Expand All @@ -530,6 +539,11 @@ bool EditorFileSystem::_test_for_reimport(const String &p_path, bool p_only_impo
return true; // version changed, reimport
}

if (!importer->are_import_settings_valid(p_path, meta)) {
// Reimport settings are out of sync with project settings, reimport.
return true;
}

// Read the md5's from a separate file (so the import parameters aren't dependent on the file version
String base_path = ResourceFormatImporter::get_singleton()->get_import_base_path(p_path);
Ref<FileAccess> md5s = FileAccess::open(base_path + ".md5", FileAccess::READ, &err);
Expand Down
12 changes: 5 additions & 7 deletions editor/import/resource_importer_layered_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,22 +433,20 @@ String ResourceImporterLayeredTexture::get_import_settings_string() const {
return s;
}

bool ResourceImporterLayeredTexture::are_import_settings_valid(const String &p_path) const {
bool ResourceImporterLayeredTexture::are_import_settings_valid(const String &p_path, const Dictionary &p_meta) const {
//will become invalid if formats are missing to import
Dictionary meta = ResourceFormatImporter::get_singleton()->get_resource_metadata(p_path);

if (!meta.has("vram_texture")) {
if (!p_meta.has("vram_texture")) {
return false;
}

bool vram = meta["vram_texture"];
bool vram = p_meta["vram_texture"];
if (!vram) {
return true; //do not care about non vram
}

Vector<String> formats_imported;
if (meta.has("imported_formats")) {
formats_imported = meta["imported_formats"];
if (p_meta.has("imported_formats")) {
formats_imported = p_meta["imported_formats"];
}

int index = 0;
Expand Down
2 changes: 1 addition & 1 deletion editor/import/resource_importer_layered_texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class ResourceImporterLayeredTexture : public ResourceImporter {

virtual Error import(const String &p_source_file, const String &p_save_path, const HashMap<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files = nullptr, Variant *r_metadata = nullptr) override;

virtual bool are_import_settings_valid(const String &p_path) const override;
virtual bool are_import_settings_valid(const String &p_path, const Dictionary &p_meta) const override;
virtual String get_import_settings_string() const override;

void set_mode(Mode p_mode) { mode = p_mode; }
Expand Down
16 changes: 7 additions & 9 deletions editor/import/resource_importer_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -740,16 +740,14 @@ String ResourceImporterTexture::get_import_settings_string() const {
return s;
}

bool ResourceImporterTexture::are_import_settings_valid(const String &p_path) const {
Dictionary meta = ResourceFormatImporter::get_singleton()->get_resource_metadata(p_path);

if (meta.has("has_editor_variant")) {
bool ResourceImporterTexture::are_import_settings_valid(const String &p_path, const Dictionary &p_meta) const {
if (p_meta.has("has_editor_variant")) {
String imported_path = ResourceFormatImporter::get_singleton()->get_internal_resource_path(p_path);
if (!FileAccess::exists(imported_path)) {
return false;
}

String editor_meta_path = imported_path.replace(".editor.ctex", ".editor.meta");
String editor_meta_path = imported_path.replace(".editor.ctex", ".editor.p_meta");
Dictionary editor_meta = _load_editor_meta(editor_meta_path);

if (editor_meta.has("editor_scale") && (float)editor_meta["editor_scale"] != EDSCALE) {
Expand All @@ -760,19 +758,19 @@ bool ResourceImporterTexture::are_import_settings_valid(const String &p_path) co
}
}

if (!meta.has("vram_texture")) {
if (!p_meta.has("vram_texture")) {
return false;
}

bool vram = meta["vram_texture"];
bool vram = p_meta["vram_texture"];
if (!vram) {
return true; // Do not care about non-VRAM.
}

// Will become invalid if formats are missing to import.
Vector<String> formats_imported;
if (meta.has("imported_formats")) {
formats_imported = meta["imported_formats"];
if (p_meta.has("imported_formats")) {
formats_imported = p_meta["imported_formats"];
}

int index = 0;
Expand Down
2 changes: 1 addition & 1 deletion editor/import/resource_importer_texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class ResourceImporterTexture : public ResourceImporter {

void update_imports();

virtual bool are_import_settings_valid(const String &p_path) const override;
virtual bool are_import_settings_valid(const String &p_path, const Dictionary &p_meta) const override;
virtual String get_import_settings_string() const override;

ResourceImporterTexture(bool p_singleton = false);
Expand Down

0 comments on commit 2ab3d17

Please sign in to comment.