From a32fad38079d6437663ad5cfc903bf063f2ab1a0 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 20 Jul 2024 07:13:14 +0800 Subject: [PATCH] perf: utility improvements --- src/core/utils/utility.h | 13 +++---------- src/core/utils/worker.inl | 7 +++---- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/src/core/utils/utility.h b/src/core/utils/utility.h index d5fe048..b04348a 100644 --- a/src/core/utils/utility.h +++ b/src/core/utils/utility.h @@ -43,17 +43,10 @@ consteval int array_sum(const std::array &arr) { /// Calculate offset list of integer array with sizes. template -requires std::is_integral_v +requires std::is_integral_v && (N > 0) consteval std::array to_offset(const std::array &arr) { - std::array offset; - static_assert(N > 0); - offset[0] = 0; - - T val = 0; - for (int i = 0; i < N - 1; ++i) { // TODO: using `std::views::iota` - val += arr[i]; - offset[i + 1] = val; - } + std::array offset {}; + std::partial_sum(arr.begin(), arr.end() - 1, offset.begin() + 1); return offset; } diff --git a/src/core/utils/worker.inl b/src/core/utils/worker.inl index d7d9baf..8aa2be4 100644 --- a/src/core/utils/worker.inl +++ b/src/core/utils/worker.inl @@ -1,6 +1,7 @@ #pragma once #include +#include namespace klotski { @@ -12,14 +13,12 @@ inline void Worker::post(Task &&task) { } inline void Worker::then(Notifier &&after) { - after_ = [after = std::move(after)]() { - after(); - }; + after_ = std::move(after); } inline Worker::~Worker() { if (tasks_.empty()) { - executor_([after = after_] { + executor_([after = std::move(after_)] { after(); // callback directly }); return;