Skip to content

Commit

Permalink
rework event system: avoid overhead for events with no listeners, sim…
Browse files Browse the repository at this point in the history
…plify
  • Loading branch information
Felk committed Oct 25, 2023
1 parent 608c3ab commit 9f0d525
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 279 deletions.
46 changes: 14 additions & 32 deletions Source/Core/Core/API/Events.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include <functional>
#include <mutex>
#include <map>
#include <ranges>

#include "Common/Assert.h"
#include "Core/Core.h"
Expand Down Expand Up @@ -59,6 +61,9 @@ template <typename T>
struct ListenerID
{
u64 value;

bool operator==(const ListenerID& o) const { return value == o.value; }
bool operator<(const ListenerID& o) const { return value < o.value; }
};

// an event container manages a single event type
Expand All @@ -68,7 +73,7 @@ class EventContainer final
public:
bool HasListeners()
{
return !m_listener_pairs.empty() || !m_one_time_listeners.empty();
return !m_listeners.empty();
}

void EmitEvent(T evt)
Expand All @@ -93,40 +98,24 @@ class EventContainer final
// Just to be sure, have some guards against concurrent modifications.
std::lock_guard lock{m_listeners_iterate_mutex};
// avoid concurrent modification issues by iterating over a copy
std::vector<std::pair<ListenerID<T>, Listener<T>>> listener_pairs = m_listener_pairs;
for (auto& listener_pair : listener_pairs)
listener_pair.second(evt);
// avoid concurrent modification issues by performing a swap
// with an fresh empty vector.
std::vector<Listener<T>> one_time_listeners;
std::swap(one_time_listeners, m_one_time_listeners);
for (auto& listener : one_time_listeners)
auto view = m_listeners | std::views::values;
for (const auto copy = std::vector<Listener<T>>(view.begin(), view.end());
const Listener<T>& listener : copy)
{
listener(evt);
}
}

ListenerID<T> ListenEvent(Listener<T> listener)
{
auto id = ListenerID<T>{m_next_listener_id++};
m_listener_pairs.emplace_back(std::pair<ListenerID<T>, Listener<T>>(id, std::move(listener)));
m_listeners[id] = std::move(listener);
return id;
}

bool UnlistenEvent(ListenerID<T> listener_id)
{
for (auto it = m_listener_pairs.begin(); it != m_listener_pairs.end(); ++it)
{
if (it->first.value == listener_id.value)
{
m_listener_pairs.erase(it);
return true;
}
}
return false;
}

void ListenEventOnce(Listener<T> listener)
{
m_one_time_listeners.emplace_back(std::move(listener));
return m_listeners.erase(listener_id) > 0;
}

void TickListeners()
Expand All @@ -135,8 +124,7 @@ class EventContainer final
}
private:
std::mutex m_listeners_iterate_mutex{};
std::vector<std::pair<ListenerID<T>, Listener<T>>> m_listener_pairs{};
std::vector<Listener<T>> m_one_time_listeners{};
std::map<ListenerID<T>, Listener<T>> m_listeners{};
u64 m_next_listener_id = 0;
};

Expand Down Expand Up @@ -177,12 +165,6 @@ class GenericEventHub final
return GetEventContainer<T>().UnlistenEvent(listener_id);
}

template <typename T>
void ListenEventOnce(Listener<T> listener)
{
GetEventContainer<T>().ListenEventOnce(listener);
}

void TickAllListeners()
{
std::apply([](auto&&... arg) { (arg.TickListeners(), ...); }, m_event_containers);
Expand Down
2 changes: 0 additions & 2 deletions Source/Core/Scripting/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
add_library(scripting
ScriptingEngine.cpp
ScriptingEngine.h
Python/coroutine.cpp
Python/coroutine.h
Python/PyScriptingBackend.cpp
Python/PyScriptingBackend.h
Python/Modules/controllermodule.cpp
Expand Down
Loading

0 comments on commit 9f0d525

Please sign in to comment.