From 02fc6be047bcc73887ac149446a16518b2c40978 Mon Sep 17 00:00:00 2001 From: Lukas Tenbrink Date: Wed, 3 Dec 2025 01:02:26 +0100 Subject: [PATCH] Make `NodePath` always immediately calculate its own hash, to avoid multithreading and unforeseen performance issues. --- core/string/node_path.cpp | 25 ++++++++----------------- core/string/node_path.h | 15 ++------------- 2 files changed, 10 insertions(+), 30 deletions(-) diff --git a/core/string/node_path.cpp b/core/string/node_path.cpp index 0bd062874e98..201c7b138bbc 100644 --- a/core/string/node_path.cpp +++ b/core/string/node_path.cpp @@ -45,18 +45,9 @@ void NodePath::_update_hash_cache() const { h = h ^ ssn[i].hash(); } - data->hash_cache_valid = true; data->hash_cache = h; } -void NodePath::prepend_period() { - if (data->path.size() && data->path[0].operator String() != ".") { - data->path.insert(0, "."); - data->concatenated_path = StringName(); - data->hash_cache_valid = false; - } -} - bool NodePath::is_absolute() const { if (!data) { return false; @@ -117,10 +108,8 @@ bool NodePath::operator==(const NodePath &p_path) const { return false; } - if (data->hash_cache_valid && p_path.data->hash_cache_valid) { - if (data->hash_cache != p_path.data->hash_cache) { - return false; - } + if (data->hash_cache != p_path.data->hash_cache) { + return false; } if (data->absolute != p_path.data->absolute) { @@ -360,7 +349,7 @@ void NodePath::simplify() { } } data->concatenated_path = StringName(); - data->hash_cache_valid = false; + _update_hash_cache(); } NodePath NodePath::simplified() const { @@ -378,7 +367,7 @@ NodePath::NodePath(const Vector &p_path, bool p_absolute) { data->refcount.init(); data->absolute = p_absolute; data->path = p_path; - data->hash_cache_valid = false; + _update_hash_cache(); } NodePath::NodePath(const Vector &p_path, const Vector &p_subpath, bool p_absolute) { @@ -391,7 +380,7 @@ NodePath::NodePath(const Vector &p_path, const Vector &p data->absolute = p_absolute; data->path = p_path; data->subpath = p_subpath; - data->hash_cache_valid = false; + _update_hash_cache(); } NodePath::NodePath(const NodePath &p_path) { @@ -455,9 +444,9 @@ NodePath::NodePath(const String &p_path) { data->refcount.init(); data->absolute = absolute; data->subpath = subpath; - data->hash_cache_valid = false; if (slices == 0) { + _update_hash_cache(); return; } data->path.resize(slices); @@ -478,6 +467,8 @@ NodePath::NodePath(const String &p_path) { last_is_slash = false; } } + + _update_hash_cache(); } NodePath::~NodePath() { diff --git a/core/string/node_path.h b/core/string/node_path.h index 9ba10d4451ce..d27b669598cb 100644 --- a/core/string/node_path.h +++ b/core/string/node_path.h @@ -43,8 +43,7 @@ class [[nodiscard]] NodePath { StringName concatenated_path; StringName concatenated_subpath; bool absolute; - mutable bool hash_cache_valid; - mutable uint32_t hash_cache; + uint32_t hash_cache; }; mutable Data *data = nullptr; @@ -68,17 +67,7 @@ class [[nodiscard]] NodePath { NodePath rel_path_to(const NodePath &p_np) const; NodePath get_as_property_path() const; - void prepend_period(); - - _FORCE_INLINE_ uint32_t hash() const { - if (!data) { - return 0; - } - if (!data->hash_cache_valid) { - _update_hash_cache(); - } - return data->hash_cache; - } + _FORCE_INLINE_ uint32_t hash() const { return data ? data->hash_cache : 0; } explicit operator String() const; bool is_empty() const;