Improve tree_entry_interesting() handling code
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> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
Mar 25, 2011 at 16:34 UTC
97d0b74a49f0c81c3f9673c1a17721ac0624c3df
3 files changed
+33
-50
builtin/grep.c
+6
-6
@@ -521,18 +521,18 @@ static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec, int
521
static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
522
struct tree_desc *tree, struct strbuf *base, int tn_len)
523
{
524
- int hit = 0, matched = 0;
524
+ int hit = 0, match = 0;
525
struct name_entry entry;
526
int old_baselen = base->len;
527
528
while (tree_entry(tree, &entry)) {
529
int te_len = tree_entry_len(entry.path, entry.sha1);
530
531
- if (matched != 2) {
532
- matched = tree_entry_interesting(&entry, base, tn_len, pathspec);
533
- if (matched == -1)
534
- break; /* no more matches */
535
- if (!matched)
531
+ if (match != 2) {
532
+ match = tree_entry_interesting(&entry, base, tn_len, pathspec);
533
+ if (match < 0)
534
+ break;
535
+ if (match == 0)
536
continue;
537
}
538
list-objects.c
+7
-11
@@ -68,7 +68,7 @@ static void process_tree(struct rev_info *revs,
68
struct tree_desc desc;
69
struct name_entry entry;
70
struct name_path me;
71
- int all_interesting = (revs->diffopt.pathspec.nr == 0);
71
+ int match = revs->diffopt.pathspec.nr == 0 ? 2 : 0;
72
int baselen = base->len;
73
74
if (!revs->tree_objects)
@@ -85,7 +85,7 @@ static void process_tree(struct rev_info *revs,
85
me.elem = name;
86
me.elem_len = strlen(name);
87
88
- if (!all_interesting) {
88
+ if (!match) {
89
strbuf_addstr(base, name);
90
if (base->len)
91
strbuf_addch(base, '/');
@@ -94,17 +94,13 @@ static void process_tree(struct rev_info *revs,
94
init_tree_desc(&desc, tree->buffer, tree->size);
95
96
while (tree_entry(&desc, &entry)) {
97
- if (!all_interesting) {
98
- int showit = tree_entry_interesting(&entry,
99
- base, 0,
100
- &revs->diffopt.pathspec);
101
-
102
- if (showit < 0)
97
+ if (match != 2) {
98
+ match = tree_entry_interesting(&entry, base, 0,
99
+ &revs->diffopt.pathspec);
100
+ if (match < 0)
101
break;
104
- else if (!showit)
102
+ if (match == 0)
103
continue;
106
- else if (showit == 2)
107
- all_interesting = 1;
104
}
105
106
if (S_ISDIR(entry.mode))
tree-diff.c
+20
-33
@@ -65,23 +65,17 @@ static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2,
65
static void show_tree(struct diff_options *opt, const char *prefix,
66
struct tree_desc *desc, struct strbuf *base)
67
{
68
- int all_interesting = 0;
69
- while (desc->size) {
70
- int show;
71
-
72
- if (all_interesting)
73
- show = 1;
74
- else {
75
- show = tree_entry_interesting(&desc->entry, base, 0,
76
- &opt->pathspec);
77
- if (show == 2)
78
- all_interesting = 1;
68
+ int match = 0;
69
+ for (; desc->size; update_tree_entry(desc)) {
70
+ if (match != 2) {
71
+ match = tree_entry_interesting(&desc->entry, base, 0,
72
+ &opt->pathspec);
73
+ if (match < 0)
74
+ break;
75
+ if (match == 0)
76
+ continue;
77
}
80
- if (show < 0)
81
- break;
82
- if (show)
83
- show_entry(opt, prefix, desc, base);
84
- update_tree_entry(desc);
78
+ show_entry(opt, prefix, desc, base);
79
}
80
}
81
@@ -121,20 +115,16 @@ static void show_entry(struct diff_options *opt, const char *prefix,
115
}
116
117
static void skip_uninteresting(struct tree_desc *t, struct strbuf *base,
124
- struct diff_options *opt, int *all_interesting)
118
+ struct diff_options *opt, int *match)
119
{
120
while (t->size) {
127
- int show = tree_entry_interesting(&t->entry, base, 0, &opt->pathspec);
128
- if (show == 2)
129
- *all_interesting = 1;
130
- if (!show) {
131
- update_tree_entry(t);
132
- continue;
121
+ *match = tree_entry_interesting(&t->entry, base, 0, &opt->pathspec);
122
+ if (*match) {
123
+ if (*match < 0)
124
+ t->size = 0;
125
+ break;
126
}
134
- /* Skip it all? */
135
- if (show < 0)
136
- t->size = 0;
137
- return;
127
+ update_tree_entry(t);
128
}
129
}
130
@@ -143,8 +133,7 @@ int diff_tree(struct tree_desc *t1, struct tree_desc *t2,
133
{
134
struct strbuf base;
135
int baselen = strlen(base_str);
146
- int all_t1_interesting = 0;
147
- int all_t2_interesting = 0;
136
+ int t1_match = 0, t2_match = 0;
137
138
/* Enable recursion indefinitely */
139
opt->pathspec.recursive = DIFF_OPT_TST(opt, RECURSIVE);
@@ -158,10 +147,8 @@ int diff_tree(struct tree_desc *t1, struct tree_desc *t2,
147
DIFF_OPT_TST(opt, HAS_CHANGES))
148
break;
149
if (opt->pathspec.nr) {
161
- if (!all_t1_interesting)
162
- skip_uninteresting(t1, &base, opt, &all_t1_interesting);
163
- if (!all_t2_interesting)
164
- skip_uninteresting(t2, &base, opt, &all_t2_interesting);
150
+ skip_uninteresting(t1, &base, opt, &t1_match);
151
+ skip_uninteresting(t2, &base, opt, &t2_match);
152
}
153
if (!t1->size) {
154
if (!t2->size)