From c848bfc1bbfd869da709e36be52447c71c43ae46 Mon Sep 17 00:00:00 2001 From: Yuri Sizov Date: Fri, 26 Jan 2024 18:55:27 +0100 Subject: [PATCH 1/2] Benchmark resource preview generator with a verbose print --- editor/editor_resource_preview.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/editor/editor_resource_preview.cpp b/editor/editor_resource_preview.cpp index 1702277ebcd..623aa3f45c7 100644 --- a/editor/editor_resource_preview.cpp +++ b/editor/editor_resource_preview.cpp @@ -129,6 +129,8 @@ void EditorResourcePreview::_preview_ready(const String &p_path, int p_hash, con void EditorResourcePreview::_generate_preview(Ref &r_texture, Ref &r_small_texture, const QueueItem &p_item, const String &cache_base, Dictionary &p_metadata) { String type; + uint64_t started_at = OS::get_singleton()->get_ticks_usec(); + if (p_item.resource.is_valid()) { type = p_item.resource->get_class(); } else { @@ -138,6 +140,10 @@ void EditorResourcePreview::_generate_preview(Ref &r_texture, Ref< if (type.is_empty()) { r_texture = Ref(); r_small_texture = Ref(); + + if (is_print_verbose_enabled()) { + print_line(vformat("Generated '%s' preview in %d usec", p_item.path, OS::get_singleton()->get_ticks_usec() - started_at)); + } return; //could not guess type } @@ -196,6 +202,10 @@ void EditorResourcePreview::_generate_preview(Ref &r_texture, Ref< _write_preview_cache(f, thumbnail_size, has_small_texture, FileAccess::get_modified_time(p_item.path), FileAccess::get_md5(p_item.path), p_metadata); } } + + if (is_print_verbose_enabled()) { + print_line(vformat("Generated '%s' preview in %d usec", p_item.path, OS::get_singleton()->get_ticks_usec() - started_at)); + } } const Dictionary EditorResourcePreview::get_preview_metadata(const String &p_path) const { From 9c919ea28504ae67226a5c8df94227e9da8899b7 Mon Sep 17 00:00:00 2001 From: Yuri Sizov Date: Fri, 26 Jan 2024 13:03:32 +0100 Subject: [PATCH 2/2] Generate script resource preview without parsing --- core/object/script_language.cpp | 12 ++++++++ core/object/script_language.h | 1 + editor/plugins/editor_preview_plugins.cpp | 37 +++++++++++++++++------ editor/plugins/editor_preview_plugins.h | 5 +++ 4 files changed, 45 insertions(+), 10 deletions(-) diff --git a/core/object/script_language.cpp b/core/object/script_language.cpp index 0b2d5e41cfc..3b9b1f90947 100644 --- a/core/object/script_language.cpp +++ b/core/object/script_language.cpp @@ -168,6 +168,18 @@ ScriptLanguage *ScriptServer::get_language(int p_idx) { return _languages[p_idx]; } +ScriptLanguage *ScriptServer::get_language_for_extension(const String &p_extension) { + MutexLock lock(languages_mutex); + + for (int i = 0; i < _language_count; i++) { + if (_languages[i] && _languages[i]->get_extension() == p_extension) { + return _languages[i]; + } + } + + return nullptr; +} + Error ScriptServer::register_language(ScriptLanguage *p_language) { MutexLock lock(languages_mutex); ERR_FAIL_NULL_V(p_language, ERR_INVALID_PARAMETER); diff --git a/core/object/script_language.h b/core/object/script_language.h index bb714d5bc35..294231a3e7d 100644 --- a/core/object/script_language.h +++ b/core/object/script_language.h @@ -75,6 +75,7 @@ public: static bool is_scripting_enabled(); _FORCE_INLINE_ static int get_language_count() { return _language_count; } static ScriptLanguage *get_language(int p_idx); + static ScriptLanguage *get_language_for_extension(const String &p_extension); static Error register_language(ScriptLanguage *p_language); static Error unregister_language(const ScriptLanguage *p_language); diff --git a/editor/plugins/editor_preview_plugins.cpp b/editor/plugins/editor_preview_plugins.cpp index 90bd117543b..0019922f9c1 100644 --- a/editor/plugins/editor_preview_plugins.cpp +++ b/editor/plugins/editor_preview_plugins.cpp @@ -464,6 +464,17 @@ bool EditorScriptPreviewPlugin::handles(const String &p_type) const { return ClassDB::is_parent_class(p_type, "Script"); } +Ref EditorScriptPreviewPlugin::generate_from_path(const String &p_path, const Size2 &p_size, Dictionary &p_metadata) const { + Error err; + String code = FileAccess::get_file_as_string(p_path, &err); + if (err != OK) { + return Ref(); + } + + ScriptLanguage *lang = ScriptServer::get_language_for_extension(p_path.get_extension()); + return _generate_from_source_code(lang, code, p_size, p_metadata); +} + Ref EditorScriptPreviewPlugin::generate(const Ref &p_from, const Size2 &p_size, Dictionary &p_metadata) const { Ref