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>
Jeff King committed
May 21, 2012 at 18:17 UTC
7db8d5370f070ab983e1bc3569380b8aca899c6d
1 file changed
+6
-15
builtin/fetch-pack.c
+6
-15
@@ -834,21 +834,12 @@ static int remove_duplicates(int nr_heads, char **heads)
834
{
835
int src, dst;
836
837
- for (src = dst = 0; src < nr_heads; src++) {
838
- /* If heads[src] is different from any of
839
- * heads[0..dst], push it in.
840
- */
841
- int i;
842
- for (i = 0; i < dst; i++) {
843
- if (!strcmp(heads[i], heads[src]))
844
- break;
845
- }
846
- if (i < dst)
847
- continue;
848
- if (src != dst)
849
- heads[dst] = heads[src];
850
- dst++;
851
- }
837
+ if (!nr_heads)
838
+ return 0;
839
+
840
+ for (src = dst = 1; src < nr_heads; src++)
841
+ if (strcmp(heads[src], heads[dst-1]))
842
+ heads[dst++] = heads[src];
843
return dst;
844
}
845