Skip to content

Commit 9e5f4a5

Browse files
author
Junio C Hamano
committed
merge-base: avoid unnecessary postprocessing.
When we have only one merge-base candidates in the result list, there is no point going back to mark the reachable commits again. And that is the most common case, so try not to waste time on it. Suggested by Linus. Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent ed9a540 commit 9e5f4a5

File tree

1 file changed

+43
-34
lines changed

1 file changed

+43
-34
lines changed

merge-base.c

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,47 @@ static struct commit *interesting(struct commit_list *list)
123123

124124
static int show_all = 0;
125125

126+
static void mark_reachable_commits(struct commit_list *result,
127+
struct commit_list *list)
128+
{
129+
struct commit_list *tmp;
130+
131+
/*
132+
* Postprocess to fully contaminate the well.
133+
*/
134+
for (tmp = result; tmp; tmp = tmp->next) {
135+
struct commit *c = tmp->item;
136+
/* Reinject uninteresting ones to list,
137+
* so we can scan their parents.
138+
*/
139+
if (c->object.flags & UNINTERESTING)
140+
commit_list_insert(c, &list);
141+
}
142+
while (list) {
143+
struct commit *c = list->item;
144+
struct commit_list *parents;
145+
146+
tmp = list;
147+
list = list->next;
148+
free(tmp);
149+
150+
/* Anything taken out of the list is uninteresting, so
151+
* mark all its parents uninteresting. We do not
152+
* parse new ones (we already parsed all the relevant
153+
* ones).
154+
*/
155+
parents = c->parents;
156+
while (parents) {
157+
struct commit *p = parents->item;
158+
parents = parents->next;
159+
if (!(p->object.flags & UNINTERESTING)) {
160+
p->object.flags |= UNINTERESTING;
161+
commit_list_insert(p, &list);
162+
}
163+
}
164+
}
165+
}
166+
126167
static int merge_base(struct commit *rev1, struct commit *rev2)
127168
{
128169
struct commit_list *list = NULL;
@@ -171,40 +212,8 @@ static int merge_base(struct commit *rev1, struct commit *rev2)
171212
if (!result)
172213
return 1;
173214

174-
/*
175-
* Postprocess to fully contaminate the well.
176-
*/
177-
for (tmp = result; tmp; tmp = tmp->next) {
178-
struct commit *c = tmp->item;
179-
/* Reinject uninteresting ones to list,
180-
* so we can scan their parents.
181-
*/
182-
if (c->object.flags & UNINTERESTING)
183-
commit_list_insert(c, &list);
184-
}
185-
while (list) {
186-
struct commit *c = list->item;
187-
struct commit_list *parents;
188-
189-
tmp = list;
190-
list = list->next;
191-
free(tmp);
192-
193-
/* Anything taken out of the list is uninteresting, so
194-
* mark all its parents uninteresting. We do not
195-
* parse new ones (we already parsed all the relevant
196-
* ones).
197-
*/
198-
parents = c->parents;
199-
while (parents) {
200-
struct commit *p = parents->item;
201-
parents = parents->next;
202-
if (!(p->object.flags & UNINTERESTING)) {
203-
p->object.flags |= UNINTERESTING;
204-
commit_list_insert(p, &list);
205-
}
206-
}
207-
}
215+
if (result->next && list)
216+
mark_reachable_commits(result, list);
208217

209218
while (result) {
210219
struct commit *commit = result->item;

0 commit comments

Comments
 (0)