Skip to content

Commit

Permalink
grep: micro-optimize hit collection for AND nodes
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
René Scharfe authored and Junio C Hamano committed Mar 7, 2009
1 parent 8cc3fe4 commit 252d560
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions grep.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 252d560

Please sign in to comment.