Merge pull request #111358 from Ivorforce/no-variant-hasher

Remove `VariantHasher` and `VariantComparator` in favour of specialising `HashMapHasherDefault` and `HashMapComparatorDefault`.
This commit is contained in:
Thaddeus Crews 2025-10-10 10:26:12 -05:00
commit 16a11ac88b
No known key found for this signature in database
GPG key ID: 8C6E5FEB5FC03CCC
12 changed files with 24 additions and 37 deletions

View file

@ -47,7 +47,7 @@ STATIC_ASSERT_INCOMPLETE_TYPE(class, String);
struct DictionaryPrivate { struct DictionaryPrivate {
SafeRefCount refcount; SafeRefCount refcount;
Variant *read_only = nullptr; // If enabled, a pointer is used to a temporary value that is used to return read-only values. Variant *read_only = nullptr; // If enabled, a pointer is used to a temporary value that is used to return read-only values.
HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator> variant_map; HashMap<Variant, Variant, HashMapHasherDefault, StringLikeVariantComparator> variant_map;
ContainerTypeValidate typed_key; ContainerTypeValidate typed_key;
ContainerTypeValidate typed_value; ContainerTypeValidate typed_value;
Variant *typed_fallback = nullptr; // Allows a typed dictionary to return dummy values when attempting an invalid access. Variant *typed_fallback = nullptr; // Allows a typed dictionary to return dummy values when attempting an invalid access.
@ -143,7 +143,7 @@ const Variant *Dictionary::getptr(const Variant &p_key) const {
if (unlikely(!_p->typed_key.validate(key, "getptr"))) { if (unlikely(!_p->typed_key.validate(key, "getptr"))) {
return nullptr; return nullptr;
} }
HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator>::ConstIterator E(_p->variant_map.find(key)); HashMap<Variant, Variant, HashMapHasherDefault, StringLikeVariantComparator>::ConstIterator E(_p->variant_map.find(key));
if (!E) { if (!E) {
return nullptr; return nullptr;
} }
@ -156,7 +156,7 @@ Variant *Dictionary::getptr(const Variant &p_key) {
if (unlikely(!_p->typed_key.validate(key, "getptr"))) { if (unlikely(!_p->typed_key.validate(key, "getptr"))) {
return nullptr; return nullptr;
} }
HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator>::Iterator E(_p->variant_map.find(key)); HashMap<Variant, Variant, HashMapHasherDefault, StringLikeVariantComparator>::Iterator E(_p->variant_map.find(key));
if (!E) { if (!E) {
return nullptr; return nullptr;
} }
@ -171,7 +171,7 @@ Variant *Dictionary::getptr(const Variant &p_key) {
Variant Dictionary::get_valid(const Variant &p_key) const { Variant Dictionary::get_valid(const Variant &p_key) const {
Variant key = p_key; Variant key = p_key;
ERR_FAIL_COND_V(!_p->typed_key.validate(key, "get_valid"), Variant()); ERR_FAIL_COND_V(!_p->typed_key.validate(key, "get_valid"), Variant());
HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator>::ConstIterator E(_p->variant_map.find(key)); HashMap<Variant, Variant, HashMapHasherDefault, StringLikeVariantComparator>::ConstIterator E(_p->variant_map.find(key));
if (!E) { if (!E) {
return Variant(); return Variant();
@ -280,7 +280,7 @@ bool Dictionary::recursive_equal(const Dictionary &p_dictionary, int recursion_c
} }
recursion_count++; recursion_count++;
for (const KeyValue<Variant, Variant> &this_E : _p->variant_map) { for (const KeyValue<Variant, Variant> &this_E : _p->variant_map) {
HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator>::ConstIterator other_E(p_dictionary._p->variant_map.find(this_E.key)); HashMap<Variant, Variant, HashMapHasherDefault, StringLikeVariantComparator>::ConstIterator other_E(p_dictionary._p->variant_map.find(this_E.key));
if (!other_E || !this_E.value.hash_compare(other_E->value, recursion_count, false)) { if (!other_E || !this_E.value.hash_compare(other_E->value, recursion_count, false)) {
return false; return false;
} }
@ -438,7 +438,7 @@ void Dictionary::assign(const Dictionary &p_dictionary) {
} }
int size = p_dictionary._p->variant_map.size(); int size = p_dictionary._p->variant_map.size();
HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator> variant_map = HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator>(size); HashMap<Variant, Variant, HashMapHasherDefault, StringLikeVariantComparator> variant_map = HashMap<Variant, Variant, HashMapHasherDefault, StringLikeVariantComparator>(size);
Vector<Variant> key_array; Vector<Variant> key_array;
key_array.resize(size); key_array.resize(size);
@ -571,7 +571,7 @@ const Variant *Dictionary::next(const Variant *p_key) const {
} }
Variant key = *p_key; Variant key = *p_key;
ERR_FAIL_COND_V(!_p->typed_key.validate(key, "next"), nullptr); ERR_FAIL_COND_V(!_p->typed_key.validate(key, "next"), nullptr);
HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator>::Iterator E = _p->variant_map.find(key); HashMap<Variant, Variant, HashMapHasherDefault, StringLikeVariantComparator>::Iterator E = _p->variant_map.find(key);
if (!E) { if (!E) {
return nullptr; return nullptr;

View file

@ -42,7 +42,6 @@ struct ContainerType;
struct ContainerTypeValidate; struct ContainerTypeValidate;
struct DictionaryPrivate; struct DictionaryPrivate;
struct StringLikeVariantComparator; struct StringLikeVariantComparator;
struct VariantHasher;
class Dictionary { class Dictionary {
mutable DictionaryPrivate *_p; mutable DictionaryPrivate *_p;
@ -51,7 +50,7 @@ class Dictionary {
void _unref() const; void _unref() const;
public: public:
using ConstIterator = HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator>::ConstIterator; using ConstIterator = HashMap<Variant, Variant, HashMapHasherDefault, StringLikeVariantComparator>::ConstIterator;
ConstIterator begin() const; ConstIterator begin() const;
ConstIterator end() const; ConstIterator end() const;

View file

@ -884,12 +884,9 @@ Vector<Variant> varray(VarArgs... p_args) {
return Vector<Variant>{ p_args... }; return Vector<Variant>{ p_args... };
} }
struct VariantHasher { template <>
static _FORCE_INLINE_ uint32_t hash(const Variant &p_variant) { return p_variant.hash(); } struct HashMapComparatorDefault<Variant> {
}; static bool compare(const Variant &p_lhs, const Variant &p_rhs) { return p_lhs.hash_compare(p_rhs); }
struct VariantComparator {
static _FORCE_INLINE_ bool compare(const Variant &p_lhs, const Variant &p_rhs) { return p_lhs.hash_compare(p_rhs); }
}; };
struct StringLikeVariantComparator { struct StringLikeVariantComparator {

View file

@ -82,15 +82,6 @@ private:
Ref<ArrayMesh> root_mesh; Ref<ArrayMesh> root_mesh;
struct Vector3Hasher {
_ALWAYS_INLINE_ uint32_t hash(const Vector3 &p_vec3) const {
uint32_t h = hash_murmur3_one_float(p_vec3.x);
h = hash_murmur3_one_float(p_vec3.y, h);
h = hash_murmur3_one_float(p_vec3.z, h);
return h;
}
};
struct ShapeUpdateSurface { struct ShapeUpdateSurface {
Vector<Vector3> vertices; Vector<Vector3> vertices;
Vector<Vector3> normals; Vector<Vector3> normals;

View file

@ -3833,7 +3833,7 @@ void GDScriptAnalyzer::reduce_cast(GDScriptParser::CastNode *p_cast) {
} }
void GDScriptAnalyzer::reduce_dictionary(GDScriptParser::DictionaryNode *p_dictionary) { void GDScriptAnalyzer::reduce_dictionary(GDScriptParser::DictionaryNode *p_dictionary) {
HashMap<Variant, GDScriptParser::ExpressionNode *, VariantHasher, StringLikeVariantComparator> elements; HashMap<Variant, GDScriptParser::ExpressionNode *, HashMapHasherDefault, StringLikeVariantComparator> elements;
for (int i = 0; i < p_dictionary->elements.size(); i++) { for (int i = 0; i < p_dictionary->elements.size(); i++) {
const GDScriptParser::DictionaryNode::Pair &element = p_dictionary->elements[i]; const GDScriptParser::DictionaryNode::Pair &element = p_dictionary->elements[i];

View file

@ -104,7 +104,7 @@ class GDScriptByteCodeGenerator : public GDScriptCodeGenerator {
List<int> temp_stack; List<int> temp_stack;
#endif #endif
HashMap<Variant, int, VariantHasher, VariantComparator> constant_map; HashMap<Variant, int> constant_map;
RBMap<StringName, int> name_map; RBMap<StringName, int> name_map;
#ifdef TOOLS_ENABLED #ifdef TOOLS_ENABLED
Vector<StringName> named_globals; Vector<StringName> named_globals;

View file

@ -33,7 +33,7 @@
#include "core/io/compression.h" #include "core/io/compression.h"
#include "core/io/marshalls.h" #include "core/io/marshalls.h"
int GDScriptTokenizerBuffer::_token_to_binary(const Token &p_token, Vector<uint8_t> &r_buffer, int p_start, HashMap<StringName, uint32_t> &r_identifiers_map, HashMap<Variant, uint32_t, VariantHasher, VariantComparator> &r_constants_map) { int GDScriptTokenizerBuffer::_token_to_binary(const Token &p_token, Vector<uint8_t> &r_buffer, int p_start, HashMap<StringName, uint32_t> &r_identifiers_map, HashMap<Variant, uint32_t> &r_constants_map) {
int pos = p_start; int pos = p_start;
int token_type = p_token.type & TOKEN_MASK; int token_type = p_token.type & TOKEN_MASK;
@ -239,7 +239,7 @@ Error GDScriptTokenizerBuffer::set_code_buffer(const Vector<uint8_t> &p_buffer)
Vector<uint8_t> GDScriptTokenizerBuffer::parse_code_string(const String &p_code, CompressMode p_compress_mode) { Vector<uint8_t> GDScriptTokenizerBuffer::parse_code_string(const String &p_code, CompressMode p_compress_mode) {
HashMap<StringName, uint32_t> identifier_map; HashMap<StringName, uint32_t> identifier_map;
HashMap<Variant, uint32_t, VariantHasher, VariantComparator> constant_map; HashMap<Variant, uint32_t> constant_map;
Vector<uint8_t> token_buffer; Vector<uint8_t> token_buffer;
HashMap<uint32_t, uint32_t> token_lines; HashMap<uint32_t, uint32_t> token_lines;
HashMap<uint32_t, uint32_t> token_columns; HashMap<uint32_t, uint32_t> token_columns;

View file

@ -63,7 +63,7 @@ public:
HashMap<int, CommentData> dummy; HashMap<int, CommentData> dummy;
#endif // TOOLS_ENABLED #endif // TOOLS_ENABLED
static int _token_to_binary(const Token &p_token, Vector<uint8_t> &r_buffer, int p_start, HashMap<StringName, uint32_t> &r_identifiers_map, HashMap<Variant, uint32_t, VariantHasher, VariantComparator> &r_constants_map); static int _token_to_binary(const Token &p_token, Vector<uint8_t> &r_buffer, int p_start, HashMap<StringName, uint32_t> &r_identifiers_map, HashMap<Variant, uint32_t> &r_constants_map);
Token _binary_to_token(const uint8_t *p_buffer); Token _binary_to_token(const uint8_t *p_buffer);
public: public:

View file

@ -535,7 +535,7 @@ class TextServerAdvanced : public TextServerExtension {
Rect2 rect; Rect2 rect;
double baseline = 0; double baseline = 0;
}; };
HashMap<Variant, EmbeddedObject, VariantHasher, VariantComparator> objects; HashMap<Variant, EmbeddedObject> objects;
/* Shaped data */ /* Shaped data */
TextServer::Direction para_direction = DIRECTION_LTR; // Detected text direction. TextServer::Direction para_direction = DIRECTION_LTR; // Detected text direction.

View file

@ -458,7 +458,7 @@ class TextServerFallback : public TextServerExtension {
Rect2 rect; Rect2 rect;
double baseline = 0; double baseline = 0;
}; };
HashMap<Variant, EmbeddedObject, VariantHasher, VariantComparator> objects; HashMap<Variant, EmbeddedObject> objects;
/* Shaped data */ /* Shaped data */
TextServer::Direction para_direction = DIRECTION_LTR; // Detected text direction. TextServer::Direction para_direction = DIRECTION_LTR; // Detected text direction.

View file

@ -775,7 +775,7 @@ static int _nm_get_string(const String &p_string, HashMap<StringName, int> &name
return idx; return idx;
} }
static int _vm_get_variant(const Variant &p_variant, HashMap<Variant, int, VariantHasher, VariantComparator> &variant_map) { static int _vm_get_variant(const Variant &p_variant, HashMap<Variant, int> &variant_map) {
if (variant_map.has(p_variant)) { if (variant_map.has(p_variant)) {
return variant_map[p_variant]; return variant_map[p_variant];
} }
@ -785,7 +785,7 @@ static int _vm_get_variant(const Variant &p_variant, HashMap<Variant, int, Varia
return idx; return idx;
} }
Error SceneState::_parse_node(Node *p_owner, Node *p_node, int p_parent_idx, HashMap<StringName, int> &name_map, HashMap<Variant, int, VariantHasher, VariantComparator> &variant_map, HashMap<Node *, int> &node_map, HashMap<Node *, int> &nodepath_map, HashSet<int32_t> &ids_saved) { Error SceneState::_parse_node(Node *p_owner, Node *p_node, int p_parent_idx, HashMap<StringName, int> &name_map, HashMap<Variant, int> &variant_map, HashMap<Node *, int> &node_map, HashMap<Node *, int> &nodepath_map, HashSet<int32_t> &ids_saved) {
// this function handles all the work related to properly packing scenes, be it // this function handles all the work related to properly packing scenes, be it
// instantiated or inherited. // instantiated or inherited.
// given the complexity of this process, an attempt will be made to properly // given the complexity of this process, an attempt will be made to properly
@ -1133,7 +1133,7 @@ Error SceneState::_parse_node(Node *p_owner, Node *p_node, int p_parent_idx, Has
return OK; return OK;
} }
Error SceneState::_parse_connections(Node *p_owner, Node *p_node, HashMap<StringName, int> &name_map, HashMap<Variant, int, VariantHasher, VariantComparator> &variant_map, HashMap<Node *, int> &node_map, HashMap<Node *, int> &nodepath_map) { Error SceneState::_parse_connections(Node *p_owner, Node *p_node, HashMap<StringName, int> &name_map, HashMap<Variant, int> &variant_map, HashMap<Node *, int> &node_map, HashMap<Node *, int> &nodepath_map) {
// Ignore nodes that are within a scene instance. // Ignore nodes that are within a scene instance.
if (p_node != p_owner && p_node->get_owner() && p_node->get_owner() != p_owner && !p_owner->is_editable_instance(p_node->get_owner())) { if (p_node != p_owner && p_node->get_owner() && p_node->get_owner() != p_owner && !p_owner->is_editable_instance(p_node->get_owner())) {
return OK; return OK;
@ -1347,7 +1347,7 @@ Error SceneState::pack(Node *p_scene) {
Node *scene = p_scene; Node *scene = p_scene;
HashMap<StringName, int> name_map; HashMap<StringName, int> name_map;
HashMap<Variant, int, VariantHasher, VariantComparator> variant_map; HashMap<Variant, int> variant_map;
HashMap<Node *, int> node_map; HashMap<Node *, int> node_map;
HashMap<Node *, int> nodepath_map; HashMap<Node *, int> nodepath_map;
HashSet<int32_t> ids_saved; HashSet<int32_t> ids_saved;

View file

@ -90,8 +90,8 @@ class SceneState : public RefCounted {
Vector<ConnectionData> connections; Vector<ConnectionData> connections;
Error _parse_node(Node *p_owner, Node *p_node, int p_parent_idx, HashMap<StringName, int> &name_map, HashMap<Variant, int, VariantHasher, VariantComparator> &variant_map, HashMap<Node *, int> &node_map, HashMap<Node *, int> &nodepath_map, HashSet<int32_t> &ids_saved); Error _parse_node(Node *p_owner, Node *p_node, int p_parent_idx, HashMap<StringName, int> &name_map, HashMap<Variant, int> &variant_map, HashMap<Node *, int> &node_map, HashMap<Node *, int> &nodepath_map, HashSet<int32_t> &ids_saved);
Error _parse_connections(Node *p_owner, Node *p_node, HashMap<StringName, int> &name_map, HashMap<Variant, int, VariantHasher, VariantComparator> &variant_map, HashMap<Node *, int> &node_map, HashMap<Node *, int> &nodepath_map); Error _parse_connections(Node *p_owner, Node *p_node, HashMap<StringName, int> &name_map, HashMap<Variant, int> &variant_map, HashMap<Node *, int> &node_map, HashMap<Node *, int> &nodepath_map);
String path; String path;