From df7636b19acc423687f9d785dd7902f94a018f18 Mon Sep 17 00:00:00 2001 From: reduz Date: Sun, 25 Jul 2021 14:44:03 -0300 Subject: [PATCH] Generate editor docs on a thread * The main generation could not be moved to a thread, as it instantiates classes to get default values, interacts with ProjectSettings, etc. * Only uncompressing documentation and merging it is threaded. * Seems to improve editor load times by 0.5 seconds. --- editor/editor_help.cpp | 46 +++++++++++++++++++++++++++++++++++++++--- editor/editor_help.h | 9 ++++++++- editor/editor_node.cpp | 2 +- 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/editor/editor_help.cpp b/editor/editor_help.cpp index 5178f505536..06e3a63f4aa 100644 --- a/editor/editor_help.cpp +++ b/editor/editor_help.cpp @@ -1724,14 +1724,34 @@ void EditorHelp::_add_text(const String &p_bbcode) { _add_text_to_rt(p_bbcode, class_desc); } -void EditorHelp::generate_doc() { - doc = memnew(DocTools); - doc->generate(true); +Thread EditorHelp::thread; + +void EditorHelp::_wait_for_thread() { + if (thread.is_started()) { + thread.wait_to_finish(); + } +} + +void EditorHelp::_gen_doc_thread(void *p_udata) { DocTools compdoc; compdoc.load_compressed(_doc_data_compressed, _doc_data_compressed_size, _doc_data_uncompressed_size); doc->merge_from(compdoc); //ensure all is up to date } +static bool doc_gen_use_threads = true; + +void EditorHelp::generate_doc() { + doc = memnew(DocTools); + // Not doable on threads unfortunately, since it instantiates all sorts of classes to get default values. + doc->generate(true); + + if (doc_gen_use_threads) { + thread.start(_gen_doc_thread, nullptr); + } else { + _gen_doc_thread(nullptr); + } +} + void EditorHelp::_toggle_scripts_pressed() { ScriptEditor::get_singleton()->toggle_scripts_panel(); update_toggle_scripts_button(); @@ -1741,6 +1761,7 @@ void EditorHelp::_notification(int p_what) { switch (p_what) { case NOTIFICATION_READY: case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: { + _wait_for_thread(); _update_doc(); } break; case NOTIFICATION_THEME_CHANGED: { @@ -1758,20 +1779,32 @@ void EditorHelp::_notification(int p_what) { } void EditorHelp::go_to_help(const String &p_help) { + _wait_for_thread(); _help_callback(p_help); } void EditorHelp::go_to_class(const String &p_class, int p_scroll) { + _wait_for_thread(); _goto_desc(p_class, p_scroll); } void EditorHelp::update_doc() { + _wait_for_thread(); ERR_FAIL_COND(!doc->class_list.has(edited_class)); ERR_FAIL_COND(!doc->class_list[edited_class].is_script_doc); _update_doc(); } +void EditorHelp::cleanup_doc() { + _wait_for_thread(); + if (doc_gen_use_threads) { + thread.wait_to_finish(); + } + memdelete(doc); +} + Vector> EditorHelp::get_sections() { + _wait_for_thread(); Vector> sections; for (int i = 0; i < section_line.size(); i++) { @@ -1781,11 +1814,13 @@ Vector> EditorHelp::get_sections() { } void EditorHelp::scroll_to_section(int p_section_index) { + _wait_for_thread(); int line = section_line[p_section_index].second; class_desc->scroll_to_paragraph(line); } void EditorHelp::popup_search() { + _wait_for_thread(); find_bar->popup_search(); } @@ -1864,6 +1899,11 @@ EditorHelp::EditorHelp() { EditorHelp::~EditorHelp() { } +DocTools *EditorHelp::get_doc_data() { + _wait_for_thread(); + return doc; +} + void EditorHelpBit::_go_to_help(String p_what) { EditorNode::get_singleton()->set_visible_editor(EditorNode::EDITOR_SCRIPT); ScriptEditor::get_singleton()->goto_help(p_what); diff --git a/editor/editor_help.h b/editor/editor_help.h index f74c64bb7c4..eb879c6d398 100644 --- a/editor/editor_help.h +++ b/editor/editor_help.h @@ -31,6 +31,7 @@ #ifndef EDITOR_HELP_H #define EDITOR_HELP_H +#include "core/os/thread.h" #include "editor/code_editor.h" #include "editor/doc_tools.h" #include "editor/editor_plugin.h" @@ -164,13 +165,19 @@ class EditorHelp : public VBoxContainer { String _fix_constant(const String &p_constant) const; void _toggle_scripts_pressed(); + static Thread thread; + + static void _wait_for_thread(); + static void _gen_doc_thread(void *p_udata); + protected: void _notification(int p_what); static void _bind_methods(); public: static void generate_doc(); - static DocTools *get_doc_data() { return doc; } + static DocTools *get_doc_data(); + static void cleanup_doc(); void go_to_help(const String &p_help); void go_to_class(const String &p_class, int p_scroll = 0); diff --git a/editor/editor_node.cpp b/editor/editor_node.cpp index cca8bc1b298..afd5407f37e 100644 --- a/editor/editor_node.cpp +++ b/editor/editor_node.cpp @@ -7211,7 +7211,7 @@ EditorNode::~EditorNode() { EditorTranslationParser::get_singleton()->clean_parsers(); remove_print_handler(&print_handler); - memdelete(EditorHelp::get_doc_data()); + EditorHelp::cleanup_doc(); memdelete(editor_selection); memdelete(editor_plugins_over); memdelete(editor_plugins_force_over);