clear_commit_marks(): avoid deep recursion
Before this patch, clear_commit_marks() recursed for each parent. This could be potentially very expensive in terms of stack space. Probably the only reason that this did not lead to problems is the fact that we typically call clear_commit_marks() after marking a relatively small set of commits. Use (sort of) a tail recursion instead: first recurse on the parents other than the first one, and then continue the loop with the first parent. Noticed by Shawn Pearce. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Lars Hjemli <hjemli@gmail.com> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Johannes Schindelin committed
Oct 10, 2007 at 23:14 UTC
60fcc2e6ce255da1baa92905c10456981d260fa0
1 file changed
+14
-9
commit.c
+14
-9
@@ -441,17 +441,22 @@ struct commit *pop_most_recent_commit(struct commit_list **list,
441
442
void clear_commit_marks(struct commit *commit, unsigned int mark)
443
{
444
- struct commit_list *parents;
444
+ while (commit) {
445
+ struct commit_list *parents;
446
446
- commit->object.flags &= ~mark;
447
- parents = commit->parents;
448
- while (parents) {
449
- struct commit *parent = parents->item;
447
+ if (!(mark & commit->object.flags))
448
+ return;
449
451
- /* Have we already cleared this? */
452
- if (mark & parent->object.flags)
453
- clear_commit_marks(parent, mark);
454
- parents = parents->next;
450
+ commit->object.flags &= ~mark;
451
+
452
+ parents = commit->parents;
453
+ if (!parents)
454
+ return;
455
+
456
+ while ((parents = parents->next))
457
+ clear_commit_marks(parents->item, mark);
458
+
459
+ commit = commit->parents->item;
460
}
461
}
462