From 6da27e2fad32eec611a2325d7f90a33fe182aed2 Mon Sep 17 00:00:00 2001 From: rektdie Date: Sun, 15 Nov 2026 15:47:04 +0100 Subject: [PATCH] feat: smart pruning stops clock searches whose best move is settled Under a game clock a search now stops early when no other root move can catch up with the leading one in the playouts the remaining time still allows (at the speed so far, divided by Lc0's smart-pruning factor 1.33). The saved time goes to later moves, whose allocation is a share of the remaining clock. movetime, nodes and infinite searches are not affected. Only the visits gained in this search count, and the move leading them must also be the most visited overall. Counting the reused tree's visits stopped almost every search at once: the reused best move often has hundreds of thousands of visits no rival could reach (a first version spent 39 ms per move instead of 230 in self-play at 8+0.08). Self-play diagnostic at 8+0.08 (3 games, 80 plies), main -> this: average time per move 230 -> 157 ms, clock left after move 40 2.0 s -> 4.6-5.2 s. The bench is unchanged: it searches by playout count. Bench: 554375 Co-Authored-By: Claude Opus 5.5 --- src/mcts.cpp | 44 +++++++++++++++++++++++++++++++++++++++++++- src/mcts.h | 7 +++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/mcts.cpp b/src/mcts.cpp index 7bcac3c..f8c7f25 100644 --- a/src/mcts.cpp +++ b/src/mcts.cpp @@ -137,6 +137,7 @@ i64 Searcher::elapsed_ms() const { void Searcher::compute_time_limit(const SearchLimits& limits) { time_limit_ms_ = 0; + clock_search_ = false; if (limits.infinite) return; if (limits.movetime) { @@ -151,6 +152,34 @@ void Searcher::compute_time_limit(const SearchLimits& limits) { const i64 moves_left = limits.movestogo ? std::min(limits.movestogo, 40) : 25; const i64 target = remaining / moves_left + limits.inc[us] * 3 / 4; time_limit_ms_ = std::clamp(target, 1, remaining / 2 + 1); + clock_search_ = true; +} + +bool Searcher::best_move_settled(i64 ms) const { + // Smart pruning (Lc0): stop when no other root move can catch up with the + // leading one in the playouts the remaining time still allows, at the + // speed so far, scaled down by SMART_PRUNING_FACTOR. Only the visits of + // this search count: a reused tree often brings the best move so many + // visits that nothing could overtake it, which would stop every search + // at once. The move leading this search must also be the one played. + if (ms <= 0 || iterations_ < 256) return false; + const Node& root = tree_[Tree::root()]; + if (root_start_visits_.size() != root.num_children) return false; + u32 first = 0, second = 0, leader = 0; + for (u32 i = 0; i < root.num_children; i++) { + const u32 v = tree_[root.first_child + i].visits - root_start_visits_[i]; + if (v > first) { + second = first; + first = v; + leader = root.first_child + i; + } else if (v > second) { + second = v; + } + } + if (leader != best_child(Tree::root())) return false; + const double remaining = + double(iterations_) / double(ms) * double(time_limit_ms_ - ms) / SMART_PRUNING_FACTOR; + return double(second) + remaining < double(first); } bool Searcher::should_stop(const SearchLimits& limits) const { @@ -163,7 +192,12 @@ bool Searcher::should_stop(const SearchLimits& limits) const { if (iterations_ >= (u64(1000) << std::min(2 * limits.depth, 14))) return true; } // The clock is cheap but not free; look at it every 64 playouts. - if (time_limit_ms_ && (iterations_ & 63) == 0 && elapsed_ms() >= time_limit_ms_) return true; + if (time_limit_ms_ && (iterations_ & 63) == 0) { + const i64 ms = elapsed_ms(); + if (ms >= time_limit_ms_) return true; + // Under a game clock the time saved on a settled move goes to later moves. + if (clock_search_ && best_move_settled(ms)) return true; + } return false; } @@ -532,6 +566,14 @@ SearchResult Searcher::search(const Position& root, const std::vector& hist expand(Tree::root(), root); } + // Root move visits brought along by tree reuse, for smart pruning. The + // root's children keep their order through collections. + root_start_visits_.clear(); + { + const Node& r = tree_[Tree::root()]; + for (u32 i = 0; i < r.num_children; i++) root_start_visits_.push_back(tree_[r.first_child + i].visits); + } + // UCI reports once a second; the dashboard for a human is redrawn in place // ten times a second. const i64 info_interval = pretty ? 100 : 1000; diff --git a/src/mcts.h b/src/mcts.h index eabcac6..8b8a7ea 100644 --- a/src/mcts.h +++ b/src/mcts.h @@ -152,6 +152,12 @@ private: Clock::time_point start_time_; i64 time_limit_ms_ = 0; + // The limit comes from the game clock (not movetime), so stopping early saves time. + bool clock_search_ = false; + std::vector root_start_visits_; + // Lc0's default: the remaining playouts are divided by this before asking + // whether another root move could still overtake the best one. + static constexpr double SMART_PRUNING_FACTOR = 1.33; u64 iterations_ = 0; u64 depth_sum_ = 0; int seldepth_ = 0; @@ -181,6 +187,7 @@ private: void compute_time_limit(const SearchLimits& limits); i64 elapsed_ms() const; bool should_stop(const SearchLimits& limits) const; + bool best_move_settled(i64 ms) const; u32 best_child(u32 idx) const; void print_info() const;