Limit the maximum number of GN worker threads to 32.
On machines with high core counts (e.g., 128 cores), using all available
CPUs for worker threads significantly slows down `gn gen` due to thread
scheduling overhead and contention.
This change caps the default number of worker threads at 32 while
maintaining the existing minimum of 8.
Profiling results of `gn gen` for Chrome using hyperfine on a 128-core
Linux machine:
Summary
./buildtools/linux64/gn gen out/Default/ --threads=32 ran
1.01 ± 0.03 times faster than ./buildtools/linux64/gn gen out/Default/ --threads=16
1.07 ± 0.03 times faster than ./buildtools/linux64/gn gen out/Default/ --threads=48
1.13 ± 0.03 times faster than ./buildtools/linux64/gn gen out/Default/ --threads=64
1.26 ± 0.03 times faster than ./buildtools/linux64/gn gen out/Default/ --threads=96
1.28 ± 0.04 times faster than ./buildtools/linux64/gn gen out/Default/ --threads=8
This improved gn gen for android too.
Summary
./buildtools/linux64/gn --threads=32 gen out/android ran
1.19 ± 0.04 times faster than ./buildtools/linux64/gn gen out/android
Bug: 484863025
Change-Id: I44b467c1813a32015666926d8bbaaad4206b5447
Reviewed-on: https://gn-review.googlesource.com/c/gn/+/21740
Reviewed-by: David Turner <digit@google.com>
Commit-Queue: Takuto Ikuta <tikuta@google.com>
diff --git a/src/util/worker_pool.cc b/src/util/worker_pool.cc
index ac903fa..3bff847 100644
--- a/src/util/worker_pool.cc
+++ b/src/util/worker_pool.cc
@@ -52,7 +52,7 @@
//
// The minimum thread count is based on measuring the optimal threads for the
// Chrome build on a several-year-old 4-core MacBook.
- return std::max(num_cores - 1, 8);
+ return std::max(std::min(num_cores - 1, 32), 8);
#endif
}