mark_parents_uninteresting(): avoid most allocation

Commit 941ba8db57 (Eliminate recursion in setting/clearing marks in commit list, 2012-01-14) used a clever double-loop to avoid allocations for single-parent chains of history. However, it did so only when following parents of parents (which was an uncommon case), and _always_ incurred at least one allocation to populate the list of pending parents in the first place. We can turn this into zero-allocation in the common case by iterating directly over the initial parent list, and then following up on any pending items we might have discovered. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed May 11, 2018 at 14:03 UTC 8702b30fd7b6c804f1bb59877a4c2f773fbe00f8
1 file changed +25 -19
revision.c
+25 -19
@@ -114,32 +114,38 @@ static void commit_stack_clear(struct commit_stack *stack)
114 stack->nr = stack->alloc = 0;
115 }
116
117 -void mark_parents_uninteresting(struct commit *commit)
117 +static void mark_one_parent_uninteresting(struct commit *commit,
118 + struct commit_stack *pending)
119 {
119 - struct commit_stack pending = COMMIT_STACK_INIT;
120 struct commit_list *l;
121
122 + if (commit->object.flags & UNINTERESTING)
123 + return;
124 + commit->object.flags |= UNINTERESTING;
125 +
126 + /*
127 + * Normally we haven't parsed the parent
128 + * yet, so we won't have a parent of a parent
129 + * here. However, it may turn out that we've
130 + * reached this commit some other way (where it
131 + * wasn't uninteresting), in which case we need
132 + * to mark its parents recursively too..
133 + */
134 for (l = commit->parents; l; l = l->next)
123 - commit_stack_push(&pending, l->item);
135 + commit_stack_push(pending, l->item);
136 +}
137
125 - while (pending.nr > 0) {
126 - struct commit *commit = commit_stack_pop(&pending);
138 +void mark_parents_uninteresting(struct commit *commit)
139 +{
140 + struct commit_stack pending = COMMIT_STACK_INIT;
141 + struct commit_list *l;
142
128 - if (commit->object.flags & UNINTERESTING)
129 - return;
130 - commit->object.flags |= UNINTERESTING;
143 + for (l = commit->parents; l; l = l->next)
144 + mark_one_parent_uninteresting(l->item, &pending);
145
132 - /*
133 - * Normally we haven't parsed the parent
134 - * yet, so we won't have a parent of a parent
135 - * here. However, it may turn out that we've
136 - * reached this commit some other way (where it
137 - * wasn't uninteresting), in which case we need
138 - * to mark its parents recursively too..
139 - */
140 - for (l = commit->parents; l; l = l->next)
141 - commit_stack_push(&pending, l->item);
142 - }
146 + while (pending.nr > 0)
147 + mark_one_parent_uninteresting(commit_stack_pop(&pending),
148 + &pending);
149
150 commit_stack_clear(&pending);
151 }