From 5538850d87a9fcff8f3ec9d0ceaef919f6319cea Mon Sep 17 00:00:00 2001 From: Thaddeus Crews Date: Sat, 31 Aug 2024 10:22:57 -0500 Subject: [PATCH] Core: Convert `Pair`/`KeyValue` to `constexpr` --- core/math/a_star.h | 2 +- core/os/os.h | 4 +- core/templates/cowdata.h | 4 +- core/templates/hashfuncs.h | 8 ++++ core/templates/pair.h | 92 ++++++++++++++------------------------ modules/fbx/fbx_state.h | 2 +- scene/main/viewport.cpp | 2 +- scene/main/viewport.h | 2 +- 8 files changed, 51 insertions(+), 65 deletions(-) diff --git a/core/math/a_star.h b/core/math/a_star.h index d7e0f3fd6ce..1351dd5b569 100644 --- a/core/math/a_star.h +++ b/core/math/a_star.h @@ -89,7 +89,7 @@ class AStar3D : public RefCounted { unsigned char direction = NONE; static uint32_t hash(const Segment &p_seg) { - return PairHash().hash(p_seg.key); + return HashMapHasherDefault::hash(p_seg.key); } bool operator==(const Segment &p_s) const { return key == p_s.key; } diff --git a/core/os/os.h b/core/os/os.h index 140449c8984..b70516436e9 100644 --- a/core/os/os.h +++ b/core/os/os.h @@ -77,8 +77,8 @@ class OS { // For tracking benchmark data bool use_benchmark = false; String benchmark_file; - HashMap, uint64_t, PairHash> benchmark_marks_from; - HashMap, double, PairHash> benchmark_marks_final; + HashMap, uint64_t> benchmark_marks_from; + HashMap, double> benchmark_marks_final; protected: void _set_logger(CompositeLogger *p_logger); diff --git a/core/templates/cowdata.h b/core/templates/cowdata.h index a0fdd3f716f..1f13a5fd075 100644 --- a/core/templates/cowdata.h +++ b/core/templates/cowdata.h @@ -40,7 +40,9 @@ static_assert(std::is_trivially_destructible_v>); -GODOT_GCC_WARNING_PUSH_AND_IGNORE("-Wplacement-new") // Silence a false positive warning (see GH-52119). +GODOT_GCC_WARNING_PUSH +GODOT_GCC_WARNING_IGNORE("-Wplacement-new") // Silence a false positive warning (see GH-52119). +GODOT_GCC_WARNING_IGNORE("-Wmaybe-uninitialized") // False positive raised when using constexpr. template class CowData { diff --git a/core/templates/hashfuncs.h b/core/templates/hashfuncs.h index 781269ec9b3..530ebbcc6e1 100644 --- a/core/templates/hashfuncs.h +++ b/core/templates/hashfuncs.h @@ -52,6 +52,7 @@ #include "core/string/node_path.h" #include "core/string/string_name.h" #include "core/string/ustring.h" +#include "core/templates/pair.h" #include "core/templates/rid.h" #include "core/typedefs.h" @@ -324,6 +325,13 @@ struct HashMapHasherDefault { template static _FORCE_INLINE_ uint32_t hash(const Ref &p_ref) { return hash_one_uint64((uint64_t)p_ref.operator->()); } + template + static _FORCE_INLINE_ uint32_t hash(const Pair &p_pair) { + uint64_t h1 = hash(p_pair.first); + uint64_t h2 = hash(p_pair.second); + return hash_one_uint64((h1 << 32) | h2); + } + static _FORCE_INLINE_ uint32_t hash(const String &p_string) { return p_string.hash(); } static _FORCE_INLINE_ uint32_t hash(const char *p_cstr) { return hash_djb2(p_cstr); } static _FORCE_INLINE_ uint32_t hash(const wchar_t p_wchar) { return hash_fmix32(uint32_t(p_wchar)); } diff --git a/core/templates/pair.h b/core/templates/pair.h index a39ed4aa7a1..c87662caf98 100644 --- a/core/templates/pair.h +++ b/core/templates/pair.h @@ -30,50 +30,29 @@ #pragma once -#include "core/templates/hashfuncs.h" #include "core/typedefs.h" + template struct Pair { - F first; - S second; + F first{}; + S second{}; - Pair() : - first(), - second() { - } + constexpr Pair() = default; + constexpr Pair(const F &p_first, const S &p_second) : + first(p_first), second(p_second) {} - Pair(F p_first, const S &p_second) : - first(p_first), - second(p_second) { - } + constexpr bool operator==(const Pair &p_other) const { return first == p_other.first && second == p_other.second; } + constexpr bool operator!=(const Pair &p_other) const { return first != p_other.first || second != p_other.second; } + constexpr bool operator<(const Pair &p_other) const { return first == p_other.first ? (second < p_other.second) : (first < p_other.first); } + constexpr bool operator<=(const Pair &p_other) const { return first == p_other.first ? (second <= p_other.second) : (first < p_other.first); } + constexpr bool operator>(const Pair &p_other) const { return first == p_other.first ? (second > p_other.second) : (first > p_other.first); } + constexpr bool operator>=(const Pair &p_other) const { return first == p_other.first ? (second >= p_other.second) : (first > p_other.first); } }; -template -bool operator==(const Pair &pair, const Pair &other) { - return (pair.first == other.first) && (pair.second == other.second); -} - -template -bool operator!=(const Pair &pair, const Pair &other) { - return (pair.first != other.first) || (pair.second != other.second); -} - template struct PairSort { - bool operator()(const Pair &A, const Pair &B) const { - if (A.first != B.first) { - return A.first < B.first; - } - return A.second < B.second; - } -}; - -template -struct PairHash { - static uint32_t hash(const Pair &P) { - uint64_t h1 = HashMapHasherDefault::hash(P.first); - uint64_t h2 = HashMapHasherDefault::hash(P.second); - return hash_one_uint64((h1 << 32) | h2); + constexpr bool operator()(const Pair &p_lhs, const Pair &p_rhs) const { + return p_lhs < p_rhs; } }; @@ -83,34 +62,31 @@ struct is_zero_constructible> : std::conjunction struct KeyValue { - const K key; - V value; + const K key{}; + V value{}; - void operator=(const KeyValue &p_kv) = delete; - _FORCE_INLINE_ KeyValue(const KeyValue &p_kv) : - key(p_kv.key), - value(p_kv.value) { - } - _FORCE_INLINE_ KeyValue(const K &p_key, const V &p_value) : - key(p_key), - value(p_value) { - } + KeyValue &operator=(const KeyValue &p_kv) = delete; + KeyValue &operator=(KeyValue &&p_kv) = delete; + + constexpr KeyValue(const KeyValue &p_kv) = default; + constexpr KeyValue(KeyValue &&p_kv) = default; + constexpr KeyValue(const K &p_key, const V &p_value) : + key(p_key), value(p_value) {} + constexpr KeyValue(const Pair &p_pair) : + key(p_pair.first), value(p_pair.second) {} + + constexpr bool operator==(const KeyValue &p_other) const { return key == p_other.key && value == p_other.value; } + constexpr bool operator!=(const KeyValue &p_other) const { return key != p_other.key || value != p_other.value; } + constexpr bool operator<(const KeyValue &p_other) const { return key == p_other.key ? (value < p_other.value) : (key < p_other.key); } + constexpr bool operator<=(const KeyValue &p_other) const { return key == p_other.key ? (value <= p_other.value) : (key < p_other.key); } + constexpr bool operator>(const KeyValue &p_other) const { return key == p_other.key ? (value > p_other.value) : (key > p_other.key); } + constexpr bool operator>=(const KeyValue &p_other) const { return key == p_other.key ? (value >= p_other.value) : (key > p_other.key); } }; -template -bool operator==(const KeyValue &pair, const KeyValue &other) { - return (pair.key == other.key) && (pair.value == other.value); -} - -template -bool operator!=(const KeyValue &pair, const KeyValue &other) { - return (pair.key != other.key) || (pair.value != other.value); -} - template struct KeyValueSort { - bool operator()(const KeyValue &A, const KeyValue &B) const { - return A.key < B.key; + constexpr bool operator()(const KeyValue &p_lhs, const KeyValue &p_rhs) const { + return p_lhs.key < p_rhs.key; } }; diff --git a/modules/fbx/fbx_state.h b/modules/fbx/fbx_state.h index 0472a07670e..dff450c76f0 100644 --- a/modules/fbx/fbx_state.h +++ b/modules/fbx/fbx_state.h @@ -49,7 +49,7 @@ class FBXState : public GLTFState { bool allow_geometry_helper_nodes = false; HashMap alpha_mode_cache; - HashMap, GLTFTextureIndex, PairHash> albedo_transparency_textures; + HashMap, GLTFTextureIndex> albedo_transparency_textures; Vector skin_indices; Vector original_skin_indices; diff --git a/scene/main/viewport.cpp b/scene/main/viewport.cpp index f66134c67d8..c32afcee55a 100644 --- a/scene/main/viewport.cpp +++ b/scene/main/viewport.cpp @@ -896,7 +896,7 @@ void Viewport::_process_picking() { send_event = false; } } - HashMap, uint64_t, PairHash>::Iterator SF = physics_2d_shape_mouseover.find(Pair(res[i].collider_id, res[i].shape)); + HashMap, uint64_t>::Iterator SF = physics_2d_shape_mouseover.find(Pair(res[i].collider_id, res[i].shape)); if (!SF) { physics_2d_shape_mouseover.insert(Pair(res[i].collider_id, res[i].shape), frame); co->_mouse_shape_enter(res[i].shape); diff --git a/scene/main/viewport.h b/scene/main/viewport.h index e7b295c4503..9744ff09e2d 100644 --- a/scene/main/viewport.h +++ b/scene/main/viewport.h @@ -751,7 +751,7 @@ private: // Collider to frame HashMap physics_2d_mouseover; // Collider & shape to frame - HashMap, uint64_t, PairHash> physics_2d_shape_mouseover; + HashMap, uint64_t> physics_2d_shape_mouseover; // Cleans up colliders corresponding to old frames or all of them. void _cleanup_mouseover_colliders(bool p_clean_all_frames, bool p_paused_only, uint64_t p_frame_reference = 0); #endif // PHYSICS_2D_DISABLED