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

[PATCH 1/5] grep: micro-optimize hit collection for AND nodes

From
René Scharfe <rene.scharfe@lsrfire.ath.cx>
Date
Mar 7, 2009, 12:27 UTC
Message-ID
<1236428835.6486.43.camel@ubuntu.ubuntu-domain>
In-Reply-To
<1236428699.6486.41.camel@ubuntu.ubuntu-domain>

In addition to returning if an expression matches a line, match_expr_eval() updates the expression's hit flag if the parameter collect_hits is set. It never sets collect_hits for children of AND nodes, though, so their hit flag will never be updated. Because of that we can return early if the first child didn't match, no matter if collect_hits is set or not.

Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
---
 grep.c |   10 +++-------
 1 files changed, 3 insertions(+), 7 deletions(-)
diff --git a/grep.c b/grep.c
index 062b2b6..db341b6 100644
--- a/grep.c
+++ b/grep.c
@@ -394,13 +394,9 @@ static int match_expr_eval(struct grep_opt *o,
 		h = !match_expr_eval(o, x->u.unary, bol, eol, ctx, 0);
 		break;
 	case GREP_NODE_AND:
-		if (!collect_hits)
-			return (match_expr_eval(o, x->u.binary.left,
-						bol, eol, ctx, 0) &&
-				match_expr_eval(o, x->u.binary.right,
-						bol, eol, ctx, 0));
-		h = match_expr_eval(o, x->u.binary.left, bol, eol, ctx, 0);
-		h &= match_expr_eval(o, x->u.binary.right, bol, eol, ctx, 0);
+		if (!match_expr_eval(o, x->u.binary.left, bol, eol, ctx, 0))
+			return 0;
+		h = match_expr_eval(o, x->u.binary.right, bol, eol, ctx, 0);
 		break;
 	case GREP_NODE_OR:
 		if (!collect_hits)
-- 
1.6.2
Previous: René ScharfeNext: René Scharfe
Message 2 of 10 in “grep: color search patterns”
  1. 0/5 grep: color search patternsRené Scharfe, Mar 7, 2009
  2. 1/5 grep: micro-optimize hit collection for AND nodesRené Scharfe, Mar 7, 2009
  3. 2/5 grep: remove grep_opt argument from match_expr_eval()René Scharfe, Mar 7, 2009
  4. 3/5 grep: add pmatch and eflags arguments to match_one_pattern()René Scharfe, Mar 7, 2009
  5. 4/5 grep: color patterns in outputRené Scharfe, Mar 7, 2009
  6. 5/5 grep: add support for coloring with external grepsRené Scharfe, Mar 7, 2009
  7. Nguyen Thai Ngoc DuyMar 10, 2009
  8. René ScharfeMar 10, 2009
  9. grep: prefer builtin over external one when coloring resultspclouds@gmail.com, Mar 16, 2009
  10. René ScharfeMar 16, 2009

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.