@matheusbsbs / matheus-bagliano / commits / 9e5f4a5539
Fork 0

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>

Junio C Hamano committed Nov 10, 2005 at 22:41 UTC 9e5f4a5539fe0e6ed31f26f34f1832a729facf4b
1 file changed +43 -34
merge-base.c
+43 -34
@@ -123,6 +123,47 @@ static struct commit *interesting(struct commit_list *list)
123
124 static int show_all = 0;
125
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 +
167 static int merge_base(struct commit *rev1, struct commit *rev2)
168 {
169 struct commit_list *list = NULL;
@@ -171,40 +212,8 @@ static int merge_base(struct commit *rev1, struct commit *rev2)
212 if (!result)
213 return 1;
214
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);
217
218 while (result) {
219 struct commit *commit = result->item;