From f977c3979342bbf24b9921dfbccbf26273cc0797 Mon Sep 17 00:00:00 2001 From: alper gungormusler Date: Thu, 28 Nov 2024 13:27:49 -0500 Subject: [PATCH] use memory pool for performer tasks #126 --- src/engine/CMakeLists.txt | 2 ++ src/engine/config.h | 3 +++ src/engine/performer.cpp | 26 ++++++++++++-------------- src/engine/performer.h | 13 ++++++++----- 4 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/engine/CMakeLists.txt b/src/engine/CMakeLists.txt index e6e6037d..c2a49483 100644 --- a/src/engine/CMakeLists.txt +++ b/src/engine/CMakeLists.txt @@ -68,7 +68,9 @@ add_library( ) target_link_libraries( barelymusician_engine_performer + barelymusician_engine_config barelymusician_engine_event + barelymusician_engine_pool barelymusician_headeronly ) diff --git a/src/engine/config.h b/src/engine/config.h index 2e98ccc2..8be81fec 100644 --- a/src/engine/config.h +++ b/src/engine/config.h @@ -9,6 +9,9 @@ inline constexpr int kMaxInstrumentCount = 128; /// Maximum number of performers allowed in the engine. inline constexpr int kMaxPerformerCount = 128; +/// Maximum number of recurring tasks allowed per performer in the engine. +inline constexpr int kMaxRecurringTaskPerPerformerCount = 32; + } // namespace barely::internal #endif // BARELYMUSICIAN_ENGINE_CONFIG_H_ diff --git a/src/engine/performer.cpp b/src/engine/performer.cpp index 22dda157..45133b13 100644 --- a/src/engine/performer.cpp +++ b/src/engine/performer.cpp @@ -9,6 +9,7 @@ #include #include "barelymusician.h" +#include "engine/config.h" #include "engine/event.h" namespace barely::internal { @@ -24,16 +25,15 @@ void Performer::Task::SetPosition(double position) noexcept { } // NOLINTNEXTLINE(bugprone-exception-escape) -Performer::Performer(int process_order) noexcept : process_order_(process_order) {} +Performer::Performer(int process_order) noexcept + : process_order_(process_order), recurring_task_pool_(kMaxRecurringTaskPerPerformerCount) {} // NOLINTNEXTLINE(bugprone-exception-escape) Performer::Task* Performer::AddTask(const TaskEvent& task_event, double position) noexcept { - auto task = std::make_unique(*this, task_event, position); - Task* task_ptr = task.get(); - [[maybe_unused]] const bool success = - recurring_tasks_.emplace(std::pair{position, task_ptr}, std::move(task)).second; + Task* task = recurring_task_pool_.Construct(*this, task_event, position); + [[maybe_unused]] const bool success = recurring_tasks_.emplace(position, task).second; assert(success); - return task_ptr; + return task; } void Performer::CancelAllOneOffTasks() noexcept { one_off_tasks_.clear(); } @@ -47,7 +47,7 @@ std::optional Performer::GetDurationToNextTask() const noexcept { std::optional next_task_position = std::nullopt; if (const auto next_recurring_task = GetNextRecurringTask(); next_recurring_task != recurring_tasks_.end()) { - next_task_position = next_recurring_task->first.first; + next_task_position = next_recurring_task->first; if (is_looping_ && (*next_task_position < position_ || (last_processed_recurring_task_it_ && *next_recurring_task <= **last_processed_recurring_task_it_))) { @@ -105,8 +105,7 @@ void Performer::ProcessNextTaskAtPosition() noexcept { } void Performer::RemoveTask(Task* task) noexcept { - if (last_processed_recurring_task_it_ && - (*last_processed_recurring_task_it_)->second.get() == task) { + if (last_processed_recurring_task_it_ && (*last_processed_recurring_task_it_)->second == task) { const auto recurring_task_it = *last_processed_recurring_task_it_; PrevLastProcessedRecurringTaskIt(); recurring_tasks_.erase(recurring_task_it); @@ -186,12 +185,11 @@ void Performer::SetPosition(double position) noexcept { } void Performer::SetTaskPosition(Task* task, double position) noexcept { - if (last_processed_recurring_task_it_ && - task == (*last_processed_recurring_task_it_)->second.get()) { + if (last_processed_recurring_task_it_ && task == (*last_processed_recurring_task_it_)->second) { PrevLastProcessedRecurringTaskIt(); } auto node = recurring_tasks_.extract({task->GetPosition(), task}); - node.key().first = position; + node.value().first = position; recurring_tasks_.insert(std::move(node)); } @@ -215,12 +213,12 @@ void Performer::Update(double duration) noexcept { } } -Performer::RecurringTaskMap::const_iterator Performer::GetNextRecurringTask() const noexcept { +Performer::RecurringTaskSet::const_iterator Performer::GetNextRecurringTask() const noexcept { auto next_it = last_processed_recurring_task_it_ ? std::next(*last_processed_recurring_task_it_) : recurring_tasks_.lower_bound({position_, nullptr}); if (is_looping_ && (next_it == recurring_tasks_.end() || - next_it->first.first >= loop_begin_position_ + loop_length_)) { + next_it->first >= loop_begin_position_ + loop_length_)) { // Loop back to the beginning. next_it = recurring_tasks_.lower_bound({loop_begin_position_, nullptr}); } diff --git a/src/engine/performer.h b/src/engine/performer.h index 849209e1..7b3b5785 100644 --- a/src/engine/performer.h +++ b/src/engine/performer.h @@ -5,10 +5,12 @@ #include #include #include +#include #include #include "barelymusician.h" #include "engine/event.h" +#include "engine/pool.h" namespace barely::internal { @@ -148,11 +150,11 @@ class Performer { void Update(double duration) noexcept; private: - // Recurring task map alias. - using RecurringTaskMap = std::map, std::unique_ptr>; + // Recurring task set alias. + using RecurringTaskSet = std::set>; // Returns an iterator to the next recurring task to process. - [[nodiscard]] RecurringTaskMap::const_iterator GetNextRecurringTask() const noexcept; + [[nodiscard]] RecurringTaskSet::const_iterator GetNextRecurringTask() const noexcept; // Loops around a given `position`. [[nodiscard]] double LoopAround(double position) const noexcept; @@ -180,10 +182,11 @@ class Performer { // Map of tasks. std::multimap> one_off_tasks_; - RecurringTaskMap recurring_tasks_; + Pool recurring_task_pool_; + RecurringTaskSet recurring_tasks_; // Last processed recurring task iterator. - std::optional last_processed_recurring_task_it_; + std::optional last_processed_recurring_task_it_; }; } // namespace barely::internal