git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
To: git@vger.kernel.org, Junio C Hamano <gitster@pobox.com>,
	Michael J Gruber <git@drmicha.warpmail.net>
Cc: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: [PATCH 3/3] Improve tree_entry_interesting() handling code
Date: Fri, 25 Mar 2011 16:34:20 +0700	[thread overview]
Message-ID: <1301045660-25053-3-git-send-email-pclouds@gmail.com> (raw)
In-Reply-To: <1301045660-25053-1-git-send-email-pclouds@gmail.com>

t_e_i() can return -1 or 2 to early shortcut a search. Current code
may use up to two variables to handle it. One for saving return value
from t_e_i temporarily, one for saving return code 2.

The second variable is not needed. If we make sure the first variable
does not change until the next t_e_i() call, then we can do something
like this:

int ret = 0;

while (...) {
	if (ret != 2) {
		ret = t_e_i();
		if (ret < 0) /* no longer interesting */
			break;
		if (ret == 0) /* skip this round */
			continue;
	}
	/* ret > 0, interesting */
}

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 builtin/grep.c |   12 ++++++------
 list-objects.c |   18 +++++++-----------
 tree-diff.c    |   53 ++++++++++++++++++++---------------------------------
 3 files changed, 33 insertions(+), 50 deletions(-)

