From a6fa3c0bb781cd1b44b57635eec1ecd990112b84 Mon Sep 17 00:00:00 2001 From: David Meybohm Date: Thu, 12 Sep 2024 21:13:01 -0400 Subject: [PATCH] Fix web logger and adjust timing --- engine/src/move_timer.cpp | 17 ++++++++++------- engine/src/move_timer.hpp | 11 ++++++++--- ui/wasm/bindings.cpp | 10 ++++------ ui/wasm/web_logger.hpp | 6 +++--- 4 files changed, 25 insertions(+), 19 deletions(-) diff --git a/engine/src/move_timer.cpp b/engine/src/move_timer.cpp index d96318fc5..1304478f5 100644 --- a/engine/src/move_timer.cpp +++ b/engine/src/move_timer.cpp @@ -8,7 +8,7 @@ namespace wisdom auto MoveTimer::isTriggered() -> bool { - if (!my_timer_state.my_started_time.has_value()) + if (!my_timer_state.started_time.has_value()) return false; if (my_timer_state.triggered || my_timer_state.cancelled) @@ -25,23 +25,26 @@ namespace wisdom } steady_clock::time_point check_time = steady_clock::now(); - auto diff_time = check_time - *my_timer_state.my_started_time; + auto diff_time = check_time - *my_timer_state.started_time; // adjust the next iteration if less than 250ms: - if (my_timer_state.my_last_check_time.has_value()) + if (my_timer_state.last_check_time.has_value()) { - auto last_time = *my_timer_state.my_last_check_time; + auto last_time = *my_timer_state.last_check_time; auto check_time_diff = check_time - last_time; + auto& timing = *my_timing_adjustment; if ( - check_time_diff < chrono::milliseconds(250) && timing.current_iterations < Max_Iterations_Before_Checking + check_time_diff < Lower_Bound_Timer_Check && + timing.current_iterations < Max_Iterations_Before_Checking ) { timing.current_iterations = narrow (std::floor (timing.current_iterations * 1.5)); if (timing.current_iterations > Max_Iterations_Before_Checking) timing.current_iterations = Max_Iterations_Before_Checking; } else if ( - check_time_diff > chrono::milliseconds (500) && timing.current_iterations > Min_Iterations_Before_Checking + check_time_diff > Upper_Bound_Timer_Check && + timing.current_iterations > Min_Iterations_Before_Checking ) { timing.current_iterations /= 2; if (timing.current_iterations < Min_Iterations_Before_Checking) @@ -49,7 +52,7 @@ namespace wisdom } } - my_timer_state.my_last_check_time = check_time; + my_timer_state.last_check_time = check_time; if (diff_time >= my_seconds) { diff --git a/engine/src/move_timer.hpp b/engine/src/move_timer.hpp index 3e482c97a..e03b6d58c 100644 --- a/engine/src/move_timer.hpp +++ b/engine/src/move_timer.hpp @@ -7,6 +7,11 @@ namespace wisdom inline constexpr int Min_Iterations_Before_Checking = 10'000; inline constexpr int Max_Iterations_Before_Checking = 1'000'000; + inline constexpr chrono::milliseconds Lower_Bound_Timer_Check = + chrono::milliseconds { 100 }; + inline constexpr chrono::milliseconds Upper_Bound_Timer_Check = + chrono::milliseconds { 150 }; + struct MoveTimer; struct TimingAdjustment @@ -16,8 +21,8 @@ namespace wisdom struct TimerState { - optional my_started_time {}; - optional my_last_check_time {}; + optional started_time {}; + optional last_check_time {}; int check_calls = 0; bool triggered = false; @@ -51,7 +56,7 @@ namespace wisdom { // Reset the state but preserve a few values: my_timer_state = TimerState {}; - my_timer_state.my_started_time = chrono::steady_clock::now(); + my_timer_state.started_time = chrono::steady_clock::now(); } [[nodiscard]] auto diff --git a/ui/wasm/bindings.cpp b/ui/wasm/bindings.cpp index 5ee849655..01b962071 100644 --- a/ui/wasm/bindings.cpp +++ b/ui/wasm/bindings.cpp @@ -169,7 +169,7 @@ EMSCRIPTEN_KEEPALIVE void workerReinitializeGame (int new_game_id) EMSCRIPTEN_KEEPALIVE void startSearch() { - const auto& logger = wisdom::worker::getLogger(); + auto logger = wisdom::worker::makeLogger(); auto state = GameState::getState(); auto game = GameState::getGame(); @@ -184,8 +184,8 @@ EMSCRIPTEN_KEEPALIVE void startSearch() if (new_status != wisdom::GameStatus::Playing) return; - logger.debug("Going to find best move"); - logger.debug("Current turn: " + asString (game->getCurrentTurn())); + logger->debug("Going to find best move"); + logger->debug("Current turn: " + asString (game->getCurrentTurn())); auto move = game->findBestMove( logger, @@ -194,7 +194,7 @@ EMSCRIPTEN_KEEPALIVE void startSearch() if (!move.has_value()) { // Could happen if game is paused: - getLogger().debug("No move found."); + logger->debug("No move found."); return; } game->move (*move); @@ -232,8 +232,6 @@ EM_JS (void, receiveMoveFromWorker, (int game_id, const char* str), EMSCRIPTEN_KEEPALIVE void mainThreadReceiveMove (int game_id, int packed_move) { - auto& logger = wisdom::worker::getLogger(); - Move unpacked_move = Move::fromInt (packed_move); auto state = GameState::getState(); std::string str = asString (unpacked_move); diff --git a/ui/wasm/web_logger.hpp b/ui/wasm/web_logger.hpp index 0fbfeedc5..121573763 100644 --- a/ui/wasm/web_logger.hpp +++ b/ui/wasm/web_logger.hpp @@ -18,11 +18,11 @@ namespace wisdom::worker static void consoleLog (const char* str); }; - [[nodiscard]] inline auto getLogger() -> const WebLogger& + [[nodiscard]] inline auto makeLogger() -> shared_ptr { using namespace wisdom; - static std::unique_ptr worker_logger = std::make_unique(); - return *worker_logger; + static std::shared_ptr worker_logger = std::make_shared(); + return worker_logger; } }