From b1b54a5fe7eda79f61304e8a760019caadde5e09 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Thu, 27 Jun 2024 17:10:01 +0200 Subject: [PATCH] impr: Drastically optimize event handler --- .../include/hex/api/event_manager.hpp | 44 ++++++++++--------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/lib/libimhex/include/hex/api/event_manager.hpp b/lib/libimhex/include/hex/api/event_manager.hpp index bbe4097e6..876644b93 100644 --- a/lib/libimhex/include/hex/api/event_manager.hpp +++ b/lib/libimhex/include/hex/api/event_manager.hpp @@ -14,18 +14,18 @@ #include -#define EVENT_DEF_IMPL(event_name, event_name_string, should_log, ...) \ - struct event_name final : public hex::impl::Event<__VA_ARGS__> { \ - constexpr static auto Id = [] { return hex::impl::EventId(event_name_string); }(); \ - constexpr static auto ShouldLog = (should_log); \ - explicit event_name(Callback func) noexcept : Event(std::move(func)) { } \ - \ - static EventManager::EventList::iterator subscribe(Event::Callback function) { return EventManager::subscribe(function); } \ - static void subscribe(void *token, Event::Callback function) { EventManager::subscribe(token, function); } \ - static void unsubscribe(const EventManager::EventList::iterator &token) noexcept { EventManager::unsubscribe(token); } \ - static void unsubscribe(void *token) noexcept { EventManager::unsubscribe(token); } \ - static void post(auto &&...args) { EventManager::post(std::forward(args)...); } \ - }; +#define EVENT_DEF_IMPL(event_name, event_name_string, should_log, ...) \ + struct event_name final : public hex::impl::Event<__VA_ARGS__> { \ + constexpr static auto Id = [] { return hex::impl::EventId(event_name_string); }(); \ + constexpr static auto ShouldLog = (should_log); \ + explicit event_name(Callback func) noexcept : Event(std::move(func)) { } \ + \ + static EventManager::EventList::iterator subscribe(Event::Callback function) { return EventManager::subscribe(std::move(function)); } \ + static void subscribe(void *token, Event::Callback function) { EventManager::subscribe(token, std::move(function)); } \ + static void unsubscribe(const EventManager::EventList::iterator &token) noexcept { EventManager::unsubscribe(token); } \ + static void unsubscribe(void *token) noexcept { EventManager::unsubscribe(token); } \ + static void post(auto &&...args) { EventManager::post(std::forward(args)...); } \ + } #define EVENT_DEF(event_name, ...) EVENT_DEF_IMPL(event_name, #event_name, true, __VA_ARGS__) #define EVENT_DEF_NO_LOG(event_name, ...) EVENT_DEF_IMPL(event_name, #event_name, false, __VA_ARGS__) @@ -58,6 +58,10 @@ namespace hex { return m_hash == other.m_hash; } + constexpr auto operator<=>(const EventId &other) const { + return m_hash <=> other.m_hash; + } + private: u32 m_hash; }; @@ -99,7 +103,7 @@ namespace hex { */ class EventManager { public: - using EventList = std::list>>; + using EventList = std::multimap>; /** * @brief Subscribes to an event @@ -112,7 +116,7 @@ namespace hex { std::scoped_lock lock(getEventMutex()); auto &events = getEvents(); - return events.insert(events.end(), std::make_pair(E::Id, std::make_unique(function))); + return events.insert({ E::Id, std::make_unique(function) }); } /** @@ -164,7 +168,7 @@ namespace hex { }); if (iter != tokenStore.end()) { - getEvents().remove(*iter->second); + getEvents().erase(iter->second); tokenStore.erase(iter); } @@ -179,10 +183,10 @@ namespace hex { static void post(auto && ...args) { std::scoped_lock lock(getEventMutex()); - for (const auto &[id, event] : getEvents()) { - if (id == E::Id) { - (*static_cast(event.get())).template call(std::forward(args)...); - } + auto [begin, end] = getEvents().equal_range(E::Id); + for (auto it = begin; it != end; ++it) { + const auto &[id, event] = *it; + (*static_cast(event.get())).template call(std::forward(args)...); } #if defined (DEBUG) @@ -276,7 +280,7 @@ namespace hex { EVENT_DEF_NO_LOG(EventFrameBegin); EVENT_DEF_NO_LOG(EventFrameEnd); EVENT_DEF_NO_LOG(EventSetTaskBarIconState, u32, u32, u32); - EVENT_DEF_NO_LOG(EventImGuiElementRendered, ImGuiID, const std::array&) + EVENT_DEF_NO_LOG(EventImGuiElementRendered, ImGuiID, const std::array&); EVENT_DEF(RequestAddInitTask, std::string, bool, std::function); EVENT_DEF(RequestAddExitTask, std::string, std::function);