diff --git a/builtin/grep.c b/builtin/grep.c
index 0bf8c01..9cb1f32 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -520,18 +520,18 @@ static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec, int
 static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
 		     struct tree_desc *tree, struct strbuf *base, int tn_len)
 {
-	int hit = 0, matched = 0;
+	int hit = 0, match = 0;
 	struct name_entry entry;
 	int old_baselen = base->len;
 
 	while (tree_entry(tree, &entry)) {
 		int te_len = tree_entry_len(entry.path, entry.sha1);
 
-		if (matched != 2) {
-			matched = tree_entry_interesting(&entry, base, tn_len, pathspec);
-			if (matched == -1)
-				break; /* no more matches */
-			if (!matched)
+		if (match != 2) {
+			match = tree_entry_interesting(&entry, base, tn_len, pathspec);
+			if (match < 0)
+				break;
+			if (match == 0)
 				continue;
 		}
 
diff --git a/list-objects.c b/list-objects.c
index 838b6a7..0fb44e7 100644
--- a/list-objects.c
+++ b/list-objects.c
@@ -68,7 +68,7 @@ static void process_tree(struct rev_info *revs,
 	struct tree_desc desc;
 	struct name_entry entry;
 	struct name_path me;
-	int all_interesting = (revs->diffopt.pathspec.nr == 0);
+	int match = revs->diffopt.pathspec.nr == 0 ? 2 : 0;
 	int baselen = base->len;
 
 	if (!revs->tree_objects)
@@ -85,7 +85,7 @@ static void process_tree(struct rev_info *revs,
 	me.elem = name;
 	me.elem_len = strlen(name);
 
-	if (!all_interesting) {
+	if (!match) {
 		strbuf_addstr(base, name);
 		if (base->len)
 			strbuf_addch(base, '/');
@@ -94,17 +94,13 @@ static void process_tree(struct rev_info *revs,
 	init_tree_desc(&desc, tree->buffer, tree->size);
 
 	while (tree_entry(&desc, &entry)) {
-		if (!all_interesting) {
-			int showit = tree_entry_interesting(&entry,
-							    base, 0,
-							    &revs->diffopt.pathspec);
-
-			if (showit < 0)
+		if (match != 2) {
+			match = tree_entry_interesting(&entry, base, 0,
+						       &revs->diffopt.pathspec);
+			if (match < 0)
 				break;
-			else if (!showit)
+			if (match == 0)
 				continue;
-			else if (showit == 2)
-				all_interesting = 1;
 		}
 
 		if (S_ISDIR(entry.mode))
diff --git a/tree-diff.c b/tree-diff.c
index 3954281..f291069 100644
--- a/tree-diff.c
+++ b/tree-diff.c
@@ -65,23 +65,17 @@ static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2,
 static void show_tree(struct diff_options *opt, const char *prefix,
 		      struct tree_desc *desc, struct strbuf *base)
 {
-	int all_interesting = 0;
-	while (desc->size) {
-		int show;
-
-		if (all_interesting)
-			show = 1;
-		else {
-			show = tree_entry_interesting(&desc->entry, base, 0,
-						      &opt->pathspec);
-			if (show == 2)
-				all_interesting = 1;
+	int match = 0;
+	for (; desc->size; update_tree_entry(desc)) {
+		if (match != 2) {
+			match = tree_entry_interesting(&desc->entry, base, 0,
+						       &opt->pathspec);
+			if (match < 0)
+				break;
+			if (match == 0)
+				continue;
 		}
-		if (show < 0)
-			break;
-		if (show)
-			show_entry(opt, prefix, desc, base);
-		update_tree_entry(desc);
+		show_entry(opt, prefix, desc, base);
 	}
 }
 
@@ -121,20 +115,16 @@ static void show_entry(struct diff_options *opt, const char *prefix,
 }
 
 static void skip_uninteresting(struct tree_desc *t, struct strbuf *base,
-			       struct diff_options *opt, int *all_interesting)
+			       struct diff_options *opt, int *match)
 {
 	while (t->size) {
-		int show = tree_entry_interesting(&t->entry, base, 0, &opt->pathspec);
-		if (show == 2)
-			*all_interesting = 1;
-		if (!show) {
-			update_tree_entry(t);
-			continue;
+		*match = tree_entry_interesting(&t->entry, base, 0, &opt->pathspec);
+		if (*match) {
+			if (*match < 0)
+				t->size = 0;
+			break;
 		}
-		/* Skip it all? */
-		if (show < 0)
-			t->size = 0;
-		return;
+		update_tree_entry(t);
 	}
 }
 
@@ -143,8 +133,7 @@ int diff_tree(struct tree_desc *t1, struct tree_desc *t2,
 {
 	struct strbuf base;
 	int baselen = strlen(base_str);
-	int all_t1_interesting = 0;
-	int all_t2_interesting = 0;
+	int t1_match = 0, t2_match = 0;
 
 	/* Enable recursion indefinitely */
 	opt->pathspec.recursive = DIFF_OPT_TST(opt, RECURSIVE);
@@ -158,10 +147,8 @@ int diff_tree(struct tree_desc *t1, struct tree_desc *t2,
 		    DIFF_OPT_TST(opt, HAS_CHANGES))
 			break;
 		if (opt->pathspec.nr) {
-			if (!all_t1_interesting)
-				skip_uninteresting(t1, &base, opt, &all_t1_interesting);
-			if (!all_t2_interesting)
-				skip_uninteresting(t2, &base, opt, &all_t2_interesting);
+			skip_uninteresting(t1, &base, opt, &t1_match);
+			skip_uninteresting(t2, &base, opt, &t2_match);
 		}
 		if (!t1->size) {
 			if (!t2->size)
-- 
1.7.4.74.g639db

  parent reply	other threads:[~2011-03-25  9:35 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-23 10:33 Relative ls-files John Tapsell
2011-03-23 10:49 ` Michael J Gruber
2011-03-23 11:27   ` John Tapsell
2011-03-23 11:28     ` demerphq
2011-03-23 11:42       ` Michael J Gruber
2011-03-23 13:54         ` Martin Langhoff
2011-03-23 14:09           ` Junio C Hamano
2011-03-23 14:14             ` Nguyen Thai Ngoc Duy
2011-03-23 15:20               ` Junio C Hamano
2011-03-23 15:41                 ` Nguyen Thai Ngoc Duy
2011-03-23 22:44                   ` Junio C Hamano
2011-03-24 13:26                     ` Nguyen Thai Ngoc Duy
2011-03-24 14:41                       ` [PATCH 1/2] Reimplement read_tree_recursive() using tree_entry_interesting() Nguyễn Thái Ngọc Duy
2011-03-24 14:41                         ` [PATCH 2/2] Convert read_tree{,_recursive} to support struct pathspec Nguyễn Thái Ngọc Duy
2011-03-24 14:49                           ` Nguyen Thai Ngoc Duy
2011-03-24 19:58                           ` Junio C Hamano
2011-03-24 19:07                         ` [PATCH 1/2] Reimplement read_tree_recursive() using tree_entry_interesting() Junio C Hamano
2011-03-24 19:55                         ` Junio C Hamano
2011-03-25  9:34                           ` [PATCH 1/3] " Nguyễn Thái Ngọc Duy
2011-03-25  9:34                             ` [PATCH 2/3] Convert read_tree{,_recursive} to support struct pathspec Nguyễn Thái Ngọc Duy
2011-03-25  9:34                             ` Nguyễn Thái Ngọc Duy [this message]
2011-03-24 14:47                       ` Relative ls-files Junio C Hamano
2011-03-23 14:46             ` Martin Langhoff
2011-03-23 15:24               ` Junio C Hamano
2011-03-23 17:35             ` Junio C Hamano
2011-03-23 15:42         ` Junio C Hamano
2011-03-23 15:57           ` Michael J Gruber
2011-03-23 16:10             ` Nguyen Thai Ngoc Duy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1301045660-25053-3-git-send-email-pclouds@gmail.com \
    --to=pclouds@gmail.com \
    --cc=git@drmicha.warpmail.net \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).