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! --- modules/csg/csg.cpp | 6 +- modules/csg/csg.h | 10 +- modules/enet/enet_multiplayer_peer.h | 4 +- modules/gdscript/editor/gdscript_highlighter.h | 2 +- .../editor/gdscript_translation_parser_plugin.h | 8 +- modules/gdscript/gdscript.cpp | 110 ++++++++++----------- modules/gdscript/gdscript.h | 70 ++++++------- modules/gdscript/gdscript_analyzer.cpp | 8 +- modules/gdscript/gdscript_analyzer.h | 2 +- modules/gdscript/gdscript_byte_codegen.h | 42 ++++---- modules/gdscript/gdscript_cache.cpp | 4 +- modules/gdscript/gdscript_cache.h | 4 +- modules/gdscript/gdscript_compiler.cpp | 18 ++-- modules/gdscript/gdscript_compiler.h | 14 +-- modules/gdscript/gdscript_editor.cpp | 44 ++++----- modules/gdscript/gdscript_function.cpp | 2 +- modules/gdscript/gdscript_function.h | 2 +- modules/gdscript/gdscript_parser.cpp | 4 +- modules/gdscript/gdscript_parser.h | 12 +-- modules/gdscript/gdscript_tokenizer.h | 8 +- modules/gdscript/gdscript_vm.cpp | 4 +- .../language_server/gdscript_extend_parser.cpp | 4 +- .../language_server/gdscript_text_document.cpp | 8 +- .../language_server/gdscript_workspace.cpp | 56 +++++------ .../gdscript/language_server/gdscript_workspace.h | 6 +- modules/gdscript/language_server/lsp.hpp | 10 +- modules/gdscript/register_types.cpp | 2 +- modules/gdscript/tests/gdscript_test_runner.cpp | 4 +- .../gltf/editor/editor_scene_importer_blend.cpp | 4 +- modules/gltf/editor/editor_scene_importer_blend.h | 4 +- modules/gltf/editor/editor_scene_importer_fbx.cpp | 6 +- modules/gltf/editor/editor_scene_importer_fbx.h | 4 +- modules/gltf/editor/editor_scene_importer_gltf.cpp | 2 +- modules/gltf/editor/editor_scene_importer_gltf.h | 2 +- modules/gltf/gltf_animation.cpp | 2 +- modules/gltf/gltf_animation.h | 4 +- modules/gltf/gltf_document.cpp | 48 ++++----- modules/gltf/gltf_document.h | 14 +-- modules/gltf/gltf_skeleton.cpp | 2 +- modules/gltf/gltf_skeleton.h | 8 +- modules/gltf/gltf_skin.cpp | 12 +-- modules/gltf/gltf_skin.h | 4 +- modules/gltf/gltf_state.cpp | 2 +- modules/gltf/gltf_state.h | 22 ++--- modules/gridmap/grid_map.cpp | 21 ++-- modules/gridmap/grid_map.h | 21 ++-- modules/jsonrpc/jsonrpc.h | 2 +- modules/minimp3/resource_importer_mp3.cpp | 4 +- modules/minimp3/resource_importer_mp3.h | 4 +- modules/mono/csharp_script.cpp | 38 +++---- modules/mono/csharp_script.h | 28 +++--- modules/mono/editor/bindings_generator.cpp | 42 ++++---- modules/mono/editor/bindings_generator.h | 10 +- modules/mono/glue/base_object_glue.cpp | 4 +- modules/mono/managed_callable.cpp | 2 +- modules/mono/managed_callable.h | 2 +- modules/mono/mono_gd/gd_mono_assembly.cpp | 4 +- modules/mono/mono_gd/gd_mono_assembly.h | 4 +- modules/mono/mono_gd/gd_mono_class.cpp | 20 ++-- modules/mono/mono_gd/gd_mono_class.h | 8 +- modules/mono/mono_gd/gd_mono_utils.cpp | 2 +- modules/navigation/nav_map.cpp | 4 +- modules/navigation/nav_map.h | 2 +- modules/navigation/nav_utils.h | 12 ++- .../openxr/extensions/openxr_extension_wrapper.h | 6 +- modules/openxr/openxr_api.cpp | 22 +++-- modules/openxr/openxr_api.h | 6 +- modules/openxr/openxr_interface.cpp | 2 +- modules/raycast/lightmap_raycaster.cpp | 6 +- modules/raycast/lightmap_raycaster.h | 6 +- modules/raycast/raycast_occlusion_cull.cpp | 2 +- modules/raycast/raycast_occlusion_cull.h | 4 +- modules/raycast/static_raycaster.cpp | 6 +- modules/raycast/static_raycaster.h | 4 +- modules/regex/regex.cpp | 8 +- modules/regex/regex.h | 4 +- modules/text_server_adv/text_server_adv.cpp | 2 +- modules/text_server_adv/text_server_adv.h | 5 +- .../visual_script/editor/visual_script_editor.cpp | 64 ++++++------ .../visual_script/editor/visual_script_editor.h | 18 ++-- .../editor/visual_script_property_selector.cpp | 18 ++-- .../editor/visual_script_property_selector.h | 10 +- modules/visual_script/visual_script.cpp | 74 +++++++------- modules/visual_script/visual_script.h | 32 +++--- modules/visual_script/visual_script_nodes.cpp | 2 +- modules/vorbis/resource_importer_ogg_vorbis.cpp | 4 +- modules/vorbis/resource_importer_ogg_vorbis.h | 4 +- modules/webrtc/webrtc_multiplayer_peer.cpp | 36 +++---- modules/webrtc/webrtc_multiplayer_peer.h | 2 +- modules/websocket/websocket_multiplayer_peer.h | 2 +- modules/websocket/wsl_client.cpp | 2 +- modules/websocket/wsl_server.cpp | 2 +- 92 files changed, 607 insertions(+), 588 deletions(-) (limited to 'modules') diff --git a/modules/csg/csg.cpp b/modules/csg/csg.cpp index 177014e5a7..93533e1690 100644 --- a/modules/csg/csg.cpp +++ b/modules/csg/csg.cpp @@ -211,7 +211,7 @@ void CSGBrush::build_from_faces(const Vector &p_vertices, const Vector< int ic = p_flip_faces.size(); const bool *ri = p_flip_faces.ptr(); - Map, int> material_map; + HashMap, int> material_map; faces.resize(p_vertices.size() / 3); @@ -242,10 +242,10 @@ void CSGBrush::build_from_faces(const Vector &p_vertices, const Vector< if (mc == vc / 3) { Ref mat = rm[i]; if (mat.is_valid()) { - const Map, int>::Element *E = material_map.find(mat); + HashMap, int>::ConstIterator E = material_map.find(mat); if (E) { - f.material = E->get(); + f.material = E->value; } else { f.material = material_map.size(); material_map[mat] = f.material; diff --git a/modules/csg/csg.h b/modules/csg/csg.h index 9ff7b13a44..53a9e5d722 100644 --- a/modules/csg/csg.h +++ b/modules/csg/csg.h @@ -38,8 +38,8 @@ #include "core/math/vector3.h" #include "core/object/ref_counted.h" #include "core/templates/list.h" -#include "core/templates/map.h" #include "core/templates/oa_hash_map.h" +#include "core/templates/rb_map.h" #include "core/templates/vector.h" #include "scene/resources/material.h" @@ -139,8 +139,8 @@ struct CSGBrushOperation { Vector points; Vector faces; - Map, int> materials; - Map vertex_map; + HashMap, int> materials; + HashMap vertex_map; OAHashMap snap_cache; float vertex_snap = 0.0; @@ -184,8 +184,8 @@ struct CSGBrushOperation { }; struct Build2DFaceCollection { - Map build2DFacesA; - Map build2DFacesB; + HashMap build2DFacesA; + HashMap build2DFacesB; }; void update_faces(const CSGBrush &p_brush_a, const int p_face_idx_a, const CSGBrush &p_brush_b, const int p_face_idx_b, Build2DFaceCollection &p_collection, float p_vertex_snap); diff --git a/modules/enet/enet_multiplayer_peer.h b/modules/enet/enet_multiplayer_peer.h index e7b61169fb..18eca18e51 100644 --- a/modules/enet/enet_multiplayer_peer.h +++ b/modules/enet/enet_multiplayer_peer.h @@ -70,8 +70,8 @@ private: ConnectionStatus connection_status = CONNECTION_DISCONNECTED; - Map> hosts; - Map> peers; + HashMap> hosts; + HashMap> peers; struct Packet { ENetPacket *packet = nullptr; diff --git a/modules/gdscript/editor/gdscript_highlighter.h b/modules/gdscript/editor/gdscript_highlighter.h index 1ae0d72896..92764e3891 100644 --- a/modules/gdscript/editor/gdscript_highlighter.h +++ b/modules/gdscript/editor/gdscript_highlighter.h @@ -45,7 +45,7 @@ private: bool line_only = false; }; Vector color_regions; - Map color_region_cache; + HashMap color_region_cache; HashMap keywords; HashMap member_keywords; diff --git a/modules/gdscript/editor/gdscript_translation_parser_plugin.h b/modules/gdscript/editor/gdscript_translation_parser_plugin.h index e7b40aa367..4633a431d8 100644 --- a/modules/gdscript/editor/gdscript_translation_parser_plugin.h +++ b/modules/gdscript/editor/gdscript_translation_parser_plugin.h @@ -31,7 +31,7 @@ #ifndef GDSCRIPT_TRANSLATION_PARSER_PLUGIN_H #define GDSCRIPT_TRANSLATION_PARSER_PLUGIN_H -#include "core/templates/set.h" +#include "core/templates/rb_set.h" #include "editor/editor_translation_parser.h" #include "modules/gdscript/gdscript_parser.h" @@ -44,9 +44,9 @@ class GDScriptEditorTranslationParserPlugin : public EditorTranslationParserPlug // List of patterns used for extracting translation strings. StringName tr_func = "tr"; StringName trn_func = "tr_n"; - Set assignment_patterns; - Set first_arg_patterns; - Set second_arg_patterns; + RBSet assignment_patterns; + RBSet first_arg_patterns; + RBSet second_arg_patterns; // FileDialog patterns. StringName fd_add_filter = "add_filter"; StringName fd_set_filter = "set_filters"; diff --git a/modules/gdscript/gdscript.cpp b/modules/gdscript/gdscript.cpp index 1b4711804c..e3c9101733 100644 --- a/modules/gdscript/gdscript.cpp +++ b/modules/gdscript/gdscript.cpp @@ -340,14 +340,14 @@ bool GDScript::has_method(const StringName &p_method) const { } MethodInfo GDScript::get_method_info(const StringName &p_method) const { - const Map::Element *E = member_functions.find(p_method); + HashMap::ConstIterator E = member_functions.find(p_method); if (!E) { return MethodInfo(); } - GDScriptFunction *func = E->get(); + GDScriptFunction *func = E->value; MethodInfo mi; - mi.name = E->key(); + mi.name = E->key; for (int i = 0; i < func->get_argument_count(); i++) { mi.arguments.push_back(func->get_argument_type(i)); } @@ -359,9 +359,9 @@ MethodInfo GDScript::get_method_info(const StringName &p_method) const { bool GDScript::get_property_default_value(const StringName &p_property, Variant &r_value) const { #ifdef TOOLS_ENABLED - const Map::Element *E = member_default_values_cache.find(p_property); + HashMap::ConstIterator E = member_default_values_cache.find(p_property); if (E) { - r_value = E->get(); + r_value = E->value; return true; } @@ -427,7 +427,7 @@ void GDScript::set_source_code(const String &p_code) { } #ifdef TOOLS_ENABLED -void GDScript::_update_exports_values(Map &values, List &propnames) { +void GDScript::_update_exports_values(HashMap &values, List &propnames) { if (base_cache.is_valid()) { base_cache->_update_exports_values(values, propnames); } @@ -759,12 +759,12 @@ bool GDScript::_update_exports(bool *r_err, bool p_recursive_call, PlaceHolderSc if ((changed || p_instance_to_update) && placeholders.size()) { //hm :( // update placeholders if any - Map values; + HashMap values; List propnames; _update_exports_values(values, propnames); if (changed) { - for (Set::Element *E = placeholders.front(); E; E = E->next()) { + for (RBSet::Element *E = placeholders.front(); E; E = E->next()) { E->get()->update(propnames, values); } } else { @@ -788,9 +788,9 @@ void GDScript::update_exports() { return; } - Set copy = inheriters_cache; //might get modified + RBSet copy = inheriters_cache; //might get modified - for (Set::Element *E = copy.front(); E; E = E->next()) { + for (RBSet::Element *E = copy.front(); E; E = E->next()) { Object *id = ObjectDB::get_instance(E->get()); GDScript *s = Object::cast_to(id); if (!s) { @@ -929,7 +929,7 @@ ScriptLanguage *GDScript::get_language() const { return GDScriptLanguage::get_singleton(); } -void GDScript::get_constants(Map *p_constants) { +void GDScript::get_constants(HashMap *p_constants) { if (p_constants) { for (const KeyValue &E : constants) { (*p_constants)[E.key] = E.value; @@ -937,9 +937,9 @@ void GDScript::get_constants(Map *p_constants) { } } -void GDScript::get_members(Set *p_members) { +void GDScript::get_members(RBSet *p_members) { if (p_members) { - for (Set::Element *E = members.front(); E; E = E->next()) { + for (RBSet::Element *E = members.front(); E; E = E->next()) { p_members->insert(E->get()); } } @@ -952,11 +952,11 @@ const Vector GDScript::get_rpc_methods() const { Variant GDScript::callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { GDScript *top = this; while (top) { - Map::Element *E = top->member_functions.find(p_method); + HashMap::Iterator E = top->member_functions.find(p_method); if (E) { - ERR_FAIL_COND_V_MSG(!E->get()->is_static(), Variant(), "Can't call non-static function '" + String(p_method) + "' in script."); + ERR_FAIL_COND_V_MSG(!E->value->is_static(), Variant(), "Can't call non-static function '" + String(p_method) + "' in script."); - return E->get()->call(nullptr, p_args, p_argcount, r_error); + return E->value->call(nullptr, p_args, p_argcount, r_error); } top = top->_base; } @@ -971,17 +971,17 @@ bool GDScript::_get(const StringName &p_name, Variant &r_ret) const { const GDScript *top = this; while (top) { { - const Map::Element *E = top->constants.find(p_name); + HashMap::ConstIterator E = top->constants.find(p_name); if (E) { - r_ret = E->get(); + r_ret = E->value; return true; } } { - const Map>::Element *E = subclasses.find(p_name); + HashMap>::ConstIterator E = subclasses.find(p_name); if (E) { - r_ret = E->get(); + r_ret = E->value; return true; } } @@ -1061,7 +1061,7 @@ Error GDScript::load_source_code(const String &p_path) { return OK; } -const Map &GDScript::debug_get_member_functions() const { +const HashMap &GDScript::debug_get_member_functions() const { return member_functions; } @@ -1209,7 +1209,7 @@ void GDScript::_init_rpc_methods_properties() { } GDScript *cscript = this; - Map>::Element *sub_E = subclasses.front(); + HashMap>::Iterator sub_E = subclasses.begin(); while (cscript) { // RPC Methods for (KeyValue &E : cscript->member_functions) { @@ -1223,11 +1223,11 @@ void GDScript::_init_rpc_methods_properties() { } if (cscript != this) { - sub_E = sub_E->next(); + ++sub_E; } if (sub_E) { - cscript = sub_E->get().ptr(); + cscript = sub_E->value.ptr(); } else { cscript = nullptr; } @@ -1282,9 +1282,9 @@ GDScript::~GDScript() { bool GDScriptInstance::set(const StringName &p_name, const Variant &p_value) { //member { - const Map::Element *E = script->member_indices.find(p_name); + HashMap::Iterator E = script->member_indices.find(p_name); if (E) { - const GDScript::MemberInfo *member = &E->get(); + const GDScript::MemberInfo *member = &E->value; if (member->setter) { const Variant *val = &p_value; Callable::CallError err; @@ -1325,13 +1325,13 @@ bool GDScriptInstance::set(const StringName &p_name, const Variant &p_value) { GDScript *sptr = script.ptr(); while (sptr) { - Map::Element *E = sptr->member_functions.find(GDScriptLanguage::get_singleton()->strings._set); + HashMap::Iterator E = sptr->member_functions.find(GDScriptLanguage::get_singleton()->strings._set); if (E) { Variant name = p_name; const Variant *args[2] = { &name, &p_value }; Callable::CallError err; - Variant ret = E->get()->call(this, (const Variant **)args, 2, err); + Variant ret = E->value->call(this, (const Variant **)args, 2, err); if (err.error == Callable::CallError::CALL_OK && ret.get_type() == Variant::BOOL && ret.operator bool()) { return true; } @@ -1346,16 +1346,16 @@ bool GDScriptInstance::get(const StringName &p_name, Variant &r_ret) const { const GDScript *sptr = script.ptr(); while (sptr) { { - const Map::Element *E = script->member_indices.find(p_name); + HashMap::ConstIterator E = script->member_indices.find(p_name); if (E) { - if (E->get().getter) { + if (E->value.getter) { Callable::CallError err; - r_ret = const_cast(this)->callp(E->get().getter, nullptr, 0, err); + r_ret = const_cast(this)->callp(E->value.getter, nullptr, 0, err); if (err.error == Callable::CallError::CALL_OK) { return true; } } - r_ret = members[E->get().index]; + r_ret = members[E->value.index]; return true; //index found } } @@ -1363,9 +1363,9 @@ bool GDScriptInstance::get(const StringName &p_name, Variant &r_ret) const { { const GDScript *sl = sptr; while (sl) { - const Map::Element *E = sl->constants.find(p_name); + HashMap::ConstIterator E = sl->constants.find(p_name); if (E) { - r_ret = E->get(); + r_ret = E->value; return true; //index found } sl = sl->_base; @@ -1376,9 +1376,9 @@ bool GDScriptInstance::get(const StringName &p_name, Variant &r_ret) const { // Signals. const GDScript *sl = sptr; while (sl) { - const Map>::Element *E = sl->_signals.find(p_name); + HashMap>::ConstIterator E = sl->_signals.find(p_name); if (E) { - r_ret = Signal(this->owner, E->key()); + r_ret = Signal(this->owner, E->key); return true; //index found } sl = sl->_base; @@ -1389,14 +1389,14 @@ bool GDScriptInstance::get(const StringName &p_name, Variant &r_ret) const { // Methods. const GDScript *sl = sptr; while (sl) { - const Map::Element *E = sl->member_functions.find(p_name); + HashMap::ConstIterator E = sl->member_functions.find(p_name); if (E) { Multiplayer::RPCConfig config; config.name = p_name; if (sptr->rpc_functions.find(config) != -1) { - r_ret = Callable(memnew(GDScriptRPCCallable(this->owner, E->key()))); + r_ret = Callable(memnew(GDScriptRPCCallable(this->owner, E->key))); } else { - r_ret = Callable(this->owner, E->key()); + r_ret = Callable(this->owner, E->key); } return true; //index found } @@ -1405,13 +1405,13 @@ bool GDScriptInstance::get(const StringName &p_name, Variant &r_ret) const { } { - const Map::Element *E = sptr->member_functions.find(GDScriptLanguage::get_singleton()->strings._get); + HashMap::ConstIterator E = sptr->member_functions.find(GDScriptLanguage::get_singleton()->strings._get); if (E) { Variant name = p_name; const Variant *args[1] = { &name }; Callable::CallError err; - Variant ret = const_cast(E->get())->call(const_cast(this), (const Variant **)args, 1, err); + Variant ret = const_cast(E->value)->call(const_cast(this), (const Variant **)args, 1, err); if (err.error == Callable::CallError::CALL_OK && ret.get_type() != Variant::NIL) { r_ret = ret; return true; @@ -1449,10 +1449,10 @@ void GDScriptInstance::get_property_list(List *p_properties) const List props; while (sptr) { - const Map::Element *E = sptr->member_functions.find(GDScriptLanguage::get_singleton()->strings._get_property_list); + HashMap::ConstIterator E = sptr->member_functions.find(GDScriptLanguage::get_singleton()->strings._get_property_list); if (E) { Callable::CallError err; - Variant ret = const_cast(E->get())->call(const_cast(this), nullptr, 0, err); + Variant ret = const_cast(E->value)->call(const_cast(this), nullptr, 0, err); if (err.error == Callable::CallError::CALL_OK) { ERR_FAIL_COND_MSG(ret.get_type() != Variant::ARRAY, "Wrong type for _get_property_list, must be an array of dictionaries."); @@ -1525,7 +1525,7 @@ void GDScriptInstance::get_method_list(List *p_list) const { bool GDScriptInstance::has_method(const StringName &p_method) const { const GDScript *sptr = script.ptr(); while (sptr) { - const Map::Element *E = sptr->member_functions.find(p_method); + HashMap::ConstIterator E = sptr->member_functions.find(p_method); if (E) { return true; } @@ -1538,9 +1538,9 @@ bool GDScriptInstance::has_method(const StringName &p_method) const { Variant GDScriptInstance::callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { GDScript *sptr = script.ptr(); while (sptr) { - Map::Element *E = sptr->member_functions.find(p_method); + HashMap::Iterator E = sptr->member_functions.find(p_method); if (E) { - return E->get()->call(this, p_args, p_argcount, r_error); + return E->value->call(this, p_args, p_argcount, r_error); } sptr = sptr->_base; } @@ -1555,10 +1555,10 @@ void GDScriptInstance::notification(int p_notification) { GDScript *sptr = script.ptr(); while (sptr) { - Map::Element *E = sptr->member_functions.find(GDScriptLanguage::get_singleton()->strings._notification); + HashMap::Iterator E = sptr->member_functions.find(GDScriptLanguage::get_singleton()->strings._notification); if (E) { Callable::CallError err; - E->get()->call(this, args, 1, err); + E->value->call(this, args, 1, err); if (err.error != Callable::CallError::CALL_OK) { //print error about notification call } @@ -1882,7 +1882,7 @@ void GDScriptLanguage::reload_tool_script(const Ref