Skip to content

Commit

Permalink
grep: disable threading in non-worktree case
Browse files Browse the repository at this point in the history
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 <rene.scharfe@lsrfire.ath.cx>
Helped-by: Jeff King <peff@peff.net>
Signed-off-by: Thomas Rast <trast@student.ethz.ch>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Thomas Rast authored and Junio C Hamano committed Dec 16, 2011
1 parent 0579f91 commit 53b8d93
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions builtin/grep.c
Original file line number Diff line number Diff line change
Expand Up @@ -1002,24 +1002,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
if (!opt.fixed && opt.ignore_case)
opt.regflags |= REG_ICASE;

#ifndef NO_PTHREADS
if (online_cpus() == 1)
use_threads = 0;
#else
use_threads = 0;
#endif

opt.use_threads = use_threads;

#ifndef NO_PTHREADS
if (use_threads) {
if (opt.pre_context || opt.post_context || opt.file_break ||
opt.funcbody)
skip_first_line = 1;
start_threads(&opt);
}
#endif

compile_grep_patterns(&opt);

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

#ifndef NO_PTHREADS
if (list.nr || cached || online_cpus() == 1)
use_threads = 0;
#else
use_threads = 0;
#endif

opt.use_threads = use_threads;

#ifndef NO_PTHREADS
if (use_threads) {
if (opt.pre_context || opt.post_context || opt.file_break ||
opt.funcbody)
skip_first_line = 1;
start_threads(&opt);
}
#endif

/* The rest are paths */
if (!seen_dashdash) {
int j;
Expand Down

0 comments on commit 53b8d93

Please sign in to comment.