{"thread":{"id":"35062","subject":"[PATCH] for-each-ref: avoid loading objects to print %(objectname)","startedAt":"2013-10-04T07:56:07Z","lastAt":"2013-10-04T07:56:07Z","messageCount":1,"participants":["Jeff King"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"228552","messageId":"20131004075606.GA26197@sigill.intra.peff.net","threadId":"35062","inReplyTo":null,"subject":"[PATCH] for-each-ref: avoid loading objects to print %(objectname)","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2013-10-04T07:56:07Z","receivedAt":"2013-10-04T07:56:07Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"If you ask for-each-ref to print each ref and its object,\nlike:\n\n  git for-each-ref --format='%(objectname) %(refname)'\n\nthis should involve little more work than looking at the ref\nfiles themselves (along with packed-refs). However,\nfor-each-ref will actually load each object from disk just\nto print its sha1. For most repositories, this isn't a big\ndeal, but it can be noticeable if you have a large number of\nrefs to print. Here are best-of-five timings for the command\nabove on a repo with ~10K refs:\n\n  [before]\n  real    0m0.112s\n  user    0m0.092s\n  sys     0m0.016s\n\n  [after]\n  real    0m0.014s\n  user    0m0.012s\n  sys     0m0.000s\n\nThis patch checks for %(objectname) and %(objectname:short)\nbefore we actually parse the object (and the rest of the\ncode is smart enough to avoid parsing if we have filled all\nof our placeholders).\n\nNote that we can't simply move the objectname parsing code\ninto the early loop. If the \"deref\" form %(*objectname) is\nused, then we do need to parse the object in order to peel\nthe tag. So instead of moving the code, we factor it out\ninto a separate function that can be called for both cases.\n\nWhile we're at it, we add some basic tests for the\ndereferenced placeholders, which were not tested at all\nbefore. This helps ensure we didn't regress that case.\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\n builtin/for-each-ref.c  | 29 ++++++++++++++++++++---------\n t/t6300-for-each-ref.sh |  4 ++++\n 2 files changed, 24 insertions(+), 9 deletions(-)\n\ndiff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c\nindex 1d4083c..d096051 100644\n--- a/builtin/for-each-ref.c\n+++ b/builtin/for-each-ref.c\n@@ -205,6 +205,22 @@ static void *get_obj(const unsigned char *sha1, struct object **obj, unsigned lo\n \treturn buf;\n }\n \n+static int grab_objectname(const char *name, const unsigned char *sha1,\n+\t\t\t    struct atom_value *v)\n+{\n+\tif (!strcmp(name, \"objectname\")) {\n+\t\tchar *s = xmalloc(41);\n+\t\tstrcpy(s, sha1_to_hex(sha1));\n+\t\tv->s = s;\n+\t\treturn 1;\n+\t}\n+\tif (!strcmp(name, \"objectname:short\")) {\n+\t\tv->s = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV));\n+\t\treturn 1;\n+\t}\n+\treturn 0;\n+}\n+\n /* See grab_values */\n static void grab_common_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)\n {\n@@ -225,15 +241,8 @@ static void grab_common_values(struct atom_value *val, int deref, struct object\n \t\t\tv->ul = sz;\n \t\t\tv->s = s;\n \t\t}\n-\t\telse if (!strcmp(name, \"objectname\")) {\n-\t\t\tchar *s = xmalloc(41);\n-\t\t\tstrcpy(s, sha1_to_hex(obj->sha1));\n-\t\t\tv->s = s;\n-\t\t}\n-\t\telse if (!strcmp(name, \"objectname:short\")) {\n-\t\t\tv->s = xstrdup(find_unique_abbrev(obj->sha1,\n-\t\t\t\t\t\t\t  DEFAULT_ABBREV));\n-\t\t}\n+\t\telse if (deref)\n+\t\t\tgrab_objectname(name, obj->sha1, v);\n \t}\n }\n \n@@ -676,6 +685,8 @@ static void populate_value(struct refinfo *ref)\n \t\t\t}\n \t\t\tcontinue;\n \t\t}\n+\t\telse if (!deref && grab_objectname(name, ref->objectname, v))\n+\t\t\tcontinue;\n \t\telse\n \t\t\tcontinue;\n \ndiff --git a/t/t6300-for-each-ref.sh b/t/t6300-for-each-ref.sh\nindex 752f5cb..2b4b9a9 100755\n--- a/t/t6300-for-each-ref.sh\n+++ b/t/t6300-for-each-ref.sh\n@@ -58,6 +58,8 @@ test_atom head type ''\n test_atom head numparent 0\n test_atom head object ''\n test_atom head type ''\n+test_atom head *objectname ''\n+test_atom head *objecttype ''\n test_atom head author 'A U Thor <author@example.com> 1151939924 +0200'\n test_atom head authorname 'A U Thor'\n test_atom head authoremail '<author@example.com>'\n@@ -91,6 +93,8 @@ test_atom tag type 'commit'\n test_atom tag numparent ''\n test_atom tag object '67a36f10722846e891fbada1ba48ed035de75581'\n test_atom tag type 'commit'\n+test_atom tag *objectname '67a36f10722846e891fbada1ba48ed035de75581'\n+test_atom tag *objecttype 'commit'\n test_atom tag author ''\n test_atom tag authorname ''\n test_atom tag authoremail ''\n-- \n1.8.4.1.4.gf327177\n"}]}