Skip to content

Commit 97d0b74

Browse files
pcloudsgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f0096c0 commit 97d0b74

File tree

3 files changed

+33
-50
lines changed

3 files changed

+33
-50
lines changed

builtin/grep.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -521,18 +521,18 @@ static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec, int
521521
static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
522522
struct tree_desc *tree, struct strbuf *base, int tn_len)
523523
{
524-
int hit = 0, matched = 0;
524+
int hit = 0, match = 0;
525525
struct name_entry entry;
526526
int old_baselen = base->len;
527527

528528
while (tree_entry(tree, &entry)) {
529529
int te_len = tree_entry_len(entry.path, entry.sha1);
530530

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)
536536
continue;
537537
}
538538

list-objects.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ static void process_tree(struct rev_info *revs,
6868
struct tree_desc desc;
6969
struct name_entry entry;
7070
struct name_path me;
71-
int all_interesting = (revs->diffopt.pathspec.nr == 0);
71+
int match = revs->diffopt.pathspec.nr == 0 ? 2 : 0;
7272
int baselen = base->len;
7373

7474
if (!revs->tree_objects)
@@ -85,7 +85,7 @@ static void process_tree(struct rev_info *revs,
8585
me.elem = name;
8686
me.elem_len = strlen(name);
8787

88-
if (!all_interesting) {
88+
if (!match) {
8989
strbuf_addstr(base, name);
9090
if (base->len)
9191
strbuf_addch(base, '/');
@@ -94,17 +94,13 @@ static void process_tree(struct rev_info *revs,
9494
init_tree_desc(&desc, tree->buffer, tree->size);
9595

9696
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)
103101
break;
104-
else if (!showit)
102+
if (match == 0)
105103
continue;
106-
else if (showit == 2)
107-
all_interesting = 1;
108104
}
109105

110106
if (S_ISDIR(entry.mode))

tree-diff.c

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -65,23 +65,17 @@ static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2,
6565
static void show_tree(struct diff_options *opt, const char *prefix,
6666
struct tree_desc *desc, struct strbuf *base)
6767
{
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;
7977
}
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);
8579
}
8680
}
8781

@@ -121,20 +115,16 @@ static void show_entry(struct diff_options *opt, const char *prefix,
121115
}
122116

123117
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)
125119
{
126120
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;
133126
}
134-
/* Skip it all? */
135-
if (show < 0)
136-
t->size = 0;
137-
return;
127+
update_tree_entry(t);
138128
}
139129
}
140130

@@ -143,8 +133,7 @@ int diff_tree(struct tree_desc *t1, struct tree_desc *t2,
143133
{
144134
struct strbuf base;
145135
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;
148137

149138
/* Enable recursion indefinitely */
150139
opt->pathspec.recursive = DIFF_OPT_TST(opt, RECURSIVE);
@@ -158,10 +147,8 @@ int diff_tree(struct tree_desc *t1, struct tree_desc *t2,
158147
DIFF_OPT_TST(opt, HAS_CHANGES))
159148
break;
160149
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);
165152
}
166153
if (!t1->size) {
167154
if (!t2->size)

0 commit comments

Comments
 (0)