Skip to content

Commit 252d560

Browse files
René Scharfegitster
authored andcommitted
grep: micro-optimize hit collection for AND nodes
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> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 8cc3fe4 commit 252d560

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

grep.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -394,13 +394,9 @@ static int match_expr_eval(struct grep_opt *o,
394394
h = !match_expr_eval(o, x->u.unary, bol, eol, ctx, 0);
395395
break;
396396
case GREP_NODE_AND:
397-
if (!collect_hits)
398-
return (match_expr_eval(o, x->u.binary.left,
399-
bol, eol, ctx, 0) &&
400-
match_expr_eval(o, x->u.binary.right,
401-
bol, eol, ctx, 0));
402-
h = match_expr_eval(o, x->u.binary.left, bol, eol, ctx, 0);
403-
h &= match_expr_eval(o, x->u.binary.right, bol, eol, ctx, 0);
397+
if (!match_expr_eval(o, x->u.binary.left, bol, eol, ctx, 0))
398+
return 0;
399+
h = match_expr_eval(o, x->u.binary.right, bol, eol, ctx, 0);
404400
break;
405401
case GREP_NODE_OR:
406402
if (!collect_hits)

0 commit comments

Comments
 (0)