fetch-pack: avoid quadratic behavior in remove_duplicates
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>
This commit is contained in:
parent
443596850f
commit
7db8d5370f
@ -834,21 +834,12 @@ static int remove_duplicates(int nr_heads, char **heads)
|
|||||||
{
|
{
|
||||||
int src, dst;
|
int src, dst;
|
||||||
|
|
||||||
for (src = dst = 0; src < nr_heads; src++) {
|
if (!nr_heads)
|
||||||
/* If heads[src] is different from any of
|
return 0;
|
||||||
* heads[0..dst], push it in.
|
|
||||||
*/
|
for (src = dst = 1; src < nr_heads; src++)
|
||||||
int i;
|
if (strcmp(heads[src], heads[dst-1]))
|
||||||
for (i = 0; i < dst; i++) {
|
heads[dst++] = heads[src];
|
||||||
if (!strcmp(heads[i], heads[src]))
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (i < dst)
|
|
||||||
continue;
|
|
||||||
if (src != dst)
|
|
||||||
heads[dst] = heads[src];
|
|
||||||
dst++;
|
|
||||||
}
|
|
||||||
return dst;
|
return dst;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user