Skip to content

Commit 0826579

Browse files
peffgitster
authored andcommitted
grep: load file data after checking binary-ness
Usually we load each file to grep into memory, check whether it's binary, and then either grep it (the default) or not (if "-I" was given). In the "-I" case, we can skip loading the file entirely if it is marked as binary via gitattributes. On my giant 3-gigabyte media repository, doing "git grep -I foo" went from: real 0m0.712s user 0m0.044s sys 0m4.780s to: real 0m0.026s user 0m0.016s sys 0m0.020s Obviously this is an extreme example. The repo is almost entirely binary files, and you can see that we spent all of our time asking the kernel to read() the data. However, with a cold disk cache, even avoiding a few binary files can have an impact. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 41b59bf commit 0826579

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

grep.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,9 +1019,6 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
10191019
}
10201020
opt->last_shown = 0;
10211021

1022-
if (grep_source_load(gs) < 0)
1023-
return 0;
1024-
10251022
switch (opt->binary) {
10261023
case GREP_BINARY_DEFAULT:
10271024
if (grep_source_is_binary(gs))
@@ -1042,6 +1039,9 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
10421039

10431040
try_lookahead = should_lookahead(opt);
10441041

1042+
if (grep_source_load(gs) < 0)
1043+
return 0;
1044+
10451045
bol = gs->buf;
10461046
left = gs->size;
10471047
while (left) {

0 commit comments

Comments
 (0)