@matheusbsbs / matheus-bagliano / commits / 24072c0256
Fork 0

grep: use REG_STARTEND (if available) to speed up regexec

BSD and glibc have an extension to regexec which takes a buffer + length pair instead of a NUL-terminated string. Since we already have the length computed this can save us a strlen call inside regexec. Signed-off-by: Benjamin Kramer <benny.kra@googlemail.com> Acked-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Benjamin Kramer committed Jan 26, 2010 at 18:48 UTC 24072c0256a520408575416fe8706667b576ff99
1 file changed +8 -1
grep.c
+8 -1
@@ -640,8 +640,15 @@ static int look_ahead(struct grep_opt *opt,
640
641 if (p->fixed)
642 hit = !fixmatch(p->pattern, bol, p->ignore_case, &m);
643 - else
643 + else {
644 +#ifdef REG_STARTEND
645 + m.rm_so = 0;
646 + m.rm_eo = *left_p;
647 + hit = !regexec(&p->regexp, bol, 1, &m, REG_STARTEND);
648 +#else
649 hit = !regexec(&p->regexp, bol, 1, &m, 0);
650 +#endif
651 + }
652 if (!hit || m.rm_so < 0 || m.rm_eo < 0)
653 continue;
654 if (earliest < 0 || m.rm_so < earliest)