Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: OTSYS_TIME cached #1853

Merged
merged 4 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/game/scheduling/dispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ Dispatcher &Dispatcher::getInstance() {
}

void Dispatcher::init() {
updateClock();
UPDATE_OTSYS_TIME();

threadPool.addLoad([this] {
std::unique_lock asyncLock(dummyMutex);

while (!threadPool.getIoContext().stopped()) {
updateClock();
UPDATE_OTSYS_TIME();

executeEvents();
executeScheduledEvents();
Expand Down Expand Up @@ -104,7 +104,7 @@ void Dispatcher::executeScheduledEvents() {
auto it = scheduledTasks.begin();
while (it != scheduledTasks.end()) {
const auto &task = *it;
if (task->getTime() > Task::TIME_NOW) {
if (task->getTime() > OTSYS_TIME()) {
break;
}

Expand Down Expand Up @@ -168,17 +168,17 @@ void Dispatcher::mergeEvents() {
checkPendingTasks();
}

std::chrono::nanoseconds Dispatcher::timeUntilNextScheduledTask() const {
static constexpr auto CHRONO_NANO_0 = std::chrono::nanoseconds(0);
static constexpr auto CHRONO_MILI_MAX = std::chrono::milliseconds::max();
std::chrono::milliseconds Dispatcher::timeUntilNextScheduledTask() const {
constexpr auto CHRONO_0 = std::chrono::milliseconds(0);
constexpr auto CHRONO_MILI_MAX = std::chrono::milliseconds::max();

if (scheduledTasks.empty()) {
return CHRONO_MILI_MAX;
}

const auto &task = *scheduledTasks.begin();
const auto timeRemaining = task->getTime() - Task::TIME_NOW;
return std::max<std::chrono::nanoseconds>(timeRemaining, CHRONO_NANO_0);
const auto timeRemaining = std::chrono::milliseconds(task->getTime() - OTSYS_TIME());
return std::max<std::chrono::milliseconds>(timeRemaining, CHRONO_0);
}

void Dispatcher::addEvent(std::function<void(void)> &&f, std::string_view context, uint32_t expiresAfterMs) {
Expand Down
9 changes: 2 additions & 7 deletions src/game/scheduling/dispatcher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ enum class DispatcherType : uint8_t {

struct DispatcherContext {
bool isOn() const {
return Task::TIME_NOW != SYSTEM_TIME_ZERO;
return OTSYS_TIME() != 0;
}

bool isGroup(const TaskGroup _group) const {
Expand Down Expand Up @@ -134,11 +134,6 @@ class Dispatcher {
private:
thread_local static DispatcherContext dispacherContext;

// Update Time Cache
static void updateClock() {
Task::TIME_NOW = std::chrono::system_clock::now();
}

const auto &getThreadTask() const {
return threads[ThreadPool::getThreadId()];
}
Expand All @@ -159,7 +154,7 @@ class Dispatcher {

inline void executeSerialEvents(std::vector<Task> &tasks);
inline void executeParallelEvents(std::vector<Task> &tasks, const uint8_t groupId);
inline std::chrono::nanoseconds timeUntilNextScheduledTask() const;
inline std::chrono::milliseconds timeUntilNextScheduledTask() const;

inline void checkPendingTasks() {
hasPendingTasks = false;
Expand Down
2 changes: 0 additions & 2 deletions src/game/scheduling/task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
#include "pch.hpp"
#include "task.hpp"
#include "lib/logging/log_with_spd_log.hpp"

std::chrono::system_clock::time_point Task::TIME_NOW = SYSTEM_TIME_ZERO;
std::atomic_uint_fast64_t Task::LAST_EVENT_ID = 0;

bool Task::execute() const {
Expand Down
16 changes: 6 additions & 10 deletions src/game/scheduling/task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,15 @@
#include "utils/tools.hpp"
#include <unordered_set>

static constexpr auto SYSTEM_TIME_ZERO = std::chrono::system_clock::time_point(std::chrono::milliseconds(0));

class Task {
public:
static std::chrono::system_clock::time_point TIME_NOW;

Task(uint32_t expiresAfterMs, std::function<void(void)> &&f, std::string_view context) :
func(std::move(f)), context(context), utime(TIME_NOW), expiration(expiresAfterMs > 0 ? TIME_NOW + std::chrono::milliseconds(expiresAfterMs) : SYSTEM_TIME_ZERO) {
func(std::move(f)), context(context), utime(OTSYS_TIME()), expiration(expiresAfterMs > 0 ? OTSYS_TIME() + expiresAfterMs : 0) {
assert(!this->context.empty() && "Context cannot be empty!");
}

Task(std::function<void(void)> &&f, std::string_view context, uint32_t delay, bool cycle = false, bool log = true) :
func(std::move(f)), context(context), utime(TIME_NOW + std::chrono::milliseconds(delay)), delay(delay), cycle(cycle), log(log) {
func(std::move(f)), context(context), utime(OTSYS_TIME() + delay), delay(delay), cycle(cycle), log(log) {
assert(!this->context.empty() && "Context cannot be empty!");
}

Expand Down Expand Up @@ -54,7 +50,7 @@ class Task {
}

bool hasExpired() const {
return expiration != SYSTEM_TIME_ZERO && expiration < TIME_NOW;
return expiration != 0 && expiration < OTSYS_TIME();
}

bool isCycle() const {
Expand All @@ -75,7 +71,7 @@ class Task {
static std::atomic_uint_fast64_t LAST_EVENT_ID;

void updateTime() {
utime = TIME_NOW + std::chrono::milliseconds(delay);
utime = OTSYS_TIME() + delay;
}

bool hasTraceableContext() const {
Expand Down Expand Up @@ -117,8 +113,8 @@ class Task {
std::function<void(void)> func = nullptr;
std::string_view context;

std::chrono::system_clock::time_point utime = SYSTEM_TIME_ZERO;
std::chrono::system_clock::time_point expiration = SYSTEM_TIME_ZERO;
int64_t utime = 0;
int64_t expiration = 0;

uint64_t id = 0;
uint32_t delay = 0;
Expand Down
7 changes: 6 additions & 1 deletion src/utils/tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1454,8 +1454,13 @@ const char* getReturnMessage(ReturnValue value) {
}
}

int64_t OTSYSTIME = 0;
void UPDATE_OTSYS_TIME() {
OTSYSTIME = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
}

int64_t OTSYS_TIME() {
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
return OTSYSTIME;
}

SpellGroup_t stringToSpellGroup(const std::string &value) {
Expand Down
1 change: 1 addition & 0 deletions src/utils/tools.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ bool isCaskItem(uint16_t itemId);
std::string getObjectCategoryName(ObjectCategory_t category);

int64_t OTSYS_TIME();
void UPDATE_OTSYS_TIME();

SpellGroup_t stringToSpellGroup(const std::string &value);

Expand Down
Loading