Browse Source

Merge pull request #50864 from reduz/gen-doc-on-thread

Rémi Verschelde 3 years ago
parent
commit
bd91ca027a
3 changed files with 52 additions and 5 deletions
  1. 43 3
      editor/editor_help.cpp
  2. 8 1
      editor/editor_help.h
  3. 1 1
      editor/editor_node.cpp

+ 43 - 3
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<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);

+ 8 - 1
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);

+ 1 - 1
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);