git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[RFC PATCH 2/2] grep: make default number of threads reflect runtime

From
Carlo Marcelo Arenas Belón <carenas@gmail.com>
Date
Aug 4, 2019, 21:15 UTC
Message-ID
<20190804211509.39229-3-carenas@gmail.com>
In-Reply-To
<20190804211509.39229-1-carenas@gmail.com>

5b594f457a (Threaded grep, 2010-01-25) added a hardcoded number of threads(8) to use in grep and 89f09dd34e (grep: add --threads=<num> option and grep.threads configuration, 2015-12-15) made it configurable through a knob as a workaround for systems where that default was not effective.

Use instead the industry standard of 2x number of CPUs (to allow for IO wait) for the default.

Using Debian 10 amd64 in a 2 CPU VirtualBox running in macOS 10.14.6 and that might had been representative of the original author environment shows an overall performance improvement by avoiding thread trashing:

Test origin/maint HEAD ----------------------------------------------------------------------------------------------- 7810.1: grep worktree, fixed regex (no match) 0.52(0.43+0.49) 0.50(0.44+0.46) -3.8% 7810.2: grep worktree, fixed regex (common) 0.94(1.20+0.50) 0.91(1.24+0.44) -3.2% 7810.3: grep -I, fixed non binary regex (common) 0.98(1.24+0.51) 0.94(1.30+0.44) -4.1% 7810.4: grep -i, fixed caseless regex (common) 0.97(1.31+0.45) 0.93(1.18+0.56) -4.1% 7810.5: grep --no-index, fixed regex (common) 1.02(1.28+0.50) 0.97(1.14+0.59) -4.9% 7810.6: grep worktree, simple regex (common) 0.77(0.96+0.45) 0.73(0.88+0.48) -5.2% 7810.7: grep -I, simple non binary regex (common) 0.78(0.96+0.48) 0.73(0.94+0.43) -6.4% 7810.8: grep -i, simple caseless regex (common) 0.87(1.11+0.48) 0.82(1.16+0.38) -5.7% 7810.9: grep worktree, expensive regex 10.37(19.67+0.76) 10.20(19.46+0.76) -1.6% 7810.10: grep --cached, fixed regex 4.48(4.37+0.10) 4.63(4.54+0.09) +3.3% 7810.11: grep --cached, expensive regex 23.74(23.61+0.11) 23.39(23.28+0.09) -1.5%

Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com>
---
 Documentation/git-grep.txt | 2 +-
 builtin/grep.c             | 3 +--
 2 files changed, 2 insertions(+), 3 deletions(-)
diff --git a/Documentation/git-grep.txt b/Documentation/git-grep.txt
index 2d27969057..5d72e03b2e 100644
--- a/Documentation/git-grep.txt
+++ b/Documentation/git-grep.txt
@@ -60,7 +60,7 @@ grep.extendedRegexp::
 
 grep.threads::
 	Number of grep worker threads to use.  If unset (or set to 0),
-	8 threads are used by default (for now).
+	2 threads per core are used by default.
 
 grep.fullName::
 	If set to true, enable `--full-name` option by default.
diff --git a/builtin/grep.c b/builtin/grep.c
index 580fd38f41..0ed8da30f8 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -32,7 +32,6 @@ static char const * const grep_usage[] = {
 
 static int recurse_submodules;
 
-#define GREP_NUM_THREADS_DEFAULT 8
 static int num_threads;
 
 static pthread_t *threads;
@@ -1068,7 +1067,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 	} else if (num_threads < 0)
 		die(_("invalid number of threads specified (%d)"), num_threads);
 	else if (num_threads == 0)
-		num_threads = HAVE_THREADS ? GREP_NUM_THREADS_DEFAULT : 1;
+		num_threads = HAVE_THREADS ? online_cpus() * 2 : 1;
 
 	if (num_threads > 1) {
 		if (!HAVE_THREADS)
-- 
2.23.0.rc1
Previous: Carlo Marcelo Arenas BelónNext: Junio C Hamano
Message 3 of 4 in “grep: make threading smarter”
  1. 0/2 grep: make threading smarterCarlo Marcelo Arenas Belón, Aug 4, 2019
  2. 1/2 p7810: add more grep performance relevant casesCarlo Marcelo Arenas Belón, Aug 4, 2019
  3. 2/2 grep: make default number of threads reflect runtimeCarlo Marcelo Arenas Belón, Aug 4, 2019
  4. Junio C HamanoAug 5, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.