From eb6b85276224ceb5420f39e4327603b28d7ece1b Mon Sep 17 00:00:00 2001 From: GreenCrowDev Date: Thu, 19 Dec 2024 17:40:02 +0100 Subject: [PATCH] Optimize dictionary lookup in `register_state` and `get_state` --- src/core/fsm.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/core/fsm.cpp b/src/core/fsm.cpp index f2eb6aa..4ec7ef5 100644 --- a/src/core/fsm.cpp +++ b/src/core/fsm.cpp @@ -38,9 +38,9 @@ void FSM::register_state(const String &p_name, const Ref &p_state) { NFSMG_ASSERT_RETURN_MSG(!fsm.is_locked(), "States cannot be registered while the FSM is locked."); NFSMG_ASSERT_RETURN_MSG(!p_name.is_empty(), "State name cannot be empty."); NFSMG_ASSERT_RETURN_MSG(!p_state.is_null(), "State cannot be null."); - NFSMG_ASSERT_RETURN_MSG(!state_map.has(p_name), ("State '" + to_std_string(p_name) + "' is already registered.").c_str()); - state_map[p_name] = p_state; + Ref map_state = state_map.get_or_add(p_name, p_state); + NFSMG_ASSERT_RETURN_MSG(map_state == p_state, ("State '" + to_std_string(p_name) + "' is already registered.").c_str()); std::shared_ptr state = std::make_shared(); state->add_on_enter_callback(to_std_string(p_name), [p_state]() { p_state->emit_signal("on_enter"); }); @@ -52,9 +52,9 @@ void FSM::register_state(const String &p_name, const Ref &p_state) { Ref FSM::get_state(const String &p_name) { NFSMG_ASSERT_RETURN_V_MSG(!p_name.is_empty(), Ref(), "State name cannot be empty."); - NFSMG_ASSERT_RETURN_V_MSG(state_map.has(p_name), Ref(), ("State '" + to_std_string(p_name) + "' does not exist.").c_str()); - - return state_map[p_name]; + Ref state = state_map.get(p_name, Ref()); + NFSMG_ASSERT_RETURN_V_MSG(!state.is_null(), Ref(), ("State '" + to_std_string(p_name) + "' does not exist.").c_str()); + return state; } void FSM::add_transition(const String &p_state, const String &p_event, const String &p_target) {