{"thread":{"id":"7604","subject":"[PATCH] clean up add_object_entry()","startedAt":"2007-04-11T02:54:36Z","lastAt":"2007-04-11T02:54:36Z","messageCount":1,"participants":["Nicolas Pitre"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"39081","messageId":"alpine.LFD.0.98.0704102248270.28181@xanadu.home","threadId":"7604","inReplyTo":null,"subject":"[PATCH] clean up add_object_entry()","fromName":"Nicolas Pitre","fromEmail":"nico@cam.org","sentAt":"2007-04-11T02:54:36Z","receivedAt":"2007-04-11T02:54:36Z","isPatch":true,"sender":{"key":"nico@fluxnic.net","avatar":"https://avatars.githubusercontent.com/u/702790?v=4"},"body":"This function used to call locate_object_entry_hash() _twice_ per added \nobject while only once should suffice. Let's reorganize that code a bit.\n\nSigned-off-by: Nicolas Pitre <nico@cam.org>\n---\n\ndiff --git a/builtin-pack-objects.c b/builtin-pack-objects.c\nindex 687b4b5..bc5f232 100644\n--- a/builtin-pack-objects.c\n+++ b/builtin-pack-objects.c\n@@ -781,12 +781,19 @@ static unsigned name_hash(const char *name)\n \n static int add_object_entry(const unsigned char *sha1, unsigned hash, int exclude)\n {\n-\tuint32_t idx = nr_objects;\n \tstruct object_entry *entry;\n-\tstruct packed_git *p;\n+\tstruct packed_git *p, *found_pack = NULL;\n \toff_t found_offset = 0;\n-\tstruct packed_git *found_pack = NULL;\n-\tint ix, status = 0;\n+\tint ix;\n+\n+\tix = nr_objects ? locate_object_entry_hash(sha1) : -1;\n+\tif (ix >= 0) {\n+\t\tif (exclude) {\n+\t\t\tentry = objects + object_ix[ix] - 1;\n+\t\t\tentry->preferred_base = 1;\n+\t\t}\n+\t\treturn 0;\n+\t}\n \n \tif (!exclude) {\n \t\tfor (p = packed_git; p; p = p->next) {\n@@ -803,43 +810,34 @@ static int add_object_entry(const unsigned char *sha1, unsigned hash, int exclud\n \t\t\t}\n \t\t}\n \t}\n-\tif ((entry = locate_object_entry(sha1)) != NULL)\n-\t\tgoto already_added;\n \n-\tif (idx >= nr_alloc) {\n-\t\tnr_alloc = (idx + 1024) * 3 / 2;\n+\tif (nr_objects >= nr_alloc) {\n+\t\tnr_alloc = (nr_alloc  + 1024) * 3 / 2;\n \t\tobjects = xrealloc(objects, nr_alloc * sizeof(*entry));\n \t}\n-\tentry = objects + idx;\n-\tnr_objects = idx + 1;\n+\n+\tentry = objects + nr_objects++;\n \tmemset(entry, 0, sizeof(*entry));\n \thashcpy(entry->sha1, sha1);\n \tentry->hash = hash;\n+\tif (exclude)\n+\t\tentry->preferred_base = 1;\n+\tif (found_pack) {\n+\t\tentry->in_pack = found_pack;\n+\t\tentry->in_pack_offset = found_offset;\n+\t}\n \n \tif (object_ix_hashsz * 3 <= nr_objects * 4)\n \t\trehash_objects();\n-\telse {\n-\t\tix = locate_object_entry_hash(entry->sha1);\n-\t\tif (0 <= ix)\n-\t\t\tdie(\"internal error in object hashing.\");\n-\t\tobject_ix[-1 - ix] = idx + 1;\n-\t}\n-\tstatus = 1;\n+\telse\n+\t\tobject_ix[-1 - ix] = nr_objects;\n \n- already_added:\n \tif (progress_update) {\n \t\tfprintf(stderr, \"Counting objects...%u\\r\", nr_objects);\n \t\tprogress_update = 0;\n \t}\n-\tif (exclude)\n-\t\tentry->preferred_base = 1;\n-\telse {\n-\t\tif (found_pack) {\n-\t\t\tentry->in_pack = found_pack;\n-\t\t\tentry->in_pack_offset = found_offset;\n-\t\t}\n-\t}\n-\treturn status;\n+\n+\treturn 1;\n }\n \n struct pbase_tree_cache {\n"}]}