[Windows] Reduce the number of worker threads on many-core machines

Testing shows that performance decreases beyond about 14 threads on
Windows. On my primary workstation, capping at 14 threads reduces
runtime by 25% - 30% depending on the configuration (8s down to 6s or
11s down to 7.5s).

Bug: none
Change-Id: I17a4f5b8e913efcfa1998b9315362dcccac6f675
Reviewed-on: https://gn-review.googlesource.com/c/gn/+/20640
Reviewed-by: Sylvain Defresne <sdefresne@chromium.org>
Commit-Queue: Greg Thompson <grt@chromium.org>
Reviewed-by: Andrew Grieve <agrieve@google.com>
diff --git a/src/util/worker_pool.cc b/src/util/worker_pool.cc
index c1105d6..9efc1d2 100644
--- a/src/util/worker_pool.cc
+++ b/src/util/worker_pool.cc
@@ -7,6 +7,7 @@
 #include "base/command_line.h"
 #include "base/strings/string_number_conversions.h"
 #include "gn/switches.h"
+#include "util/build_config.h"
 #include "util/sys_info.h"
 
 namespace {
@@ -23,6 +24,15 @@
     return result;
   }
 
+  // Almost all CPUs now are hyperthreaded.
+  int num_cores = NumberOfProcessors() / 2;
+
+#if defined(OS_WIN)
+  // Experiments on Windows show that 8 threads is a good value for a 12-core
+  // machine, whereas anything over 12-14 threads on a 64-core machine gets
+  // progressively worse as the thread count increases.
+  return std::min(std::max(num_cores - 1, 8), 14);
+#else
   // Base the default number of worker threads on number of cores in the
   // system. When building large projects, the speed can be limited by how fast
   // the main thread can dispatch work and connect the dependency graph. If
@@ -36,9 +46,8 @@
   //
   // The minimum thread count is based on measuring the optimal threads for the
   // Chrome build on a several-year-old 4-core MacBook.
-  // Almost all CPUs now are hyperthreaded.
-  int num_cores = NumberOfProcessors() / 2;
   return std::max(num_cores - 1, 8);
+#endif
 }
 
 }  // namespace