Skip to content

Commit 53b8d93

Browse files
trastgitster
authored andcommitted
grep: disable threading in non-worktree case
Measurements by various people have shown that grepping in parallel is not beneficial when the object store is involved. For example, with a simple regex: Threads | --cached case | worktree case ---------------------------------------------------------------- 8 (default) | 2.88u 0.21s 0:02.94real | 0.19u 0.32s 0:00.16real 4 | 2.89u 0.29s 0:02.99real | 0.16u 0.34s 0:00.17real 2 | 2.83u 0.36s 0:02.87real | 0.18u 0.32s 0:00.26real NO_PTHREADS | 2.16u 0.08s 0:02.25real | 0.12u 0.17s 0:00.31real This happens because all the threads contend on read_sha1_mutex almost all of the time. A more complex regex allows the threads to do more work in parallel, but as Jeff King found out, the "super boost" (much higher clock when only one core is active) feature of recent CPUs still causes the unthreaded case to win by a large margin. So until the pack machinery allows unthreaded access, we disable grep's threading in all but the worktree case. Helped-by: René Scharfe <[email protected]> Helped-by: Jeff King <[email protected]> Signed-off-by: Thomas Rast <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0579f91 commit 53b8d93

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

builtin/grep.c

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,24 +1002,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
10021002
if (!opt.fixed && opt.ignore_case)
10031003
opt.regflags |= REG_ICASE;
10041004

1005-
#ifndef NO_PTHREADS
1006-
if (online_cpus() == 1)
1007-
use_threads = 0;
1008-
#else
1009-
use_threads = 0;
1010-
#endif
1011-
1012-
opt.use_threads = use_threads;
1013-
1014-
#ifndef NO_PTHREADS
1015-
if (use_threads) {
1016-
if (opt.pre_context || opt.post_context || opt.file_break ||
1017-
opt.funcbody)
1018-
skip_first_line = 1;
1019-
start_threads(&opt);
1020-
}
1021-
#endif
1022-
10231005
compile_grep_patterns(&opt);
10241006

10251007
/* Check revs and then paths */
@@ -1041,6 +1023,24 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
10411023
break;
10421024
}
10431025

1026+
#ifndef NO_PTHREADS
1027+
if (list.nr || cached || online_cpus() == 1)
1028+
use_threads = 0;
1029+
#else
1030+
use_threads = 0;
1031+
#endif
1032+
1033+
opt.use_threads = use_threads;
1034+
1035+
#ifndef NO_PTHREADS
1036+
if (use_threads) {
1037+
if (opt.pre_context || opt.post_context || opt.file_break ||
1038+
opt.funcbody)
1039+
skip_first_line = 1;
1040+
start_threads(&opt);
1041+
}
1042+
#endif
1043+
10441044
/* The rest are paths */
10451045
if (!seen_dashdash) {
10461046
int j;

0 commit comments

Comments
 (0)