Skip to content

Commit 5fc4a7e

Browse files
committed
Merge branch 'rv/grep-cleanup'
Threaded "git grep" has been optimized to avoid allocation in code section that is covered under a mutex. * rv/grep-cleanup: grep: simplify grep_oid and grep_file grep: move grep_source_init outside critical section
2 parents 9e69a14 + 38ef24d commit 5fc4a7e

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

builtin/grep.c

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,15 @@ static pthread_cond_t cond_result;
9292

9393
static int skip_first_line;
9494

95-
static void add_work(struct grep_opt *opt, enum grep_source_type type,
96-
const char *name, const char *path, const void *id)
95+
static void add_work(struct grep_opt *opt, const struct grep_source *gs)
9796
{
9897
grep_lock();
9998

10099
while ((todo_end+1) % ARRAY_SIZE(todo) == todo_done) {
101100
pthread_cond_wait(&cond_write, &grep_mutex);
102101
}
103102

104-
grep_source_init(&todo[todo_end].source, type, name, path, id);
103+
todo[todo_end].source = *gs;
105104
if (opt->binary != GREP_BINARY_TEXT)
106105
grep_source_load_driver(&todo[todo_end].source);
107106
todo[todo_end].done = 0;
@@ -317,6 +316,7 @@ static int grep_oid(struct grep_opt *opt, const struct object_id *oid,
317316
const char *path)
318317
{
319318
struct strbuf pathbuf = STRBUF_INIT;
319+
struct grep_source gs;
320320

321321
if (opt->relative && opt->prefix_length) {
322322
quote_path_relative(filename + tree_name_len, opt->prefix, &pathbuf);
@@ -325,19 +325,22 @@ static int grep_oid(struct grep_opt *opt, const struct object_id *oid,
325325
strbuf_addstr(&pathbuf, filename);
326326
}
327327

328+
grep_source_init(&gs, GREP_SOURCE_OID, pathbuf.buf, path, oid);
329+
strbuf_release(&pathbuf);
330+
328331
#ifndef NO_PTHREADS
329332
if (num_threads) {
330-
add_work(opt, GREP_SOURCE_OID, pathbuf.buf, path, oid);
331-
strbuf_release(&pathbuf);
333+
/*
334+
* add_work() copies gs and thus assumes ownership of
335+
* its fields, so do not call grep_source_clear()
336+
*/
337+
add_work(opt, &gs);
332338
return 0;
333339
} else
334340
#endif
335341
{
336-
struct grep_source gs;
337342
int hit;
338343

339-
grep_source_init(&gs, GREP_SOURCE_OID, pathbuf.buf, path, oid);
340-
strbuf_release(&pathbuf);
341344
hit = grep_source(opt, &gs);
342345

343346
grep_source_clear(&gs);
@@ -348,25 +351,29 @@ static int grep_oid(struct grep_opt *opt, const struct object_id *oid,
348351
static int grep_file(struct grep_opt *opt, const char *filename)
349352
{
350353
struct strbuf buf = STRBUF_INIT;
354+
struct grep_source gs;
351355

352356
if (opt->relative && opt->prefix_length)
353357
quote_path_relative(filename, opt->prefix, &buf);
354358
else
355359
strbuf_addstr(&buf, filename);
356360

361+
grep_source_init(&gs, GREP_SOURCE_FILE, buf.buf, filename, filename);
362+
strbuf_release(&buf);
363+
357364
#ifndef NO_PTHREADS
358365
if (num_threads) {
359-
add_work(opt, GREP_SOURCE_FILE, buf.buf, filename, filename);
360-
strbuf_release(&buf);
366+
/*
367+
* add_work() copies gs and thus assumes ownership of
368+
* its fields, so do not call grep_source_clear()
369+
*/
370+
add_work(opt, &gs);
361371
return 0;
362372
} else
363373
#endif
364374
{
365-
struct grep_source gs;
366375
int hit;
367376

368-
grep_source_init(&gs, GREP_SOURCE_FILE, buf.buf, filename, filename);
369-
strbuf_release(&buf);
370377
hit = grep_source(opt, &gs);
371378

372379
grep_source_clear(&gs);

0 commit comments

Comments
 (0)