for-each-ref: Do not lookup objects when they will not be used
This makes commands such as `git for-each-ref --format='%(refname)'`, which are used heavily by the bash_completion code, run about 6 times faster on an uncached repository (3 s intead of 18 s on my linux-2.6 repository with several remotes). Signed-off-by: Anders Kaseorg <andersk@mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Anders Kaseorg committed
May 27, 2009 at 15:23 UTC
b7dd2d20fad1036ce7da0b635a8bb14cd4230f1a
1 file changed
+17
-9
builtin-for-each-ref.c
+17
-9
@@ -561,14 +561,6 @@ static void populate_value(struct refinfo *ref)
561
562
ref->value = xcalloc(sizeof(struct atom_value), used_atom_cnt);
563
564
- buf = get_obj(ref->objectname, &obj, &size, &eaten);
565
- if (!buf)
566
- die("missing object %s for %s",
567
- sha1_to_hex(ref->objectname), ref->refname);
568
- if (!obj)
569
- die("parse_object_buffer failed on %s for %s",
570
- sha1_to_hex(ref->objectname), ref->refname);
571
-
564
/* Fill in specials first */
565
for (i = 0; i < used_atom_cnt; i++) {
566
const char *name = used_atom[i];
@@ -621,6 +613,22 @@ static void populate_value(struct refinfo *ref)
613
}
614
}
615
616
+ for (i = 0; i < used_atom_cnt; i++) {
617
+ struct atom_value *v = &ref->value[i];
618
+ if (v->s == NULL)
619
+ goto need_obj;
620
+ }
621
+ return;
622
+
623
+ need_obj:
624
+ buf = get_obj(ref->objectname, &obj, &size, &eaten);
625
+ if (!buf)
626
+ die("missing object %s for %s",
627
+ sha1_to_hex(ref->objectname), ref->refname);
628
+ if (!obj)
629
+ die("parse_object_buffer failed on %s for %s",
630
+ sha1_to_hex(ref->objectname), ref->refname);
631
+
632
grab_values(ref->value, 0, obj, buf, size);
633
if (!eaten)
634
free(buf);
@@ -926,7 +934,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
934
935
memset(&cbdata, 0, sizeof(cbdata));
936
cbdata.grab_pattern = argv;
929
- for_each_ref(grab_single_ref, &cbdata);
937
+ for_each_rawref(grab_single_ref, &cbdata);
938
refs = cbdata.grab_array;
939
num_refs = cbdata.grab_cnt;
940