Skip to content

Commit

Permalink
clear_commit_marks(): avoid deep recursion
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Johannes Schindelin authored and Shawn O. Pearce committed Oct 16, 2007
1 parent 7b40a45 commit 60fcc2e
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -441,17 +441,22 @@ struct commit *pop_most_recent_commit(struct commit_list **list,

void clear_commit_marks(struct commit *commit, unsigned int mark)
{
struct commit_list *parents;
while (commit) {
struct commit_list *parents;

commit->object.flags &= ~mark;
parents = commit->parents;
while (parents) {
struct commit *parent = parents->item;
if (!(mark & commit->object.flags))
return;

/* Have we already cleared this? */
if (mark & parent->object.flags)
clear_commit_marks(parent, mark);
parents = parents->next;
commit->object.flags &= ~mark;

parents = commit->parents;
if (!parents)
return;

while ((parents = parents->next))
clear_commit_marks(parents->item, mark);

commit = commit->parents->item;
}
}

Expand Down

0 comments on commit 60fcc2e

Please sign in to comment.