git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v4] Threaded grep

From
Benjamin Kramer <benny.kra@googlemail.com>
Date
Jan 26, 2010, 16:30 UTC
Message-ID
<4B5F1894.4070509@googlemail.com>
In-Reply-To
<alpine.LFD.2.00.1001260728260.3574@localhost.localdomain>

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. ---

On 26.01.10 16:28, Linus Torvalds wrote:
> so it's sadly internal to regex. It would be nice if there was a 
> non-string interface to regexec (ie a "buffer + length" instead of a 
> NUL-terminated string).

BSD and glibc have an "REG_STARTEND" flag to do that. I made a small PoC patch to use it if it's available but it didn't give any significant speedup on my system.

 grep.c |    9 ++++++++-
 1 files changed, 8 insertions(+), 1 deletions(-)
diff --git a/grep.c b/grep.c
index d281a02..60cce46 100644
--- a/grep.c
+++ b/grep.c
@@ -675,8 +675,15 @@ static int look_ahead(struct grep_opt *opt,
 
 		if (p->fixed)
 			hit = !fixmatch(p->pattern, bol, p->ignore_case, &m);
-		else
+		else {
+#ifdef REG_STARTEND
+			m.rm_so = 0;
+			m.rm_eo = *left_p;
+			hit = !regexec(&p->regexp, bol, 1, &m, REG_STARTEND);
+#else
 			hit = !regexec(&p->regexp, bol, 1, &m, 0);
+#endif
+		}
 		if (!hit || m.rm_so < 0 || m.rm_eo < 0)
 			continue;
 		if (earliest < 0 || m.rm_so < earliest)
--
1.7.0.rc0.12.gc33c3
Previous: Linus TorvaldsNext: Linus Torvalds
Message 5 of 12 in “Threaded grep”
  1. Threaded grepFredrik Kuivinen, Jan 25, 2010
  2. Linus TorvaldsJan 25, 2010
  3. Fredrik KuivinenJan 26, 2010
  4. Linus TorvaldsJan 26, 2010
  5. Benjamin KramerJan 26, 2010
  6. Linus TorvaldsJan 26, 2010
  7. Linus TorvaldsJan 26, 2010
  8. Mike HommeyJan 26, 2010
  9. grep: use REG_STARTEND (if available) to speed up regexecBenjamin Kramer, Jan 26, 2010
  10. Junio C HamanoJan 26, 2010
  11. Fredrik KuivinenJan 26, 2010
  12. Junio C HamanoJan 26, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.