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

[PATCH v2 1/1] fetch-pack.c: use oidset to check existence of loose object

From
TITakuto Ikuta <tikuta@chromium.org>
Date
Mar 9, 2018, 13:11 UTC
Message-ID
<20180309131136.219303-2-tikuta@chromium.org>
In-Reply-To
<20180309131136.219303-1-tikuta@chromium.org>

In repository having large number of remote refs, because to check existence of each refs in local repository, 'git fetch' ends up doing a lot of lstat(2) calls to see if it exists in loose form, which makes it slow.

This patch enumerates loose objects in hashmap beforehand and uses it to check existence instead of using lstat(2) to improve performance of fetch-pack for repositories having large number of remote refs compared to the number of loose objects.

With this patch, the number of lstat(2) calls in `git fetch` is reduced from 411412 to 13794 for chromium repository, it has more than 480000 remote refs.

I took time stat of `git fetch` disabling quickfetch, so that fetch-pack
runs, for chromium repository 3 time on linux with SSD.
* with this patch
8.105s
8.309s
7.640s
avg: 8.018s
* master
12.287s
11.175s
12.227s
avg: 11.896s
On my MacBook Air which has slower lstat(2).
* with this patch
14.501s
* master
1m16.027s
`git fetch` on slow disk will be improved largely.
Signed-off-by: Takuto Ikuta <tikuta@chromium.org>
---
 cache.h      |  2 ++
 fetch-pack.c | 26 +++++++++++++++++++++++---
 sha1_file.c  |  3 +++
 3 files changed, 28 insertions(+), 3 deletions(-)
diff --git a/cache.h b/cache.h
index d06932ed0..6a72f54d7 100644
--- a/cache.h
+++ b/cache.h
@@ -1773,6 +1773,8 @@ struct object_info {
 #define OBJECT_INFO_SKIP_CACHED 4
 /* Do not retry packed storage after checking packed and loose storage */
 #define OBJECT_INFO_QUICK 8
+/* Do not check loose object */
+#define OBJECT_INFO_IGNORE_LOOSE 16
 extern int sha1_object_info_extended(const unsigned char *, struct object_info *, unsigned flags);
 
 /*
diff --git a/fetch-pack.c b/fetch-pack.c
index d97461296..ef8b93424 100644
--- a/fetch-pack.c
+++ b/fetch-pack.c
@@ -711,6 +711,14 @@ static void mark_alternate_complete(struct object *obj)
 	mark_complete(&obj->oid);
 }
 
+static int add_loose_objects_to_set(const struct object_id *oid,
+				    const char *path,
+				    void *data)
+{
+	oidset_insert(data, oid);
+	return 0;
+}
+
 static int everything_local(struct fetch_pack_args *args,
 			    struct ref **refs,
 			    struct ref **sought, int nr_sought)
@@ -719,16 +727,26 @@ static int everything_local(struct fetch_pack_args *args,
 	int retval;
 	int old_save_commit_buffer = save_commit_buffer;
 	timestamp_t cutoff = 0;
+	struct oidset loose_oid_set = OIDSET_INIT;
+
+	/* Enumerate all loose objects. */
+	for_each_loose_object(add_loose_objects_to_set, &loose_oid_set, 0);
 
 	save_commit_buffer = 0;
 
 	for (ref = *refs; ref; ref = ref->next) {
 		struct object *o;
+		unsigned int flag = OBJECT_INFO_QUICK;
 
-		if (!has_object_file_with_flags(&ref->old_oid,
-						OBJECT_INFO_QUICK))
-			continue;
+		if (!oidset_contains(&loose_oid_set, &ref->old_oid)) {
+			/* I know this does not exist in the loose form,
+			 * so check if it exists in a non-loose form.
+			 */
+			flag |= OBJECT_INFO_IGNORE_LOOSE;
+		}
 
+		if (!has_object_file_with_flags(&ref->old_oid, flag))
+			continue;
 		o = parse_object(&ref->old_oid);
 		if (!o)
 			continue;
@@ -744,6 +762,8 @@ static int everything_local(struct fetch_pack_args *args,
 		}
 	}
 
+	oidset_clear(&loose_oid_set);
+
 	if (!args->no_dependents) {
 		if (!args->deepen) {
 			for_each_ref(mark_complete_oid, NULL);
diff --git a/sha1_file.c b/sha1_file.c
index 1b94f39c4..c0a197947 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -1262,6 +1262,9 @@ int sha1_object_info_extended(const unsigned char *sha1, struct object_info *oi,
 		if (find_pack_entry(real, &e))
 			break;
 
+		if (flags & OBJECT_INFO_IGNORE_LOOSE)
+			return -1;
+
 		/* Most likely it's a loose object. */
 		if (!sha1_loose_object_info(real, oi, flags))
 			return 0;
-- 
2.16.2
Previous: Takuto IkutaNext: Takuto Ikuta
Message 6 of 20 in “fetch-pack.c: use oidset to check existence of loose object”
  1. fetch-pack.c: use oidset to check existence of loose objectTakuto Ikuta, Mar 8, 2018
  2. René ScharfeMar 8, 2018
  3. Takuto IkutaMar 9, 2018
  4. Junio C HamanoMar 8, 2018
  5. 0/1 fetch-pack.c: use oidset to check existence of loose objectTakuto Ikuta, Mar 9, 2018
  6. 1/1 fetch-pack.c: use oidset to check existence of loose objectTakuto Ikuta, Mar 9, 2018
  7. fetch-pack.c: use oidset to check existence of loose objectTakuto Ikuta, Mar 9, 2018
  8. Junio C HamanoMar 9, 2018
  9. Takuto IkutaMar 10, 2018
  10. Junio C HamanoMar 13, 2018
  11. Takuto IkutaMar 14, 2018
  12. fetch-pack.c: use oidset to check existence of loose objectTakuto Ikuta, Mar 10, 2018
  13. fetch-pack.c: use oidset to check existence of loose objectTakuto Ikuta, Mar 10, 2018
  14. Junio C HamanoMar 13, 2018
  15. fetch-pack.c: use oidset to check existence of loose objectTakuto Ikuta, Mar 14, 2018
  16. fetch-pack.c: use oidset to check existence of loose objectTakuto Ikuta, Mar 14, 2018
  17. Takuto IkutaMar 9, 2018
  18. Junio C HamanoMar 9, 2018
  19. Junio C HamanoMar 9, 2018
  20. sha1_file: restore OBJECT_INFO_QUICK functionalityJonathan Tan, Mar 13, 2018

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.