Skip to content

Commit 2fd41b7

Browse files
committed
Replace unordered_map with AHashMap
1 parent 28e8b1a commit 2fd41b7

File tree

3 files changed

+776
-52
lines changed

3 files changed

+776
-52
lines changed

include/godot_cpp/core/class_db.hpp

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,10 @@
4444
// Needs to come after method_bind and object have been included.
4545
#include <godot_cpp/variant/callable_method_pointer.hpp>
4646

47-
#include <godot_cpp/templates/local_vector.hpp>
48-
47+
#include <godot_cpp/templates/a_hash_map.hpp>
4948
#include <list>
5049
#include <mutex>
5150
#include <set>
52-
#include <unordered_map>
53-
54-
// Needed to use StringName as key in `std::unordered_map`
55-
template <>
56-
struct std::hash<godot::StringName> {
57-
std::size_t operator()(godot::StringName const &s) const noexcept {
58-
return s.hash();
59-
}
60-
};
6151

6252
namespace godot {
6353

@@ -95,9 +85,9 @@ class ClassDB {
9585
StringName name;
9686
StringName parent_name;
9787
GDExtensionInitializationLevel level = GDEXTENSION_INITIALIZATION_SCENE;
98-
std::unordered_map<StringName, MethodBind *> method_map;
88+
AHashMap<StringName, MethodBind *> method_map;
9989
std::set<StringName> signal_names;
100-
std::unordered_map<StringName, VirtualMethod> virtual_methods;
90+
AHashMap<StringName, VirtualMethod> virtual_methods;
10191
std::set<StringName> property_names;
10292
std::set<StringName> constant_names;
10393
// Pointer to the parent custom class, if any. Will be null if the parent class is a Godot class.
@@ -106,11 +96,11 @@ class ClassDB {
10696

10797
private:
10898
// This may only contain custom classes, not Godot classes
109-
static std::unordered_map<StringName, ClassInfo> classes;
110-
static std::unordered_map<StringName, const GDExtensionInstanceBindingCallbacks *> instance_binding_callbacks;
99+
static AHashMap<StringName, ClassInfo> classes;
100+
static AHashMap<StringName, const GDExtensionInstanceBindingCallbacks *> instance_binding_callbacks;
111101
// Used to remember the custom class registration order.
112102
static LocalVector<StringName> class_register_order;
113-
static std::unordered_map<StringName, Object *> engine_singletons;
103+
static AHashMap<StringName, Object *> engine_singletons;
114104
static std::mutex engine_singletons_mutex;
115105

116106
static MethodBind *bind_methodfi(uint32_t p_flags, MethodBind *p_bind, const MethodDefinition &method_name, const void **p_defs, int p_defcount);
@@ -171,9 +161,9 @@ class ClassDB {
171161

172162
static void _register_engine_singleton(const StringName &p_class_name, Object *p_singleton) {
173163
std::lock_guard<std::mutex> lock(engine_singletons_mutex);
174-
std::unordered_map<StringName, Object *>::const_iterator i = engine_singletons.find(p_class_name);
164+
AHashMap<StringName, Object *>::ConstIterator i = engine_singletons.find(p_class_name);
175165
if (i != engine_singletons.end()) {
176-
ERR_FAIL_COND((*i).second != p_singleton);
166+
ERR_FAIL_COND((*i).value != p_singleton);
177167
return;
178168
}
179169
engine_singletons[p_class_name] = p_singleton;
@@ -243,10 +233,10 @@ void ClassDB::_register_class(bool p_virtual, bool p_exposed, bool p_runtime) {
243233
cl.name = T::get_class_static();
244234
cl.parent_name = T::get_parent_class_static();
245235
cl.level = current_level;
246-
std::unordered_map<StringName, ClassInfo>::iterator parent_it = classes.find(cl.parent_name);
236+
AHashMap<StringName, ClassInfo>::Iterator parent_it = classes.find(cl.parent_name);
247237
if (parent_it != classes.end()) {
248238
// Assign parent if it is also a custom class
249-
cl.parent_ptr = &parent_it->second;
239+
cl.parent_ptr = &parent_it->value;
250240
}
251241
classes[cl.name] = cl;
252242
class_register_order.push_back(cl.name);
@@ -340,13 +330,13 @@ MethodBind *ClassDB::bind_vararg_method(uint32_t p_flags, StringName p_name, M p
340330

341331
StringName instance_type = bind->get_instance_class();
342332

343-
std::unordered_map<StringName, ClassInfo>::iterator type_it = classes.find(instance_type);
333+
AHashMap<StringName, ClassInfo>::Iterator type_it = classes.find(instance_type);
344334
if (type_it == classes.end()) {
345335
memdelete(bind);
346336
ERR_FAIL_V_MSG(nullptr, String("Class '{0}' doesn't exist.").format(Array::make(instance_type)));
347337
}
348338

349-
ClassInfo &type = type_it->second;
339+
ClassInfo &type = type_it->value;
350340

351341
if (type.method_map.find(p_name) != type.method_map.end()) {
352342
memdelete(bind);

0 commit comments

Comments
 (0)