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.
This commit is contained in:
reduz 2021-07-25 14:44:03 -03:00 committed by Rémi Verschelde
parent fcc2648e18
commit df7636b19a
No known key found for this signature in database
GPG Key ID: C3336907360768E1
3 changed files with 52 additions and 5 deletions

View File

@ -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<Pair<String, int>> EditorHelp::get_sections() {
_wait_for_thread();
Vector<Pair<String, int>> sections;
for (int i = 0; i < section_line.size(); i++) {
@ -1781,11 +1814,13 @@ Vector<Pair<String, int>> 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);

View File

@ -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);

View File

@ -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);