Skip to content

Commit

Permalink
use memory pool for performer tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
anokta committed Nov 28, 2024
1 parent 2ed965b commit f977c39
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
2 changes: 2 additions & 0 deletions src/engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ add_library(
)
target_link_libraries(
barelymusician_engine_performer
barelymusician_engine_config
barelymusician_engine_event
barelymusician_engine_pool
barelymusician_headeronly
)

Expand Down
3 changes: 3 additions & 0 deletions src/engine/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_
26 changes: 12 additions & 14 deletions src/engine/performer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <utility>

#include "barelymusician.h"
#include "engine/config.h"
#include "engine/event.h"

namespace barely::internal {
Expand All @@ -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<Task>(*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(); }
Expand All @@ -47,7 +47,7 @@ std::optional<double> Performer::GetDurationToNextTask() const noexcept {
std::optional<double> 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_))) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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));
}

Expand All @@ -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});
}
Expand Down
13 changes: 8 additions & 5 deletions src/engine/performer.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
#include <map>
#include <memory>
#include <optional>
#include <set>
#include <utility>

#include "barelymusician.h"
#include "engine/event.h"
#include "engine/pool.h"

namespace barely::internal {

Expand Down Expand Up @@ -148,11 +150,11 @@ class Performer {
void Update(double duration) noexcept;

private:
// Recurring task map alias.
using RecurringTaskMap = std::map<std::pair<double, Task*>, std::unique_ptr<Task>>;
// Recurring task set alias.
using RecurringTaskSet = std::set<std::pair<double, Task*>>;

// 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;
Expand Down Expand Up @@ -180,10 +182,11 @@ class Performer {

// Map of tasks.
std::multimap<double, Event<TaskEvent>> one_off_tasks_;
RecurringTaskMap recurring_tasks_;
Pool<Task> recurring_task_pool_;
RecurringTaskSet recurring_tasks_;

// Last processed recurring task iterator.
std::optional<RecurringTaskMap::const_iterator> last_processed_recurring_task_it_;
std::optional<RecurringTaskSet::const_iterator> last_processed_recurring_task_it_;
};

} // namespace barely::internal
Expand Down

0 comments on commit f977c39

Please sign in to comment.