Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-enable docs cache with fixes #78615

Merged
merged 1 commit into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/object/class_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ MethodDefinition D_METHODP(const char *p_name, const char *const **p_args, uint3
#endif

ClassDB::APIType ClassDB::current_api = API_CORE;
HashMap<ClassDB::APIType, uint64_t> ClassDB::api_hashes_cache;
HashMap<ClassDB::APIType, uint32_t> ClassDB::api_hashes_cache;

void ClassDB::set_current_api(APIType p_api) {
DEV_ASSERT(!api_hashes_cache.has(p_api)); // This API type may not be suitable for caching of hash if it can change later.
Expand Down Expand Up @@ -163,7 +163,7 @@ ClassDB::APIType ClassDB::get_api_type(const StringName &p_class) {
return ti->api;
}

uint64_t ClassDB::get_api_hash(APIType p_api) {
uint32_t ClassDB::get_api_hash(APIType p_api) {
OBJTYPE_RLOCK;
#ifdef DEBUG_METHODS_ENABLED

Expand Down
4 changes: 2 additions & 2 deletions core/object/class_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class ClassDB {
#endif

static APIType current_api;
static HashMap<APIType, uint64_t> api_hashes_cache;
static HashMap<APIType, uint32_t> api_hashes_cache;

static void _add_class2(const StringName &p_class, const StringName &p_inherits);

Expand Down Expand Up @@ -246,7 +246,7 @@ class ClassDB {

static APIType get_api_type(const StringName &p_class);

static uint64_t get_api_hash(APIType p_api);
static uint32_t get_api_hash(APIType p_api);

template <typename>
struct member_function_traits;
Expand Down
50 changes: 11 additions & 39 deletions editor/editor_help.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,33 +49,6 @@
// Might this be a problem?
DocTools *EditorHelp::doc = nullptr;

class DocCache : public Resource {
GDCLASS(DocCache, Resource);
RES_BASE_EXTENSION("doc_cache");

String version_hash;
Array classes;

protected:
static void _bind_methods() {
ClassDB::bind_method(D_METHOD("set_version_hash", "version_hash"), &DocCache::set_version_hash);
ClassDB::bind_method(D_METHOD("get_version_hash"), &DocCache::get_version_hash);

ClassDB::bind_method(D_METHOD("set_classes", "classes"), &DocCache::set_classes);
ClassDB::bind_method(D_METHOD("get_classes"), &DocCache::get_classes);

ADD_PROPERTY(PropertyInfo(Variant::STRING, "version_hash"), "set_version_hash", "get_version_hash");
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "classes"), "set_classes", "get_classes");
}

public:
String get_version_hash() const { return version_hash; }
void set_version_hash(const String &p_version_hash) { version_hash = p_version_hash; }

Array get_classes() const { return classes; }
void set_classes(const Array &p_classes) { classes = p_classes; }
};

static bool _attempt_doc_load(const String &p_class) {
// Docgen always happens in the outer-most class: it also generates docs for inner classes.
String outer_class = p_class.get_slice(".", 0);
Expand Down Expand Up @@ -2264,21 +2237,23 @@ void EditorHelp::_wait_for_thread() {
}

String EditorHelp::get_cache_full_path() {
return EditorPaths::get_singleton()->get_cache_dir().path_join("editor.doc_cache");
return EditorPaths::get_singleton()->get_cache_dir().path_join("editor_doc_cache.res");
}

static bool first_attempt = true;

static String _compute_doc_version_hash() {
return vformat("%d/%d/%s", ClassDB::get_api_hash(ClassDB::API_CORE), ClassDB::get_api_hash(ClassDB::API_EDITOR), _doc_data_hash);
uint32_t version_hash = Engine::get_singleton()->get_version_info().hash();
return vformat("%d/%d/%d/%s", version_hash, ClassDB::get_api_hash(ClassDB::API_CORE), ClassDB::get_api_hash(ClassDB::API_EDITOR), _doc_data_hash);
}

void EditorHelp::_load_doc_thread(void *p_udata) {
DEV_ASSERT(first_attempt);
Ref<DocCache> cache_res = ResourceLoader::load(get_cache_full_path());
if (cache_res.is_valid() && cache_res->get_version_hash() == _compute_doc_version_hash()) {
for (int i = 0; i < cache_res->get_classes().size(); i++) {
doc->add_doc(DocData::ClassDoc::from_dict(cache_res->get_classes()[i]));
Ref<Resource> cache_res = ResourceLoader::load(get_cache_full_path());
if (cache_res.is_valid() && cache_res->get_meta("version_hash", "") == _compute_doc_version_hash()) {
Array classes = cache_res->get_meta("classes", Array());
for (int i = 0; i < classes.size(); i++) {
doc->add_doc(DocData::ClassDoc::from_dict(classes[i]));
}
} else {
// We have to go back to the main thread to start from scratch.
Expand All @@ -2292,14 +2267,14 @@ void EditorHelp::_gen_doc_thread(void *p_udata) {
compdoc.load_compressed(_doc_data_compressed, _doc_data_compressed_size, _doc_data_uncompressed_size);
doc->merge_from(compdoc); // Ensure all is up to date.

Ref<DocCache> cache_res;
Ref<Resource> cache_res;
cache_res.instantiate();
cache_res->set_version_hash(_compute_doc_version_hash());
cache_res->set_meta("version_hash", _compute_doc_version_hash());
Array classes;
for (const KeyValue<String, DocData::ClassDoc> &E : doc->class_list) {
classes.push_back(DocData::ClassDoc::to_dict(E.value));
}
cache_res->set_classes(classes);
cache_res->set_meta("classes", classes);
Error err = ResourceSaver::save(cache_res, get_cache_full_path(), ResourceSaver::FLAG_COMPRESS);
if (err) {
ERR_PRINT("Cannot save editor help cache (" + get_cache_full_path() + ").");
Expand All @@ -2309,9 +2284,6 @@ void EditorHelp::_gen_doc_thread(void *p_udata) {
static bool doc_gen_use_threads = true;

void EditorHelp::generate_doc(bool p_use_cache) {
// Temporarily disable use of cache for pre-RC stabilization.
p_use_cache = false;

OS::get_singleton()->benchmark_begin_measure("EditorHelp::generate_doc");
if (doc_gen_use_threads) {
// In case not the first attempt.
Expand Down
3 changes: 2 additions & 1 deletion editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6732,7 +6732,8 @@ EditorNode::EditorNode() {
// No scripting by default if in editor.
ScriptServer::set_scripting_enabled(false);

EditorHelp::generate_doc(); // Before any editor classes are created.
EditorSettings::ensure_class_registered();
EditorHelp::generate_doc();
SceneState::set_disable_placeholders(true);
ResourceLoader::clear_translation_remaps(); // Using no remaps if in editor.
ResourceLoader::clear_path_remaps();
Expand Down
9 changes: 8 additions & 1 deletion editor/editor_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,13 @@ EditorSettings *EditorSettings::get_singleton() {
return singleton.ptr();
}

void EditorSettings::ensure_class_registered() {
ClassDB::APIType prev_api = ClassDB::get_current_api();
ClassDB::set_current_api(ClassDB::API_EDITOR);
GDREGISTER_CLASS(EditorSettings); // Otherwise it can't be unserialized.
ClassDB::set_current_api(prev_api);
}

void EditorSettings::create() {
// IMPORTANT: create() *must* create a valid EditorSettings singleton,
// as the rest of the engine code will assume it. As such, it should never
Expand All @@ -887,7 +894,7 @@ void EditorSettings::create() {
return;
}

GDREGISTER_CLASS(EditorSettings); // Otherwise it can't be unserialized.
ensure_class_registered();

String config_file_path;
Ref<ConfigFile> extra_config = memnew(ConfigFile);
Expand Down
1 change: 1 addition & 0 deletions editor/editor_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class EditorSettings : public Resource {

static EditorSettings *get_singleton();

static void ensure_class_registered();
static void create();
void setup_language();
void setup_network();
Expand Down