From 746dddc0673d7261f19b1e056e90e6e3a49ef33a Mon Sep 17 00:00:00 2001 From: reduz Date: Fri, 13 May 2022 15:04:37 +0200 Subject: Replace most uses of Map by HashMap * Map is unnecessary and inefficient in almost every case. * Replaced by the new HashMap. * Renamed Map to RBMap and Set to RBSet for cases that still make sense (order matters) but use is discouraged. There were very few cases where replacing by HashMap was undesired because keeping the key order was intended. I tried to keep those (as RBMap) as much as possible, but might have missed some. Review appreciated! --- core/config/engine.cpp | 4 ++-- core/config/engine.h | 2 +- core/config/project_settings.cpp | 51 ++++++++++++++++++++-------------------- core/config/project_settings.h | 18 +++++++------- 4 files changed, 38 insertions(+), 37 deletions(-) (limited to 'core/config') diff --git a/core/config/engine.cpp b/core/config/engine.cpp index ff8a8d283f..782d369ae6 100644 --- a/core/config/engine.cpp +++ b/core/config/engine.cpp @@ -208,9 +208,9 @@ void Engine::add_singleton(const Singleton &p_singleton) { } Object *Engine::get_singleton_object(const StringName &p_name) const { - const Map::Element *E = singleton_ptrs.find(p_name); + HashMap::ConstIterator E = singleton_ptrs.find(p_name); ERR_FAIL_COND_V_MSG(!E, nullptr, "Failed to retrieve non-existent singleton '" + String(p_name) + "'."); - return E->get(); + return E->value; } bool Engine::is_singleton_user_created(const StringName &p_name) const { diff --git a/core/config/engine.h b/core/config/engine.h index eac96852b3..82e3ee5487 100644 --- a/core/config/engine.h +++ b/core/config/engine.h @@ -69,7 +69,7 @@ private: bool _in_physics = false; List singletons; - Map singleton_ptrs; + HashMap singleton_ptrs; bool editor_hint = false; bool project_manager_hint = false; diff --git a/core/config/project_settings.cpp b/core/config/project_settings.cpp index ba108d75da..3ef9a69d06 100644 --- a/core/config/project_settings.cpp +++ b/core/config/project_settings.cpp @@ -331,7 +331,7 @@ struct _VCSort { void ProjectSettings::_get_property_list(List *p_list) const { _THREAD_SAFE_METHOD_ - Set<_VCSort> vclist; + RBSet<_VCSort> vclist; for (const KeyValue &E : props) { const VariantContainer *v = &E.value; @@ -360,7 +360,7 @@ void ProjectSettings::_get_property_list(List *p_list) const { vclist.insert(vc); } - for (Set<_VCSort>::Element *E = vclist.front(); E; E = E->next()) { + for (RBSet<_VCSort>::Element *E = vclist.front(); E; E = E->next()) { String prop_info_name = E->get().name; int dot = prop_info_name.find("."); if (dot != -1 && !custom_prop_info.has(prop_info_name)) { @@ -764,7 +764,7 @@ Error ProjectSettings::save() { return error; } -Error ProjectSettings::_save_settings_binary(const String &p_file, const Map> &props, const CustomMap &p_custom, const String &p_custom_features) { +Error ProjectSettings::_save_settings_binary(const String &p_file, const HashMap> &props, const CustomMap &p_custom, const String &p_custom_features) { Error err; Ref file = FileAccess::open(p_file, FileAccess::WRITE, &err); ERR_FAIL_COND_V_MSG(err != OK, err, "Couldn't save project.binary at " + p_file + "."); @@ -800,19 +800,20 @@ Error ProjectSettings::_save_settings_binary(const String &p_file, const Mapstore_32(count); //store how many properties are saved } - for (Map>::Element *E = props.front(); E; E = E->next()) { - for (String &key : E->get()) { - if (!E->key().is_empty()) { - key = E->key() + "/" + key; + for (const KeyValue> &E : props) { + for (const String &key : E.value) { + String k = key; + if (!E.key.is_empty()) { + k = E.key + "/" + k; } Variant value; - if (p_custom.has(key)) { - value = p_custom[key]; + if (p_custom.has(k)) { + value = p_custom[k]; } else { - value = get(key); + value = get(k); } - file->store_pascal_string(key); + file->store_pascal_string(k); int len; err = encode_variant(value, nullptr, len, true); @@ -831,7 +832,7 @@ Error ProjectSettings::_save_settings_binary(const String &p_file, const Map> &props, const CustomMap &p_custom, const String &p_custom_features) { +Error ProjectSettings::_save_settings_text(const String &p_file, const HashMap> &props, const CustomMap &p_custom, const String &p_custom_features) { Error err; Ref file = FileAccess::open(p_file, FileAccess::WRITE, &err); @@ -852,18 +853,18 @@ Error ProjectSettings::_save_settings_text(const String &p_file, const Mapstore_string("\n"); - for (const Map>::Element *E = props.front(); E; E = E->next()) { - if (E != props.front()) { + for (const KeyValue> &E : props) { + if (E.key != props.begin()->key) { file->store_string("\n"); } - if (!E->key().is_empty()) { - file->store_string("[" + E->key() + "]\n\n"); + if (!E.key.is_empty()) { + file->store_string("[" + E.key + "]\n\n"); } - for (const String &F : E->get()) { + for (const String &F : E.value) { String key = F; - if (!E->key().is_empty()) { - key = E->key() + "/" + key; + if (!E.key.is_empty()) { + key = E.key + "/" + key; } Variant value; if (p_custom.has(key)) { @@ -917,7 +918,7 @@ Error ProjectSettings::save_custom(const String &p_path, const CustomMap &p_cust project_features = _trim_to_supported_features(project_features); set_setting("application/config/features", project_features); - Set<_VCSort> vclist; + RBSet<_VCSort> vclist; if (p_merge_with_current) { for (const KeyValue &G : props) { @@ -946,19 +947,19 @@ Error ProjectSettings::save_custom(const String &p_path, const CustomMap &p_cust for (const KeyValue &E : p_custom) { // Lookup global prop to store in the same order - Map::Element *global_prop = props.find(E.key); + HashMap::Iterator global_prop = props.find(E.key); _VCSort vc; vc.name = E.key; - vc.order = global_prop ? global_prop->get().order : 0xFFFFFFF; + vc.order = global_prop ? global_prop->value.order : 0xFFFFFFF; vc.type = E.value.get_type(); vc.flags = PROPERTY_USAGE_STORAGE; vclist.insert(vc); } - Map> props; + HashMap> props; - for (Set<_VCSort>::Element *E = vclist.front(); E; E = E->next()) { + for (RBSet<_VCSort>::Element *E = vclist.front(); E; E = E->next()) { String category = E->get().name; String name = E->get().name; @@ -1051,7 +1052,7 @@ void ProjectSettings::set_custom_property_info(const String &p_prop, const Prope custom_prop_info[p_prop].name = p_prop; } -const Map &ProjectSettings::get_custom_property_info() const { +const HashMap &ProjectSettings::get_custom_property_info() const { return custom_prop_info; } diff --git a/core/config/project_settings.h b/core/config/project_settings.h index 8655526edd..f8dc618cb8 100644 --- a/core/config/project_settings.h +++ b/core/config/project_settings.h @@ -34,14 +34,14 @@ #include "core/object/class_db.h" #include "core/os/thread_safe.h" #include "core/templates/hash_map.h" -#include "core/templates/set.h" +#include "core/templates/rb_set.h" class ProjectSettings : public Object { GDCLASS(ProjectSettings, Object); _THREAD_SAFE_CLASS_ public: - typedef Map CustomMap; + typedef HashMap CustomMap; static const String PROJECT_DATA_DIR_NAME_SUFFIX; enum { @@ -84,15 +84,15 @@ protected: int last_builtin_order = 0; uint64_t last_save_time = 0; - Map props; + HashMap props; String resource_path; - Map custom_prop_info; + HashMap custom_prop_info; bool disable_feature_overrides = false; bool using_datapack = false; List input_presets; - Set custom_features; - Map feature_overrides; + RBSet custom_features; + HashMap feature_overrides; HashMap autoloads; @@ -108,8 +108,8 @@ protected: Error _load_settings_binary(const String &p_path); Error _load_settings_text_or_binary(const String &p_text_path, const String &p_bin_path); - Error _save_settings_text(const String &p_file, const Map> &props, const CustomMap &p_custom = CustomMap(), const String &p_custom_features = String()); - Error _save_settings_binary(const String &p_file, const Map> &props, const CustomMap &p_custom = CustomMap(), const String &p_custom_features = String()); + Error _save_settings_text(const String &p_file, const HashMap> &props, const CustomMap &p_custom = CustomMap(), const String &p_custom_features = String()); + Error _save_settings_binary(const String &p_file, const HashMap> &props, const CustomMap &p_custom = CustomMap(), const String &p_custom_features = String()); Error _save_custom_bnd(const String &p_file); @@ -168,7 +168,7 @@ public: Error save_custom(const String &p_path = "", const CustomMap &p_custom = CustomMap(), const Vector &p_custom_features = Vector(), bool p_merge_with_current = true); Error save(); void set_custom_property_info(const String &p_prop, const PropertyInfo &p_info); - const Map &get_custom_property_info() const; + const HashMap &get_custom_property_info() const; uint64_t get_last_saved_time() { return last_save_time; } Vector get_optimizer_presets() const; -- cgit v1.2.3