]> git.ipfire.org Git - thirdparty/git.git/commitdiff
mark_parents_uninteresting(): replace list with stack
authorJeff King <peff@peff.net>
Fri, 11 May 2018 18:02:27 +0000 (14:02 -0400)
committerJunio C Hamano <gitster@pobox.com>
Sun, 13 May 2018 02:23:59 +0000 (11:23 +0900)
The mark_parents_uninteresting() function uses two loops:
an outer one to process our queue of pending parents, and an
inner one to process first-parent chains. This is a clever
optimization from 941ba8db57 (Eliminate recursion in
setting/clearing marks in commit list, 2012-01-14) to limit
the number of linked-list allocations when following
single-parent chains.

Unfortunately, this makes the result a little hard to read.
Let's replace the list with a stack. Then we don't have to
worry about doing this double-loop optimization, as we'll
just reuse the top element of the stack as we pop/push.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
revision.c

index e1fa9b13b9d9320256c647a1bba9c12eff6629cf..125327062fab067b19fc69d0a6878b7b2269ea55 100644 (file)
@@ -91,38 +91,57 @@ void mark_tree_uninteresting(struct tree *tree)
        mark_tree_contents_uninteresting(tree);
 }
 
-void mark_parents_uninteresting(struct commit *commit)
+struct commit_stack {
+       struct commit **items;
+       size_t nr, alloc;
+};
+#define COMMIT_STACK_INIT { NULL, 0, 0 }
+
+static void commit_stack_push(struct commit_stack *stack, struct commit *commit)
 {
-       struct commit_list *parents = NULL, *l;
+       ALLOC_GROW(stack->items, stack->nr + 1, stack->alloc);
+       stack->items[stack->nr++] = commit;
+}
 
-       for (l = commit->parents; l; l = l->next)
-               commit_list_insert(l->item, &parents);
+static struct commit *commit_stack_pop(struct commit_stack *stack)
+{
+       return stack->nr ? stack->items[--stack->nr] : NULL;
+}
 
-       while (parents) {
-               struct commit *commit = pop_commit(&parents);
+static void commit_stack_clear(struct commit_stack *stack)
+{
+       FREE_AND_NULL(stack->items);
+       stack->nr = stack->alloc = 0;
+}
 
-               while (commit) {
-                       if (commit->object.flags & UNINTERESTING)
-                               break;
+void mark_parents_uninteresting(struct commit *commit)
+{
+       struct commit_stack pending = COMMIT_STACK_INIT;
+       struct commit_list *l;
 
-                       commit->object.flags |= UNINTERESTING;
+       for (l = commit->parents; l; l = l->next)
+               commit_stack_push(&pending, l->item);
 
-                       /*
-                        * Normally we haven't parsed the parent
-                        * yet, so we won't have a parent of a parent
-                        * here. However, it may turn out that we've
-                        * reached this commit some other way (where it
-                        * wasn't uninteresting), in which case we need
-                        * to mark its parents recursively too..
-                        */
-                       if (!commit->parents)
-                               break;
+       while (pending.nr > 0) {
+               struct commit *commit = commit_stack_pop(&pending);
 
-                       for (l = commit->parents->next; l; l = l->next)
-                               commit_list_insert(l->item, &parents);
-                       commit = commit->parents->item;
-               }
+               if (commit->object.flags & UNINTERESTING)
+                       return;
+               commit->object.flags |= UNINTERESTING;
+
+               /*
+                * Normally we haven't parsed the parent
+                * yet, so we won't have a parent of a parent
+                * here. However, it may turn out that we've
+                * reached this commit some other way (where it
+                * wasn't uninteresting), in which case we need
+                * to mark its parents recursively too..
+                */
+               for (l = commit->parents; l; l = l->next)
+                       commit_stack_push(&pending, l->item);
        }
+
+       commit_stack_clear(&pending);
 }
 
 static void add_pending_object_with_path(struct rev_info *revs,