fetch-pack: sort incoming heads

There's no reason to preserve the incoming order of the heads we're requested to fetch. By having them sorted, we can replace some of the quadratic algorithms with linear ones. 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 443596850f464c768624e6b6477acadc2aa35e8f
1 file changed +9 -1
builtin/fetch-pack.c
+9 -1
@@ -1057,6 +1057,11 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
1057 return ret;
1058 }
1059
1060 +static int compare_heads(const void *a, const void *b)
1061 +{
1062 + return strcmp(*(const char **)a, *(const char **)b);
1063 +}
1064 +
1065 struct ref *fetch_pack(struct fetch_pack_args *my_args,
1066 int fd[], struct child_process *conn,
1067 const struct ref *ref,
@@ -1076,8 +1081,11 @@ struct ref *fetch_pack(struct fetch_pack_args *my_args,
1081 st.st_mtime = 0;
1082 }
1083
1079 - if (heads && nr_heads)
1084 + if (heads && nr_heads) {
1085 nr_heads = remove_duplicates(nr_heads, heads);
1086 + qsort(heads, nr_heads, sizeof(*heads), compare_heads);
1087 + }
1088 +
1089 if (!ref) {
1090 packet_flush(fd[1]);
1091 die("no matching remote head");