Skip to content

Commit

Permalink
fetch-pack: avoid quadratic behavior in remove_duplicates
Browse files Browse the repository at this point in the history
We remove duplicate entries from the list of refs we are
fed in fetch-pack. The original algorithm is quadratic over
the number of refs, but since the list is now guaranteed to
be sorted, we can do it in linear time.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Jeff King authored and Junio C Hamano committed May 22, 2012
1 parent 4435968 commit 7db8d53
Showing 1 changed file with 6 additions and 15 deletions.
21 changes: 6 additions & 15 deletions builtin/fetch-pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -834,21 +834,12 @@ static int remove_duplicates(int nr_heads, char **heads)
{
int src, dst;

for (src = dst = 0; src < nr_heads; src++) {
/* If heads[src] is different from any of
* heads[0..dst], push it in.
*/
int i;
for (i = 0; i < dst; i++) {
if (!strcmp(heads[i], heads[src]))
break;
}
if (i < dst)
continue;
if (src != dst)
heads[dst] = heads[src];
dst++;
}
if (!nr_heads)
return 0;

for (src = dst = 1; src < nr_heads; src++)
if (strcmp(heads[src], heads[dst-1]))
heads[dst++] = heads[src];
return dst;
}

Expand Down

0 comments on commit 7db8d53

Please sign in to comment.