From 0df8e69c8801c496354d407d3bbc7494b0c4e19d Mon Sep 17 00:00:00 2001 From: tastytea Date: Thu, 8 Aug 2019 19:06:57 +0200 Subject: [PATCH] Set the maximum number of threads to use dynamically. The formula is reported_number_of_threads / 3 + 1. Each thread works on at least 50 entries. --- src/cli/main.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/cli/main.cpp b/src/cli/main.cpp index 7e31ec0..f38edfd 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -126,9 +126,19 @@ int App::main(const std::vector &args) { const size_t len = entries.size(); constexpr size_t min_len = 100; - constexpr size_t n_threads = 2; - // If there are over `min_len` entries, use `n_threads` threads. - const size_t cut_at = ((len > min_len) ? (len / n_threads) : len); + constexpr size_t min_per_thread = 50; + const size_t n_threads = thread::hardware_concurrency() / 3 + 1; + size_t cut_at = len; + if (len > min_len) + { // If there are over `min_len` entries, use `n_threads` threads. + cut_at = len / n_threads; + + // But don't use less than `min_per_thread` entries per thread. + if (cut_at < min_per_thread) + { + cut_at = min_per_thread; + } + } list> segments;