{"thread":{"id":"7682","subject":"[PATCH 4/9] pack-objects: clean up list sorting","startedAt":"2007-04-16T16:14:52Z","lastAt":"2007-04-16T16:14:52Z","messageCount":1,"participants":["Nicolas Pitre"],"isPatch":true,"patchVersion":1,"patchTotal":9},"messages":[{"id":"39537","messageId":"S1030818AbXDPQO4/20070416161458Z+64@vger.kernel.org","threadId":"7682","inReplyTo":null,"subject":"[PATCH 4/9] pack-objects: clean up list sorting","fromName":"Nicolas Pitre","fromEmail":"nico@cam.org","sentAt":"2007-04-16T16:14:52Z","receivedAt":"2007-04-16T16:14:52Z","isPatch":true,"sender":{"key":"nico@fluxnic.net","avatar":"https://avatars.githubusercontent.com/u/702790?v=4"},"body":"Get rid of sort_comparator() as it impose a run time double indirect\nfunction call for little compile time type checking gain.\n\nAlso get rid of create_sorted_list() as it only has one user which would\nas well be just fine doing its sorting locally.  Eventually the list of\ndeltifiable objects might be shorter than the whole object list.\n\nSigned-off-by: Nicolas Pitre <nico@cam.org>\n---\n builtin-pack-objects.c |   53 +++++++++++++++++++----------------------------\n 1 files changed, 22 insertions(+), 31 deletions(-)\n\ndiff --git a/builtin-pack-objects.c b/builtin-pack-objects.c\nindex d44b8f4..15119d6 100644\n--- a/builtin-pack-objects.c\n+++ b/builtin-pack-objects.c\n@@ -66,7 +66,7 @@ static int local;\n static int incremental;\n static int allow_ofs_delta;\n \n-static struct object_entry **sorted_by_sha, **sorted_by_type;\n+static struct object_entry **sorted_by_sha;\n static struct object_entry *objects;\n static uint32_t nr_objects, nr_alloc, nr_result;\n static const char *base_name;\n@@ -1181,31 +1181,10 @@ static void get_object_details(void)\n \t\tcheck_object(entry);\n }\n \n-typedef int (*entry_sort_t)(const struct object_entry *, const struct object_entry *);\n-\n-static entry_sort_t current_sort;\n-\n-static int sort_comparator(const void *_a, const void *_b)\n-{\n-\tstruct object_entry *a = *(struct object_entry **)_a;\n-\tstruct object_entry *b = *(struct object_entry **)_b;\n-\treturn current_sort(a,b);\n-}\n-\n-static struct object_entry **create_sorted_list(entry_sort_t sort)\n-{\n-\tstruct object_entry **list = xmalloc(nr_objects * sizeof(struct object_entry *));\n-\tuint32_t i;\n-\n-\tfor (i = 0; i < nr_objects; i++)\n-\t\tlist[i] = objects + i;\n-\tcurrent_sort = sort;\n-\tqsort(list, nr_objects, sizeof(struct object_entry *), sort_comparator);\n-\treturn list;\n-}\n-\n-static int sha1_sort(const struct object_entry *a, const struct object_entry *b)\n+static int sha1_sort(const void *_a, const void *_b)\n {\n+\tconst struct object_entry *a = *(struct object_entry **)_a;\n+\tconst struct object_entry *b = *(struct object_entry **)_b;\n \treturn hashcmp(a->sha1, b->sha1);\n }\n \n@@ -1222,13 +1201,15 @@ static struct object_entry **create_final_object_list(void)\n \t\tif (!objects[i].preferred_base)\n \t\t\tlist[j++] = objects + i;\n \t}\n-\tcurrent_sort = sha1_sort;\n-\tqsort(list, nr_result, sizeof(struct object_entry *), sort_comparator);\n+\tqsort(list, nr_result, sizeof(struct object_entry *), sha1_sort);\n \treturn list;\n }\n \n-static int type_size_sort(const struct object_entry *a, const struct object_entry *b)\n+static int type_size_sort(const void *_a, const void *_b)\n {\n+\tconst struct object_entry *a = *(struct object_entry **)_a;\n+\tconst struct object_entry *b = *(struct object_entry **)_b;\n+\n \tif (a->type < b->type)\n \t\treturn -1;\n \tif (a->type > b->type)\n@@ -1448,10 +1429,20 @@ static void find_deltas(struct object_entry **list, int window, int depth)\n \n static void prepare_pack(int window, int depth)\n {\n+\tstruct object_entry **delta_list;\n+\tuint32_t i;\n+\n \tget_object_details();\n-\tsorted_by_type = create_sorted_list(type_size_sort);\n-\tif (window && depth)\n-\t\tfind_deltas(sorted_by_type, window+1, depth);\n+\n+\tif (!window || !depth)\n+\t\treturn;\n+\n+\tdelta_list = xmalloc(nr_objects * sizeof(*delta_list));\n+\tfor (i = 0; i < nr_objects; i++)\n+\t\tdelta_list[i] = objects + i;\n+\tqsort(delta_list, nr_objects, sizeof(*delta_list), type_size_sort);\n+\tfind_deltas(delta_list, window+1, depth);\n+\tfree(delta_list);\n }\n \n static int reuse_cached_pack(unsigned char *sha1)\n-- \n1.5.1.1.781.g65e8\n"}]}