Skip to content

Commit fbc21e3

Browse files
derrickstoleegitster
authored andcommitted
commit-reach: use one walk in remove_redundant()
The current implementation of remove_redundant() uses several calls to paint_down_to_common() to determine that commits are independent of each other. This leads to quadratic behavior when many inputs are passed to commands such as 'git merge-base'. For example, in the Linux kernel repository, I tested the performance by passing all tags: git merge-base --independent $(git for-each-ref refs/tags --format="$(refname)") (Note: I had to delete the tags v2.6.11-tree and v2.6.11 as they do not point to commits.) Here is the performance improvement introduced by this change: Before: 16.4s After: 1.1s This performance improvement requires the commit-graph file to be present. We keep the old algorithm around as remove_redundant_no_gen() and use it when generation_numbers_enabled() is false. This is similar to other algorithms within commit-reach.c. The new algorithm is implemented in remove_redundant_with_gen(). The basic approach is to do one commit walk instead of many. First, scan all commits in the list and mark their _parents_ with the STALE flag. This flag will indicate commits that are reachable from one of the inputs, except not including themselves. Then, walk commits until covering all commits up to the minimum generation number pushing the STALE flag throughout. At the end, we need to clear the STALE bit from all of the commits we walked. We move the non-stale commits in 'array' to the beginning of the list, and this might overwrite stale commits. However, we store an array of commits that started the walk, and use clear_commit_marks() on each of those starting commits. That method will walk the reachable commits with the STALE bit and clear them all. This makes the algorithm safe for re-entry or for other uses of those commits after this walk. This logic is covered by tests in t6600-test-reach.sh, so the behavior does not change. This is tested both in the case with a commit-graph and without. Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0fac156 commit fbc21e3

File tree

1 file changed

+96
-8
lines changed

1 file changed

+96
-8
lines changed

commit-reach.c

Lines changed: 96 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,9 @@ struct commit_list *get_octopus_merge_bases(struct commit_list *in)
156156
return ret;
157157
}
158158

159-
static int remove_redundant(struct repository *r, struct commit **array, int cnt)
159+
static int remove_redundant_no_gen(struct repository *r,
160+
struct commit **array, int cnt)
160161
{
161-
/*
162-
* Some commit in the array may be an ancestor of
163-
* another commit. Move the independent commits to the
164-
* beginning of 'array' and return their number. Callers
165-
* should not rely upon the contents of 'array' after
166-
* that number.
167-
*/
168162
struct commit **work;
169163
unsigned char *redundant;
170164
int *filled_index;
@@ -216,6 +210,100 @@ static int remove_redundant(struct repository *r, struct commit **array, int cnt
216210
return filled;
217211
}
218212

213+
static int remove_redundant_with_gen(struct repository *r,
214+
struct commit **array, int cnt)
215+
{
216+
int i, count_non_stale = 0;
217+
timestamp_t min_generation = GENERATION_NUMBER_INFINITY;
218+
struct commit **walk_start;
219+
size_t walk_start_nr = 0, walk_start_alloc = cnt;
220+
struct prio_queue queue = { compare_commits_by_gen_then_commit_date };
221+
222+
ALLOC_ARRAY(walk_start, walk_start_alloc);
223+
224+
/* Mark all parents of the input as STALE */
225+
for (i = 0; i < cnt; i++) {
226+
struct commit_list *parents;
227+
timestamp_t generation;
228+
229+
repo_parse_commit(r, array[i]);
230+
parents = array[i]->parents;
231+
232+
while (parents) {
233+
repo_parse_commit(r, parents->item);
234+
if (!(parents->item->object.flags & STALE)) {
235+
parents->item->object.flags |= STALE;
236+
ALLOC_GROW(walk_start, walk_start_nr + 1, walk_start_alloc);
237+
walk_start[walk_start_nr++] = parents->item;
238+
prio_queue_put(&queue, parents->item);
239+
}
240+
parents = parents->next;
241+
}
242+
243+
generation = commit_graph_generation(array[i]);
244+
245+
if (generation < min_generation)
246+
min_generation = generation;
247+
}
248+
249+
/* push the STALE bits up to min generation */
250+
while (queue.nr) {
251+
struct commit_list *parents;
252+
struct commit *c = prio_queue_get(&queue);
253+
254+
repo_parse_commit(r, c);
255+
256+
if (commit_graph_generation(c) < min_generation)
257+
continue;
258+
259+
parents = c->parents;
260+
while (parents) {
261+
if (!(parents->item->object.flags & STALE)) {
262+
parents->item->object.flags |= STALE;
263+
prio_queue_put(&queue, parents->item);
264+
}
265+
parents = parents->next;
266+
}
267+
}
268+
269+
/* rearrange array */
270+
for (i = count_non_stale = 0; i < cnt; i++) {
271+
if (!(array[i]->object.flags & STALE))
272+
array[count_non_stale++] = array[i];
273+
}
274+
275+
/* clear marks */
276+
clear_commit_marks_many(walk_start_nr, walk_start, STALE);
277+
free(walk_start);
278+
279+
return count_non_stale;
280+
}
281+
282+
static int remove_redundant(struct repository *r, struct commit **array, int cnt)
283+
{
284+
/*
285+
* Some commit in the array may be an ancestor of
286+
* another commit. Move the independent commits to the
287+
* beginning of 'array' and return their number. Callers
288+
* should not rely upon the contents of 'array' after
289+
* that number.
290+
*/
291+
if (generation_numbers_enabled(r)) {
292+
int i;
293+
294+
/*
295+
* If we have a single commit with finite generation
296+
* number, then the _with_gen algorithm is preferred.
297+
*/
298+
for (i = 0; i < cnt; i++) {
299+
if (commit_graph_generation(array[i]) < GENERATION_NUMBER_INFINITY)
300+
return remove_redundant_with_gen(r, array, cnt);
301+
}
302+
}
303+
304+
return remove_redundant_no_gen(r, array, cnt);
305+
}
306+
219307
static struct commit_list *get_merge_bases_many_0(struct repository *r,
220308
struct commit *one,
221309
int n,

0 commit comments

Comments
 (0)