git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/2] for-each-ref: Do not lookup objects when they will not be used

From
Anders Kaseorg <andersk@mit.edu>
Date
May 27, 2009, 19:23 UTC
Message-ID
<alpine.DEB.2.00.0905271518540.24169@vinegar-pot.mit.edu>

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>
---
 builtin-for-each-ref.c |   26 +++++++++++++++++---------
 1 files changed, 17 insertions(+), 9 deletions(-)
diff --git a/builtin-for-each-ref.c b/builtin-for-each-ref.c
index d091e04..1911cda 100644
--- a/builtin-for-each-ref.c
+++ b/builtin-for-each-ref.c
@@ -561,14 +561,6 @@ static void populate_value(struct refinfo *ref)
 
 	ref->value = xcalloc(sizeof(struct atom_value), used_atom_cnt);
 
-	buf = get_obj(ref->objectname, &obj, &size, &eaten);
-	if (!buf)
-		die("missing object %s for %s",
-		    sha1_to_hex(ref->objectname), ref->refname);
-	if (!obj)
-		die("parse_object_buffer failed on %s for %s",
-		    sha1_to_hex(ref->objectname), ref->refname);
-
 	/* Fill in specials first */
 	for (i = 0; i < used_atom_cnt; i++) {
 		const char *name = used_atom[i];
@@ -621,6 +613,22 @@ static void populate_value(struct refinfo *ref)
 		}
 	}
 
+	for (i = 0; i < used_atom_cnt; i++) {
+		struct atom_value *v = &ref->value[i];
+		if (v->s == NULL)
+			goto need_obj;
+	}
+	return;
+
+ need_obj:
+	buf = get_obj(ref->objectname, &obj, &size, &eaten);
+	if (!buf)
+		die("missing object %s for %s",
+		    sha1_to_hex(ref->objectname), ref->refname);
+	if (!obj)
+		die("parse_object_buffer failed on %s for %s",
+		    sha1_to_hex(ref->objectname), ref->refname);
+
 	grab_values(ref->value, 0, obj, buf, size);
 	if (!eaten)
 		free(buf);
@@ -926,7 +934,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
 
 	memset(&cbdata, 0, sizeof(cbdata));
 	cbdata.grab_pattern = argv;
-	for_each_ref(grab_single_ref, &cbdata);
+	for_each_rawref(grab_single_ref, &cbdata);
 	refs = cbdata.grab_array;
 	num_refs = cbdata.grab_cnt;
 
-- 
1.6.3.1
Next: Anders Kaseorg
Message 1 of 2 in “for-each-ref: Do not lookup objects when they will not be used”
  1. 1/2 for-each-ref: Do not lookup objects when they will not be usedAnders Kaseorg, May 27, 2009
  2. 2/2 branch: Do not lookup commits when they will not be usedAnders Kaseorg, May 27, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.