grep: optimize built-in grep by skipping lines that do not hit
commita26345b6085340ebd61e156aa8154a80196bee0f
authorJunio C Hamano <gitster@pobox.com>
Mon, 11 Jan 2010 06:39:36 +0000 (10 22:39 -0800)
committerJunio C Hamano <gitster@pobox.com>
Tue, 12 Jan 2010 08:47:50 +0000 (12 00:47 -0800)
treed8aa79fd39d8d3418383d52dbad8f4b7401a8e93
parentcb572206d9dac4ba52878e7e1a4a7028d85707ab
grep: optimize built-in grep by skipping lines that do not hit

The internal "grep" engine we use checks for hits line-by-line, instead of
letting the underlying regexec()/fixmatch() routines scan for the first
match from the rest of the buffer.  This was a major source of overhead
compared to the external grep.

Introduce a "look-ahead" mechanism to find the next line that would
potentially match by using regexec()/fixmatch() in the remainder of the
text to skip unmatching lines, and use it when the query criteria is
simple enough (i.e. punt for an advanced grep boolean expression like
"lines that have both X and Y but not Z" for now) and we are not running
under "-v" (aka "--invert-match") option.

Note that "-L" (aka "--files-without-match") is not a reason to disable
this optimization.  Under the option, we are interested if the file has
any hit at all, and that is what we determine reliably with or without the
optimization.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
grep.c