impr: Speed up initial resource loading and event handling slightly

This commit is contained in:
WerWolv
2025-08-02 01:14:00 +02:00
parent 1e18abc598
commit f73866b86e
12 changed files with 44 additions and 27 deletions

View File

@@ -9,7 +9,6 @@
#include <map>
#include <string_view>
#include <hex/api/imhex_api.hpp>
#include <hex/helpers/logger.hpp>
#include <wolv/types/type_name.hpp>
@@ -90,9 +89,9 @@ EXPORT_MODULE namespace hex {
explicit Event(Callback func) noexcept : m_func(std::move(func)) { }
template<typename E>
void call(Params... params) const {
void call(auto&& ... params) const {
try {
m_func(params...);
m_func(std::forward<decltype(params)>(params)...);
} catch (const std::exception &e) {
log::error("An exception occurred while handling event {}: {}", wolv::type::getTypeName<E>(), e.what());
throw;
@@ -125,8 +124,8 @@ EXPORT_MODULE namespace hex {
* @return Token to unsubscribe from the event
*/
template<impl::EventType E>
static EventList::iterator subscribe(typename E::Callback function) {
std::scoped_lock lock(getEventMutex());
static EventList::iterator subscribe(E::Callback function) {
std::lock_guard lock(getEventMutex());
auto &events = getEvents();
return events.insert({ E::Id, std::make_unique<E>(function) });
@@ -139,15 +138,15 @@ EXPORT_MODULE namespace hex {
* @param function Function to call when the event is posted
*/
template<impl::EventType E>
static void subscribe(void *token, typename E::Callback function) {
std::scoped_lock lock(getEventMutex());
static void subscribe(void *token, E::Callback function) {
std::lock_guard lock(getEventMutex());
if (isAlreadyRegistered(token, E::Id)) {
log::fatal("The token '{}' has already registered the same event ('{}')", token, wolv::type::getTypeName<E>());
return;
}
getTokenStore().insert({ token, subscribe<E>(function) });
getTokenStore().insert({ token, subscribe<E>(std::move(function)) });
}
/**
@@ -155,7 +154,7 @@ EXPORT_MODULE namespace hex {
* @param token Token returned by subscribe
*/
static void unsubscribe(const EventList::iterator &token) noexcept {
std::scoped_lock lock(getEventMutex());
std::lock_guard lock(getEventMutex());
getEvents().erase(token);
}
@@ -167,7 +166,7 @@ EXPORT_MODULE namespace hex {
*/
template<impl::EventType E>
static void unsubscribe(void *token) noexcept {
std::scoped_lock lock(getEventMutex());
std::lock_guard lock(getEventMutex());
unsubscribe(token, E::Id);
}
@@ -179,9 +178,9 @@ EXPORT_MODULE namespace hex {
*/
template<impl::EventType E>
static void post(auto && ...args) {
std::scoped_lock lock(getEventMutex());
std::lock_guard lock(getEventMutex());
auto [begin, end] = getEvents().equal_range(E::Id);
const auto &[begin, end] = getEvents().equal_range(E::Id);
for (auto it = begin; it != end; ++it) {
const auto &[id, event] = *it;
(*static_cast<E *const>(event.get())).template call<E>(std::forward<decltype(args)>(args)...);
@@ -197,7 +196,7 @@ EXPORT_MODULE namespace hex {
* @brief Unsubscribe all subscribers from all events
*/
static void clear() noexcept {
std::scoped_lock lock(getEventMutex());
std::lock_guard lock(getEventMutex());
getEvents().clear();
getTokenStore().clear();

View File

@@ -4,6 +4,7 @@
/* Forward declarations */
struct GLFWwindow;
using ImGuiID = unsigned int;
namespace hex { class View; }
/* GUI events definitions */

View File

@@ -1,5 +1,6 @@
#pragma once
#include <hex/api/imhex_api.hpp>
#include <hex/api/event_manager.hpp>
#include <hex/helpers/patches.hpp>

View File

@@ -1,6 +1,7 @@
#pragma once
#include <hex/api/event_manager.hpp>
#include <hex/helpers/semantic_version.hpp>
struct ImGuiTestEngine;

View File

@@ -5,6 +5,11 @@
/* Provider events definitions */
namespace hex {
namespace prv {
class Provider;
}
/**
* @brief Called when the provider is created.
* This event is responsible for (optionally) initializing the provider and calling EventProviderOpened

View File

@@ -1,6 +1,7 @@
#pragma once
#include <hex.hpp>
#include <hex/api/imhex_api.hpp>
#include <hex/api/event_manager.hpp>
/* Forward declarations */