{"thread":{"id":"6379","subject":"Re: [PATCH] Remove hash in git-describe in favor of util slot.","startedAt":"2007-01-15T03:16:55Z","lastAt":"2007-01-15T05:08:58Z","messageCount":2,"participants":["Junio C Hamano","Shawn O. Pearce"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"31715","messageId":"20070115031655.GA11809@spearce.org","threadId":"6379","inReplyTo":null,"subject":"[PATCH] Remove hash in git-describe in favor of util slot.","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2007-01-15T03:16:55Z","receivedAt":"2007-01-15T03:16:55Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"Currently we don't use the util field of struct commit but we want\nfast access to the highest priority name that references any given\ncommit object during our matching loop.  A really simple approach\nis to just store the name directly in the util field.\n\nSigned-off-by: Shawn O. Pearce <spearce@spearce.org>\n---\n\n This removes more lines than it adds, so it must be good, right?\n :-)\n\n builtin-describe.c |   76 ++++++++-------------------------------------------\n 1 files changed, 12 insertions(+), 64 deletions(-)\n\ndiff --git a/builtin-describe.c b/builtin-describe.c\nindex e38c899..e7b8f95 100644\n--- a/builtin-describe.c\n+++ b/builtin-describe.c\n@@ -16,60 +16,27 @@ static int tags;\t/* But allow any tags if --tags is specified */\n static int abbrev = DEFAULT_ABBREV;\n static int max_candidates = 10;\n \n-static unsigned int names[256], allocs[256];\n-static struct commit_name {\n-\tstruct commit *commit;\n+struct commit_name {\n \tint prio; /* annotated tag = 2, tag = 1, head = 0 */\n \tchar path[FLEX_ARRAY]; /* more */\n-} **name_array[256];\n+};\n static const char *prio_names[] = {\n \t\"head\", \"lightweight\", \"annotated\",\n };\n \n-static struct commit_name *match(struct commit *cmit)\n-{\n-\tunsigned char level0 = cmit->object.sha1[0];\n-\tstruct commit_name **p = name_array[level0];\n-\tunsigned int hi = names[level0];\n-\tunsigned int lo = 0;\n-\n-\twhile (lo < hi) {\n-\t\tunsigned int mi = (lo + hi) / 2;\n-\t\tint cmp = hashcmp(p[mi]->commit->object.sha1,\n-\t\t\tcmit->object.sha1);\n-\t\tif (!cmp) {\n-\t\t\twhile (mi && p[mi - 1]->commit == cmit)\n-\t\t\t\tmi--;\n-\t\t\treturn p[mi];\n-\t\t}\n-\t\tif (cmp > 0)\n-\t\t\thi = mi;\n-\t\telse\n-\t\t\tlo = mi+1;\n-\t}\n-\treturn NULL;\n-}\n-\n static void add_to_known_names(const char *path,\n \t\t\t       struct commit *commit,\n \t\t\t       int prio)\n {\n-\tint idx;\n-\tint len = strlen(path)+1;\n-\tstruct commit_name *name = xmalloc(sizeof(struct commit_name) + len);\n-\tunsigned char m = commit->object.sha1[0];\n-\n-\tname->commit = commit;\n-\tname->prio = prio;\n-\tmemcpy(name->path, path, len);\n-\tidx = names[m];\n-\tif (idx >= allocs[m]) {\n-\t\tallocs[m] = (idx + 50) * 3 / 2;\n-\t\tname_array[m] = xrealloc(name_array[m],\n-\t\t\tallocs[m] * sizeof(*name_array));\n+\tstruct commit_name *e = commit->util;\n+\tif (!e || e->prio < prio) {\n+\t\tsize_t len = strlen(path)+1;\n+\t\tfree(e);\n+\t\te = xmalloc(sizeof(struct commit_name) + len);\n+\t\te->prio = prio;\n+\t\tmemcpy(e->path, path, len);\n+\t\tcommit->util = e;\n \t}\n-\tname_array[m][idx] = name;\n-\tnames[m] = ++idx;\n }\n \n static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data)\n@@ -104,21 +71,6 @@ static int get_name(const char *path, const unsigned char *sha1, int flag, void\n \treturn 0;\n }\n \n-static int compare_names(const void *_a, const void *_b)\n-{\n-\tstruct commit_name *a = *(struct commit_name **)_a;\n-\tstruct commit_name *b = *(struct commit_name **)_b;\n-\tunsigned long a_date = a->commit->date;\n-\tunsigned long b_date = b->commit->date;\n-\tint cmp = hashcmp(a->commit->object.sha1, b->commit->object.sha1);\n-\n-\tif (cmp)\n-\t\treturn cmp;\n-\tif (a->prio != b->prio)\n-\t\treturn b->prio - a->prio;\n-\treturn (a_date > b_date) ? -1 : (a_date == b_date) ? 0 : 1;\n-}\n-\n struct possible_tag {\n \tstruct commit_name *name;\n \tint depth;\n@@ -158,15 +110,11 @@ static void describe(const char *arg, int last_one)\n \t\tdie(\"%s is not a valid '%s' object\", arg, commit_type);\n \n \tif (!initialized) {\n-\t\tunsigned int m;\n \t\tinitialized = 1;\n \t\tfor_each_ref(get_name, NULL);\n-\t\tfor (m = 0; m < ARRAY_SIZE(name_array); m++)\n-\t\t\tqsort(name_array[m], names[m],\n-\t\t\t\tsizeof(*name_array[m]), compare_names);\n \t}\n \n-\tn = match(cmit);\n+\tn = cmit->util;\n \tif (n) {\n \t\tprintf(\"%s\\n\", n->path);\n \t\treturn;\n@@ -182,7 +130,7 @@ static void describe(const char *arg, int last_one)\n \t\tstruct commit *c = pop_commit(&list);\n \t\tstruct commit_list *parents = c->parents;\n \t\tseen_commits++;\n-\t\tn = match(c);\n+\t\tn = c->util;\n \t\tif (n) {\n \t\t\tif (match_cnt < max_candidates) {\n \t\t\t\tstruct possible_tag *t = &all_matches[match_cnt++];\n-- \n1.5.0.rc1.g4494\n"},{"id":"31694","messageId":"7vvej8ev11.fsf@assigned-by-dhcp.cox.net","threadId":"6379","inReplyTo":"20070115031655.GA11809@spearce.org","subject":"Re: [PATCH] Remove hash in git-describe in favor of util slot.","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2007-01-15T05:08:58Z","receivedAt":"2007-01-15T05:08:58Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Shawn O. Pearce\" <spearce@spearce.org> writes:\n\n> Currently we don't use the util field of struct commit but we want\n> fast access to the highest priority name that references any given\n> commit object during our matching loop.  A really simple approach\n> is to just store the name directly in the util field.\n\nThat's the right thing to do, I would agree.\n"}]}