From 51c3af9dd01951cf23f17c5f7c6cb9f7c32dccdc Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Mon, 19 Oct 2009 07:44:06 +0100 Subject: [PATCH 1/1] Avoid a needless locking in sp_search() Only in less then 2% of cases we have a new sp->bestValue, so check before to lock and save a costly locking most of the times. Patch suggested by Joona. No functional search. Signed-off-by: Marco Costalba --- src/search.cpp | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/search.cpp b/src/search.cpp index 13affe1b..ec4272a9 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -1759,21 +1759,24 @@ namespace { break; // New best move? - lock_grab(&(sp->lock)); - if (value > sp->bestValue && !thread_should_stop(threadID)) + if (value > sp->bestValue) // Less then 2% of cases { - sp->bestValue = value; - if (sp->bestValue >= sp->beta) + lock_grab(&(sp->lock)); + if (value > sp->bestValue && !thread_should_stop(threadID)) { - sp_update_pv(sp->parentSstack, ss, sp->ply); - for (int i = 0; i < ActiveThreads; i++) - if (i != threadID && (i == sp->master || sp->slaves[i])) - Threads[i].stop = true; + sp->bestValue = value; + if (sp->bestValue >= sp->beta) + { + sp_update_pv(sp->parentSstack, ss, sp->ply); + for (int i = 0; i < ActiveThreads; i++) + if (i != threadID && (i == sp->master || sp->slaves[i])) + Threads[i].stop = true; - sp->finished = true; - } + sp->finished = true; + } + } + lock_release(&(sp->lock)); } - lock_release(&(sp->lock)); } lock_grab(&(sp->lock)); -- 2.39.2