Skip to contentSkip to conversation

fetch-pack.c: use oidset to check existence of loose object

PATCH Superseded Takuto Ikuta 4 participants 20 messages Mar 8, 2018 – Mar 14, 2018

lore
Share & tools
lore
#0 lore raw

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

In repository having large number of refs, lstat for non-existing loose objects makes git fetch slow.

This patch stores existing loose objects in hashmap beforehand and use it to check existence instead of using lstat.

With this patch, the number of lstat calls in git fetch is reduced from 411412 to 13794 for chromium repository.

I took time stat of git fetch disabling quickfetch 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.
* with this patch
14.501s

* master
1m16.027s

git fetch on slow disk will be improved largely.

---
 cache.h      |  2 ++
 fetch-pack.c | 22 +++++++++++++++++++---
 sha1_file.c  |  3 +++
 3 files changed, 24 insertions(+), 3 deletions(-)
cache.h +2 -0
index d06932ed0..db38db40e 100644
@@ -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_SKIP_LOOSE 16
 extern int sha1_object_info_extended(const unsigned char *, struct object_info *, unsigned flags);
 
 /*
fetch-pack.c +19 -3
index d97461296..1658487f7 100644
@@ -711,6 +711,15 @@ 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)
+{
+	struct oidset* set = (struct oidset*)(data);
+	oidset_insert(set, oid);
+	return 0;
+}
+
 static int everything_local(struct fetch_pack_args *args,
 			    struct ref **refs,
 			    struct ref **sought, int nr_sought)
@@ -719,16 +728,21 @@ 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;
+
+	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))
+			flag |= OBJECT_INFO_SKIP_LOOSE;
 
+		if (!has_object_file_with_flags(&ref->old_oid, flag))
+			continue;
 		o = parse_object(&ref->old_oid);
 		if (!o)
 			continue;
@@ -744,6 +758,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);
sha1_file.c +3 -0
index 1b94f39c4..c903cbcec 100644
@@ -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_SKIP_LOOSE)
+			return -1;
+
 		/* Most likely it's a loose object. */
 		if (!sha1_loose_object_info(real, oi, flags))
 			return 0;
Signature · 1 line
2.16.2
#1 lore raw

Am 08.03.2018 um 13:06 schrieb Takuto Ikuta:

Quoted 23 lines

In repository having large number of refs, lstat for non-existing loose objects makes git fetch slow.

This patch stores existing loose objects in hashmap beforehand and use it to check existence instead of using lstat.

With this patch, the number of lstat calls in git fetch is reduced from 411412 to 13794 for chromium repository.

I took time stat of git fetch disabling quickfetch 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

Quoted 6 lines

On my MacBook Air which has slower lstat.
* with this patch
14.501s

* master
1m16.027s

Nice improvement!

Quoted 28 lines

git fetch on slow disk will be improved largely.

---
  cache.h      |  2 ++
  fetch-pack.c | 22 +++++++++++++++++++---
  sha1_file.c  |  3 +++
  3 files changed, 24 insertions(+), 3 deletions(-)
cache.h +2 -0
index d06932ed0..db38db40e 100644
@@ -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_SKIP_LOOSE 16
  extern int sha1_object_info_extended(const unsigned char *, struct object_info *, unsigned flags);
 
  /*
fetch-pack.c +0 -0
index d97461296..1658487f7 100644

fetch-pack.c static void mark_alternate_complete(struct object *obj) · line 718

Quoted 8 lines
fetch-pack.c +5 -0
@@ -711,6 +711,15 @@ 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)
+{
+	struct oidset* set = (struct oidset*)(data);

This cast is not needed (unlike in C++). And the asterisk should be stuck to the variable, not the type (see Documentation/CodingGuidelines).

fetch-pack.c static void mark_alternate_complete(struct object *obj) · line 719

Quoted 1 line
fetch-pack.c +1 -0
+	oidset_insert(set, oid);

In fact, you could just put "data" in here instead of "set" (without a cast), with no loss in readability or safety.

Quoted 32 lines
fetch-pack.c +11 -3
+	return 0;
+}
+
  static int everything_local(struct fetch_pack_args *args,
  			    struct ref **refs,
  			    struct ref **sought, int nr_sought)
@@ -719,16 +728,21 @@ 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;
+
+	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))
+			flag |= OBJECT_INFO_SKIP_LOOSE;
 
+		if (!has_object_file_with_flags(&ref->old_oid, flag))
+			continue;
  		o = parse_object(&ref->old_oid);
  		if (!o)
  			continue;

fetch-pack.c static int everything_local(struct fetch_pack_args *args, · line 762

Quoted 5 lines
fetch-pack.c +2 -0
@@ -744,6 +758,8 @@ static int everything_local(struct fetch_pack_args *args,
  		}
  	}
 
+	oidset_clear(&loose_oid_set);
+

This part looks fine to me. (Except perhaps call the variable "flags" because you sometimes have two?)

Why not include packed objects as well? Probably because packs have indexes which can queried quickly to determine object existence, and because there are only few loose objects in typical repositories, right?

A similar cache was introduced by cc817ca3ef (sha1_name: cache readdir(3) results in find_short_object_filename()) to speed up finding unambiguous shorts object hashes. I wonder if it could be used here as well, but I don't see an easy way.

Quoted 9 lines
fetch-pack.c +0 -0
  	if (!args->no_dependents) {
  		if (!args->deepen) {
  			for_each_ref(mark_complete_oid, NULL);
sha1_file.c +0 -0
index 1b94f39c4..c903cbcec 100644

sha1_file.c int sha1_object_info_extended(const unsigned char *sha1, struct object_info *oi, · line 1270

Quoted 9 lines
sha1_file.c +3 -0
@@ -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_SKIP_LOOSE)
+			return -1;
+
  		/* Most likely it's a loose object. */
  		if (!sha1_loose_object_info(real, oi, flags))
  			return 0;

This early return doesn't just skip checking loose objects. It also skips reloading packs and fetching missing objects for partial clones. That may not be a problem for fetch-pack, but it means the flag has a misleading name. Do you get the same performance improvement if you make it only skip that sha1_loose_object_info() call?

René

#2 lore raw

2018-03-09 2:19 GMT+09:00 René Scharfe <[email protected]>:

Quoted 10 lines

Am 08.03.2018 um 13:06 schrieb Takuto Ikuta:

fetch-pack.c static void mark_alternate_complete(struct object *obj) · line 718

Quoted 5 lines
fetch-pack.c +5 -0
+static int add_loose_objects_to_set(const struct object_id *oid,
+                                 const char *path,
+                                 void *data)
+{
+     struct oidset* set = (struct oidset*)(data);

This cast is not needed (unlike in C++). And the asterisk should be stuck to the variable, not the type (see Documentation/CodingGuidelines).

Quoted 5 lines
Quoted 1 line

+ oidset_insert(set, oid);

In fact, you could just put "data" in here instead of "set" (without a cast), with no loss in readability or safety.

Thank you for review, changed to use data directly in v2.

Quoted 35 lines
Quoted 35 lines
fetch-pack.c +11 -3
+     return 0;
+}
+
  static int everything_local(struct fetch_pack_args *args,
                          struct ref **refs,
                          struct ref **sought, int nr_sought)
@@ -719,16 +728,21 @@ 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;
+
+     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))
+                     flag |= OBJECT_INFO_SKIP_LOOSE;
 
+             if (!has_object_file_with_flags(&ref->old_oid, flag))
+                     continue;
              o = parse_object(&ref->old_oid);
              if (!o)
                      continue;
@@ -744,6 +758,8 @@ static int everything_local(struct fetch_pack_args *args,
              }
      }
 
Quoted 6 lines

fetch-pack.c static int everything_local(struct fetch_pack_args *args, · line 762

Quoted 2 lines
fetch-pack.c +2 -0
+     oidset_clear(&loose_oid_set);
+

This part looks fine to me. (Except perhaps call the variable "flags" because you sometimes have two?)

Changed flag names.

Quoted 5 lines

Why not include packed objects as well? Probably because packs have indexes which can queried quickly to determine object existence, and because there are only few loose objects in typical repositories, right?

Correct. In my target repository, chromium, fetch-pack's slowness comes from many lstat
to non-existing loose objects for remote refs. I focus on to remove such lstat.

Quoted 16 lines

A similar cache was introduced by cc817ca3ef (sha1_name: cache readdir(3) results in find_short_object_filename()) to speed up finding unambiguous shorts object hashes. I wonder if it could be used here as well, but I don't see an easy way.

Quoted 11 lines
fetch-pack.c +0 -0
      if (!args->no_dependents) {
              if (!args->deepen) {
                      for_each_ref(mark_complete_oid, NULL);
sha1_file.c +0 -0
index 1b94f39c4..c903cbcec 100644
@@ -1262,6 +1262,9 @@ int sha1_object_info_extended(const unsigned char *sha1, struct object_info *oi,
              if (find_pack_entry(real, &e))
                      break;
 
Quoted 15 lines

sha1_file.c int sha1_object_info_extended(const unsigned char *sha1, struct object_info *oi, · line 1270

Quoted 7 lines
sha1_file.c +3 -0
+             if (flags & OBJECT_INFO_SKIP_LOOSE)
+                     return -1;
+
              /* Most likely it's a loose object. */
              if (!sha1_loose_object_info(real, oi, flags))
                      return 0;

This early return doesn't just skip checking loose objects. It also skips reloading packs and fetching missing objects for partial clones. That may not be a problem for fetch-pack, but it means the flag has a misleading name. Do you get the same performance improvement if you make it only skip that sha1_loose_object_info() call?

I changed the flag name following Junio's suggestion. If we skip sha1_loose_object_info call,
reprepare_packed_git(...) runs for every non-existing refs and git fetch becomes slower.

Takuto

#3 lore raw
In reply to Takuto Ikuta
Quoted 2 lines · Takuto Ikuta

In repository having large number of refs, lstat for non-existing loose objects makes git fetch slow.

It is not immediately clear how "large number of refs" and "lstat for loose objects" interact with each other to create a problem. "In repository having large number of refs, because such and such processing needs to do this and that, 'git fetch' ends up doing a lot of lstat(2) calls to see if many objects exist in the loose form, which makes it slow". Please fill in the blanks.

Quoted 8 lines

This patch stores existing loose objects in hashmap beforehand and use it to check existence instead of using lstat.

With this patch, the number of lstat calls in git fetch is reduced from 411412 to 13794 for chromium repository.

I took time stat of git fetch disabling quickfetch for chromium repository 3 time on linux with SSD.

Now you drop a clue that would help to fill in the blanks above, but I am not sure what the significance of your having to disable quickfetch in order to take measurements---it makes it sound as if it is an articificial problem that does not exist in real life (i.e. when quickfetch is not disabled), but I am getting the feeling that it is not what you wanted to say here.

In any case, do_fetch_pack() tries to see if all of the tip commits we are going to fetch exist locally, so when you are trying a fetch that grabs huge number of refs (by the way, it means that the first sentence of the proposed log message is not quite true---it is "When fetching a large number of refs", as it does not matter how many refs _we_ have, no?), everything_local() ends up making repeated calls to has_object_file_with_flags() to all of the refs.

I like the idea---this turns "for each of these many things, check if it exists with lstat(2)" into "enumerate what exists with lstat(2), and then use that for the existence test"; if you need to try N objects for existence, and you only have M objects loose where N is vastly larger than M, it will be a huge win. If you have very many loose objects and checking only a handful of objects for existence check, you would lose big, though, no?

fetch-pack.c static void mark_alternate_complete(struct object *obj) · line 718

Quoted 13 lines
fetch-pack.c +5 -0
index d97461296..1658487f7 100644
@@ -711,6 +711,15 @@ 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)
+{
+	struct oidset* set = (struct oidset*)(data);

Style: in our codebase, asterisk does not stick to the type.

	struct oidset *set = (struct oidset *)(data);

fetch-pack.c static int everything_local(struct fetch_pack_args *args, · line 733

Quoted 7 lines
fetch-pack.c +3 -0
@@ -719,16 +728,21 @@ 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;
+
+	for_each_loose_object(add_loose_objects_to_set, &loose_oid_set, 0);

OK, so this is the "enumerate all loose objects" phase.

fetch-pack.c static int everything_local(struct fetch_pack_args *args, · line 739

Quoted 5 lines
fetch-pack.c +1 -0
 	save_commit_buffer = 0;
 
 	for (ref = *refs; ref; ref = ref->next) {
 		struct object *o;
+		unsigned int flag = OBJECT_INFO_QUICK;

Hmm, OBJECT_INFO_QUICK optimization was added in dfdd4afc ("sha1_file: teach sha1_object_info_extended more flags", 2017-06-21), but since 8b4c0103 ("sha1_file: support lazily fetching missing objects", 2017-12-08) it appears that passing OBJECT_INFO_QUICK down the codepath does not do anything interesting. Jonathan (cc'ed), are all remaining hits from "git grep OBJECT_INFO_QUICK" all dead no-ops these days?

fetch-pack.c static int everything_local(struct fetch_pack_args *args, · line 745

Quoted 9 lines
fetch-pack.c +4 -3
-		if (!has_object_file_with_flags(&ref->old_oid,
-						OBJECT_INFO_QUICK))
-			continue;
+		if (!oidset_contains(&loose_oid_set, &ref->old_oid))
+			flag |= OBJECT_INFO_SKIP_LOOSE;
 
+		if (!has_object_file_with_flags(&ref->old_oid, flag))
+			continue;

Here, you want a way to say "I know this does not exist in the loose form, so check if it exists in a non-loose form", and that is why you invented the new flag.

sha1_file.c int sha1_object_info_extended(const unsigned char *sha1, struct object_info *oi, · line 1267

Quoted 11 lines
sha1_file.c +3 -0
index 1b94f39c4..c903cbcec 100644
@@ -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_SKIP_LOOSE)
+			return -1;
+

I cannot quite convince myself that this is done at the right layer; it smells to be at a bit too low a layer. This change makes sense only to a caller that is interested in the existence test. If the flag is named after what it does, i.e. "ignore loose object", then it does sort-of make sense, though.

Quoted 3 lines
sha1_file.c +0 -0
 		/* Most likely it's a loose object. */
 		if (!sha1_loose_object_info(real, oi, flags))
 			return 0;
#4 lore raw

v2 cover fetch-pack.c: use oidset to check existence of loose object

I removed unnecessary type cast, added comment in everthing_local(...), changed flag name and updated description.

Takuto Ikuta (1):
fetch-pack.c: use oidset to check existence of loose object

 cache.h      |  2 ++
 fetch-pack.c | 26 +++++++++++++++++++++++---
 sha1_file.c  |  3 +++
 3 files changed, 28 insertions(+), 3 deletions(-)

--
2.16.2

#5 lore raw

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

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.

---
 cache.h      |  2 ++
 fetch-pack.c | 26 +++++++++++++++++++++++---
 sha1_file.c  |  3 +++
 3 files changed, 28 insertions(+), 3 deletions(-)
cache.h +2 -0
index d06932ed0..6a72f54d7 100644
@@ -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);
 
 /*
fetch-pack.c +23 -3
index d97461296..ef8b93424 100644
@@ -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);
sha1_file.c +3 -0
index 1b94f39c4..c0a197947 100644
@@ -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;
Signature · 1 line
2.16.2
#6 lore raw

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

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.

---
 cache.h      |  2 ++
 fetch-pack.c | 26 +++++++++++++++++++++++---
 sha1_file.c  |  3 +++
 3 files changed, 28 insertions(+), 3 deletions(-)
cache.h +2 -0
index d06932ed0..6a72f54d7 100644
@@ -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);
 
 /*
fetch-pack.c +23 -3
index d97461296..aed4aa213 100644
@@ -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 flags = 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.
+			 */
+			flags |= OBJECT_INFO_IGNORE_LOOSE;
+		}
 
+		if (!has_object_file_with_flags(&ref->old_oid, flags))
+			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);
sha1_file.c +3 -0
index 1b94f39c4..c0a197947 100644
@@ -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;
Signature · 1 line
2.16.2
#7 lore raw
Quoted 1 line · Takuto Ikuta

In repository having large number of remote refs, because to check

Isn't this "When fetching from a repository with large number of refs,"? The number of refs (whether it is local or remote-tracking) the local side has has nothing to do with the issue you are addressing, no?

Quoted 3 lines

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.

Other than that, the above description reads much better and makes the result easier to understand.

Quoted 4 lines

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.

We'd rather write this paragraph as if giving an order to the codebase "to be like so", e.g.

Instead of making as many lstat(2) calls as the refs the remote side advertised to see if these objects exist in the loose form, first enumerate all the existing loose objects in hashmap beforehand and use it to check existence of them...

Quoted 1 line

I took time stat of git fetch disabling quickfetch, so that fetch-pack

I still do not know if a benchmark with quickfetch disabled gives relevant numbers, for reasons I gave earlier. The relative numbers between Linux and MacBook look quite convincing, as they illustrate differences of lstat(2) performance on these platforms.

fetch-pack.c static int everything_local(struct fetch_pack_args *args, · line 744

Quoted 11 lines
fetch-pack.c +5 -3
 	for (ref = *refs; ref; ref = ref->next) {
 		struct object *o;
+		unsigned int flags = 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.
+			 */

/*
* Our multi-line comment looks like this,
* with opening slash-asterisk and closing
* asterisk-slash on their own lines. */

Thanks.

#8 lore raw

2018-03-10 3:00 GMT+09:00 Junio C Hamano <[email protected]>:

Quoted 17 lines
Quoted 6 lines · Takuto Ikuta

Yes, I just wanted to say 'git fetch' invokes fetch-pack. fetch-pack is skipped when running git fetch repeatedly while remote has no update by quickfetch. So I disabled it to see the performance of fetch-pack. In chromium repository, master branch is updated several times in an hour, so git fetch invokes fetch-pack in such frequency.

I do understand that if you run "git fetch" against the same place in quick succession three times, the first one may cost a lot (not just that it has to do the everything_local() computation that you care about, it also has to actually do the network transfer), while the second and third ones that are done before anything new happens on the other end will not involve everything_local() overhead thanks to quickfetch.

Quoted 8 lines

A "fetch" that is run against a remote that has nothing new, but still triggers everything_local() only because quickfetch is disabled, is an artificial case that has no relevance to the real world, I suspect, because the quickfetch optimization is to solve the "there is nothing to be done, still do_fetch_pack() spends so much cycles only to realize that it does not have anything to do, why?" issue.

I changed not to say "disabling quickfetch" in v5 description. Actually, I'd like to take several stats on linux because the perf difference is small and it can be affected by network and quickfetch.

Quoted 6 lines

Isn't running the "git fetch" command with the "--dry-run" option many times in quick succession a lot closer to what you really want to measure, I wonder? That way, your first fetch won't be touching the state of the local side to affect your second and subsequent fetches.

Looks no? Even when I use --dry-run, second run did not invoke fetch-pack.

Quoted 11 lines
Quoted 10 lines
Quoted 7 lines

In any case, do_fetch_pack() tries to see if all of the tip commits we are going to fetch exist locally, so when you are trying a fetch that grabs huge number of refs (by the way, it means that the first sentence of the proposed log message is not quite true---it is "When fetching a large number of refs", as it does not matter how many refs _we_ have, no?), everything_local() ends up making repeated calls to has_object_file_with_flags() to all of the refs.

I fixed description by changing 'refs' to 'remote refs'. In my understanding, git tries to check existence of remote refs even if we won't fetch such refs.

Quoted 8 lines

During fetch, everything_local() tries to mark common part by walking the refs the other side advertised upon the first contact, so it is correct that the number of checks is not reduced in a fetch that does not fetch many refs, but the number of remote-tracking refs you have has no effect, so I doubt such a rephrasing would make the description more understandable. "When fetching from a repository with large number of refs" is probably what you want to say, no?

For refs existing in local repository, everything_local looks to be able to find corresponding object from packed or loose objects. And if it exists, I think, cost of lstat(2) is relatively smaller than other operations. But for remote refs, everything_local fails to find it from packed object (this check is fast)
and it tries to find loose object by using lstat(2), and this fails and slow. My patch is to skip this lstat(2) to non-existing ref objects for repositories having large number of remote refs.

2018-03-10 4:41 GMT+09:00 Junio C Hamano <[email protected]>:

Quoted 23 lines
Quoted 5 lines · Junio C Hamano
Quoted 2 lines

Yes. But I think the default limit for the number of loose objects, 7000, gives us small overhead when we do enumeration of all objects.

Hmph, I didn't see the code that does the estimation of loose object count before starting to enumerate, though.

Another thing the code could do to avoid negative consequences on projects that look quite different from yours (e.g. the other side does not have insane number of refs, but there are locally quite a few loose objects) is to count how many entries are on *refs list before we decide to enumerate all loose objects. When the refs list is relatively shorter than the estimated number of loose objects (you can actually do the estimation based on sampling, or just rely on your assumed 7k), it may be a win _not_ to trigger the new code you are adding to this codepath with this patch. I would imagine that the simplest implementaion may just count

        for (ref = *refs, count = 0; ref && count++ < LIMIT; ref = ref->next)
                ;
        use_oidset_optim = (LIMIT <= count);
Quoted 7 lines

assuming your "up to 7k loose objects" and then experimenting to determine the LIMIT which is a rough number of refs that makes the oidset optimization worthwhile.

We need a bit better/descriptive name for the LIMIT, if we go that route, though.

Sorry, 7000 was wrong. I wanted to say the default number of gc.auto, 6700. I changed to count refs in for_each_loose_object(), and use it to determine whether
we use oidset or not.

2018-03-10 4:54 GMT+09:00 Junio C Hamano <[email protected]>:

Quoted 9 lines
Quoted 1 line · Takuto Ikuta

In repository having large number of remote refs, because to check

Isn't this "When fetching from a repository with large number of refs,"? The number of refs (whether it is local or remote-tracking) the local side has has nothing to do with the issue you are addressing, no?

Yes it has nothing to do with the issue. At least, my patch won't change performance of git fetch for the repository having large number of local refs.

Quoted 12 lines
Quoted 3 lines

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.

Other than that, the above description reads much better and makes the result easier to understand.

Quoted 4 lines

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.

Quoted 9 lines

We'd rather write this paragraph as if giving an order to the codebase "to be like so", e.g.

Instead of making as many lstat(2) calls as the refs the remote side advertised to see if these objects exist in the loose form, first enumerate all the existing loose objects in hashmap beforehand and use it to check existence of them...

Thanks, I changed description like it.

Quoted 19 lines
Quoted 1 line

I took time stat of git fetch disabling quickfetch, so that fetch-pack

I still do not know if a benchmark with quickfetch disabled gives relevant numbers, for reasons I gave earlier. The relative numbers between Linux and MacBook look quite convincing, as they illustrate differences of lstat(2) performance on these platforms.

fetch-pack.c static int everything_local(struct fetch_pack_args *args, · line 744

Quoted 11 lines
      for (ref = *refs; ref; ref = ref->next) {
              struct object *o;
fetch-pack.c +5 -3
+             unsigned int flags = 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.
+                      */
Quoted 6 lines

/*
* Our multi-line comment looks like this,
* with opening slash-asterisk and closing
* asterisk-slash on their own lines. */

I fixed.

Thanks, Takuto

#9 lore raw
Quoted 8 lines · Takuto Ikuta
Quoted 7 lines

During fetch, everything_local() tries to mark common part by walking the refs the other side advertised upon the first contact, so it is correct that the number of checks is not reduced in a fetch that does not fetch many refs, but the number of remote-tracking refs you have has no effect, so I doubt such a rephrasing would make the description more understandable. "When fetching from a repository with large number of refs" is probably what you want to say, no?

Quoted 8 lines

For refs existing in local repository, everything_local looks to be able to find corresponding object from packed or loose objects. And if it exists, I think, cost of lstat(2) is relatively smaller than other operations. But for remote refs, everything_local fails to find it from packed object (this check is fast)
and it tries to find loose object by using lstat(2), and this fails and slow. My patch is to skip this lstat(2) to non-existing ref objects for repositories having large number of remote refs.

This still does not make sense to me, and I suspect that I am misreading you. In what sense are you using the word "repository" and "remote refs"?

Imagine this situation. I have a local repository A, I fetch from a remote repository B but in my repository A, I do *not* use remote-tracking refs to remember what the last values of refs at repository B. Now when I try to fetch a single ref from B into A, many refs B advertises point at objects A has never heard about, and that triggers many lstat(2) that yields ENOENT that is slow. Your patch is to optimize this so that we learn these objects do not exist locally without running many lstat(2) to objects and helps repositories (like my repository A) when fetching from a repository with large number of refs (like the repository B). It does not matter how many "remote refs" receiving repository (e.g. my A) has, and it does not matter how many "remote refs" sending repository (e.g. my B) has---whether it is refs/remotes/origin/foo (i.e. "remote") or refs/heads/foo (i.e. "local"), a ref at B that points at an object that is missing at A will cause the same lstat(2) at A without your change.

That is why I think "When fetching from a repository with large number of refs" is what you meant, not "fetching into a repository with large number of remote refs" nor "fetching from a repository with large number of remote refs".

Thanks.

#10 lore raw

2018年3月14日(水) 2:53 Junio C Hamano <[email protected]>:

Quoted 11 lines
Quoted 9 lines · Takuto Ikuta
Quoted 7 lines

During fetch, everything_local() tries to mark common part by walking the refs the other side advertised upon the first contact, so it is correct that the number of checks is not reduced in a fetch that does not fetch many refs, but the number of remote-tracking refs you have has no effect, so I doubt such a rephrasing would make the description more understandable. "When fetching from a repository with large number of refs" is probably what you want to say, no?

For refs existing in local repository, everything_local looks to be

able to find

Quoted 5 lines
Quoted 5 lines

corresponding object from packed or loose objects. And if it exists, I think, cost of lstat(2) is relatively smaller than other operations. But for remote refs, everything_local fails to find it from packed object (this check is fast)
and it tries to find loose object by using lstat(2), and this fails and

slow.

Quoted 1 line
Quoted 1 line

My patch is to skip this lstat(2) to non-existing ref objects for

repositories

Quoted 5 lines
Quoted 1 line

having large number of remote refs.

This still does not make sense to me, and I suspect that I am misreading you. In what sense are you using the word "repository" and "remote refs"?

Yes, I think I did not understand the terms correctly.
I meant "repository" for repository in remote servers, and "remote refs" for refs shown by git ls-remote.

Quoted 17 lines

Imagine this situation. I have a local repository A, I fetch from a remote repository B but in my repository A, I do *not* use remote-tracking refs to remember what the last values of refs at repository B. Now when I try to fetch a single ref from B into A, many refs B advertises point at objects A has never heard about, and that triggers many lstat(2) that yields ENOENT that is slow. Your patch is to optimize this so that we learn these objects do not exist locally without running many lstat(2) to objects and helps repositories (like my repository A) when fetching from a repository with large number of refs (like the repository B). It does not matter how many "remote refs" receiving repository (e.g. my A) has, and it does not matter how many "remote refs" sending repository (e.g. my B) has---whether it is refs/remotes/origin/foo (i.e. "remote") or refs/heads/foo (i.e. "local"), a ref at B that points at an object that is missing at A will cause the same lstat(2) at A without your change.

Quoted 4 lines

That is why I think "When fetching from a repository with large number of refs" is what you meant, not "fetching into a repository with large number of remote refs" nor "fetching from a repository with large number of remote refs".

Thank you for explanation.
And yes, that is exactly I want to do in my patch. Fixed description in v6.

Thanks.

#11 lore raw

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

In reply to Takuto Ikuta

In repository having large number of remote refs, because to check existence of each refs in local repository to packed and loose objects, 'git fetch' ends up doing a lot of lstat(2) to non-existing loose form, which makes it slow.

Instead of making as many lstat(2) calls as the refs the remote side advertised to see if these objects exist in the loose form, first enumerate all the existing loose objects in hashmap beforehand and use it to check existence of them if the number of refs is larger than 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.

---
 cache.h      |  2 ++
 fetch-pack.c | 45 ++++++++++++++++++++++++++++++++++++++++++---
 sha1_file.c  |  3 +++
 3 files changed, 47 insertions(+), 3 deletions(-)
cache.h +2 -0
index d06932ed0..6a72f54d7 100644
@@ -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);
 
 /*
fetch-pack.c +42 -3

70 lines not on this page · raw message

sha1_file.c +3 -0
index 1b94f39c4..c0a197947 100644
@@ -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;
Signature · 1 line
2.16.2
#12 lore raw

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

In repository having large number of remote refs, because to check existence of each refs in local repository to packed and loose objects, 'git fetch' ends up doing a lot of lstat(2) to non-existing loose form, which makes it slow.

Instead of making as many lstat(2) calls as the refs the remote side advertised to see if these objects exist in the loose form, first enumerate all the existing loose objects in hashmap beforehand and use it to check existence of them if the number of refs is larger than 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 when fetch-pack happens for chromium repository 3 times 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.

---
 cache.h      |  2 ++
 fetch-pack.c | 45 ++++++++++++++++++++++++++++++++++++++++++---
 sha1_file.c  |  3 +++
 3 files changed, 47 insertions(+), 3 deletions(-)
cache.h +2 -0
index d06932ed0..6a72f54d7 100644
@@ -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);
 
 /*
fetch-pack.c +42 -3

70 lines not on this page · raw message

sha1_file.c +3 -0
index 1b94f39c4..c0a197947 100644
@@ -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;
Signature · 1 line
2.16.2
#13 lore raw
Quoted 11 lines · Takuto Ikuta
fetch-pack.c +11 -0
+struct loose_object_iter {
+	struct oidset *loose_object_set;
+	struct ref *refs;
+};
+
+/*
+ *  If the number of refs is not larger than the number of loose objects,
+ *  this function stops inserting and returns false.
+ */
+static int add_loose_objects_to_set(const struct object_id *oid,
+				    const char *path,

fetch-pack.c static void mark_alternate_complete(struct object *obj) · line 734

Quoted 10 lines
fetch-pack.c +10 -0
+				    void *data)
+{
+	struct loose_object_iter *iter = data;
+	oidset_insert(iter->loose_object_set, oid);
+	if (iter->refs == NULL)
+		return 1;
+
+	iter->refs = iter->refs->next;
+	return 0;
+}

That's clever. By traversing the refs list as you iterate over the loose objects, you do not do any unnecessary work ;-)

Thanks for working on this.

Quoted 35 lines
fetch-pack.c +18 -3
@@ -719,16 +741,31 @@ 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;
+	int use_oidset = 0;
+	struct loose_object_iter iter = {&loose_oid_set, *refs};
+
+	/* Enumerate all loose objects or know refs are not so many. */
+	use_oidset = !for_each_loose_object(add_loose_objects_to_set,
+					    &iter, 0);
 
 	save_commit_buffer = 0;
 
 	for (ref = *refs; ref; ref = ref->next) {
 		struct object *o;
+		unsigned int flags = OBJECT_INFO_QUICK;
 
-		if (!has_object_file_with_flags(&ref->old_oid,
-						OBJECT_INFO_QUICK))
-			continue;
+		if (use_oidset &&
+		    !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.
+			 */
+			flags |= OBJECT_INFO_IGNORE_LOOSE;
+		}
 
+		if (!has_object_file_with_flags(&ref->old_oid, flags))
+			continue;
 		o = parse_object(&ref->old_oid);
 		if (!o)
 			continue;
#14 lore raw

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

In reply to Takuto Ikuta

When fetching from a repository with large number of refs, because to check existence of each refs in local repository to packed and loose objects, 'git fetch' ends up doing a lot of lstat(2) to non-existing loose form, which makes it slow.

Instead of making as many lstat(2) calls as the refs the remote side advertised to see if these objects exist in the loose form, first enumerate all the existing loose objects in hashmap beforehand and use it to check existence of them if the number of refs is larger than 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 when fetch-pack happens for chromium repository 3 times 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.

---
 cache.h      |  2 ++
 fetch-pack.c | 45 ++++++++++++++++++++++++++++++++++++++++++---
 sha1_file.c  |  3 +++
 3 files changed, 47 insertions(+), 3 deletions(-)
cache.h +2 -0
index d06932ed0..6a72f54d7 100644
@@ -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);
 
 /*
fetch-pack.c +42 -3

70 lines not on this page · raw message

sha1_file.c +3 -0
index 1b94f39c4..c0a197947 100644
@@ -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;
Signature · 1 line
2.16.2.804.g6dcf76e118-goog
#15 lore raw

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

When fetching from a repository with large number of refs, because to check existence of each refs in local repository to packed and loose objects, 'git fetch' ends up doing a lot of lstat(2) to non-existing loose form, which makes it slow.

Instead of making as many lstat(2) calls as the refs the remote side advertised to see if these objects exist in the loose form, first enumerate all the existing loose objects in hashmap beforehand and use it to check existence of them if the number of refs is larger than 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 when fetch-pack happens for chromium repository 3 times 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.

---
 cache.h      |  2 ++
 fetch-pack.c | 45 ++++++++++++++++++++++++++++++++++++++++++---
 sha1_file.c  |  3 +++
 3 files changed, 47 insertions(+), 3 deletions(-)
cache.h +2 -0
index d06932ed0..6a72f54d7 100644
@@ -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);
 
 /*
fetch-pack.c +42 -3

70 lines not on this page · raw message

sha1_file.c +3 -0
index 1b94f39c4..c0a197947 100644
@@ -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;
Signature · 1 line
2.16.2
#16 lore raw
In reply to Junio C Hamano

2018-03-09 3:42 GMT+09:00 Junio C Hamano <[email protected]>:

Quoted 10 lines
Quoted 8 lines · Takuto Ikuta

This patch stores existing loose objects in hashmap beforehand and use it to check existence instead of using lstat.

With this patch, the number of lstat calls in git fetch is reduced from 411412 to 13794 for chromium repository.

I took time stat of git fetch disabling quickfetch for chromium repository 3 time on linux with SSD.

Quoted 7 lines

Now you drop a clue that would help to fill in the blanks above, but I am not sure what the significance of your having to disable quickfetch in order to take measurements---it makes it sound as if it is an articificial problem that does not exist in real life (i.e. when quickfetch is not disabled), but I am getting the feeling that it is not what you wanted to say here.

Yes, I just wanted to say 'git fetch' invokes fetch-pack. fetch-pack is skipped when running git fetch repeatedly while remote has no update by quickfetch. So I disabled it to see the performance of fetch-pack. In chromium repository, master branch is updated several times in an hour, so git fetch invokes fetch-pack in such frequency.

Quoted 8 lines

In any case, do_fetch_pack() tries to see if all of the tip commits we are going to fetch exist locally, so when you are trying a fetch that grabs huge number of refs (by the way, it means that the first sentence of the proposed log message is not quite true---it is "When fetching a large number of refs", as it does not matter how many refs _we_ have, no?), everything_local() ends up making repeated calls to has_object_file_with_flags() to all of the refs.

I fixed description by changing 'refs' to 'remote refs'. In my understanding, git tries to check existence of remote refs even if we won't fetch such refs.

Quoted 8 lines

I like the idea---this turns "for each of these many things, check if it exists with lstat(2)" into "enumerate what exists with lstat(2), and then use that for the existence test"; if you need to try N objects for existence, and you only have M objects loose where N is vastly larger than M, it will be a huge win. If you have very many loose objects and checking only a handful of objects for existence check, you would lose big, though, no?

Yes. But I think the default limit for the number of loose objects, 7000, gives us small overhead when we do enumeration of all objects.

Quoted 34 lines

fetch-pack.c static void mark_alternate_complete(struct object *obj) · line 718

Quoted 13 lines
fetch-pack.c +5 -0
index d97461296..1658487f7 100644
@@ -711,6 +711,15 @@ 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)
+{
+     struct oidset* set = (struct oidset*)(data);

Style: in our codebase, asterisk does not stick to the type.

        struct oidset *set = (struct oidset *)(data);

fetch-pack.c static int everything_local(struct fetch_pack_args *args, · line 733

Quoted 7 lines
fetch-pack.c +3 -0
@@ -719,16 +728,21 @@ 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;
+
+     for_each_loose_object(add_loose_objects_to_set, &loose_oid_set, 0);

OK, so this is the "enumerate all loose objects" phase.

fetch-pack.c static int everything_local(struct fetch_pack_args *args, · line 739

Quoted 5 lines
fetch-pack.c +1 -0
      save_commit_buffer = 0;
 
      for (ref = *refs; ref; ref = ref->next) {
              struct object *o;
+             unsigned int flag = OBJECT_INFO_QUICK;
Quoted 8 lines

Hmm, OBJECT_INFO_QUICK optimization was added in dfdd4afc ("sha1_file: teach sha1_object_info_extended more flags", 2017-06-21), but since 8b4c0103 ("sha1_file: support lazily fetching missing objects", 2017-12-08) it appears that passing OBJECT_INFO_QUICK down the codepath does not do anything interesting. Jonathan (cc'ed), are all remaining hits from "git grep OBJECT_INFO_QUICK" all dead no-ops these days?

Yes the flag is no-op now, but let me untouched the flag in this patch.

Quoted 8 lines
Quoted 8 lines
sha1_file.c +0 -0
index 1b94f39c4..c903cbcec 100644
@@ -1262,6 +1262,9 @@ int sha1_object_info_extended(const unsigned char *sha1, struct object_info *oi,
              if (find_pack_entry(real, &e))
                      break;
 
Quoted 10 lines

sha1_file.c int sha1_object_info_extended(const unsigned char *sha1, struct object_info *oi, · line 1267

Quoted 3 lines
sha1_file.c +3 -0
+             if (flags & OBJECT_INFO_SKIP_LOOSE)
+                     return -1;
+

I cannot quite convince myself that this is done at the right layer; it smells to be at a bit too low a layer. This change makes sense only to a caller that is interested in the existence test. If the flag is named after what it does, i.e. "ignore loose object", then it does sort-of make sense, though.

Couldn't come up with good alternative for this, I followed your flag name suggestion in patch v3.

Takuto

#17 lore raw
Quoted 6 lines · Takuto Ikuta

Yes, I just wanted to say 'git fetch' invokes fetch-pack. fetch-pack is skipped when running git fetch repeatedly while remote has no update by quickfetch. So I disabled it to see the performance of fetch-pack. In chromium repository, master branch is updated several times in an hour, so git fetch invokes fetch-pack in such frequency.

I do understand that if you run "git fetch" against the same place in quick succession three times, the first one may cost a lot (not just that it has to do the everything_local() computation that you care about, it also has to actually do the network transfer), while the second and third ones that are done before anything new happens on the other end will not involve everything_local() overhead thanks to quickfetch.

A "fetch" that is run against a remote that has nothing new, but still triggers everything_local() only because quickfetch is disabled, is an artificial case that has no relevance to the real world, I suspect, because the quickfetch optimization is to solve the "there is nothing to be done, still do_fetch_pack() spends so much cycles only to realize that it does not have anything to do, why?" issue.

Isn't running the "git fetch" command with the "--dry-run" option many times in quick succession a lot closer to what you really want to measure, I wonder? That way, your first fetch won't be touching the state of the local side to affect your second and subsequent fetches.

Quoted 10 lines
Quoted 7 lines

In any case, do_fetch_pack() tries to see if all of the tip commits we are going to fetch exist locally, so when you are trying a fetch that grabs huge number of refs (by the way, it means that the first sentence of the proposed log message is not quite true---it is "When fetching a large number of refs", as it does not matter how many refs _we_ have, no?), everything_local() ends up making repeated calls to has_object_file_with_flags() to all of the refs.

I fixed description by changing 'refs' to 'remote refs'. In my understanding, git tries to check existence of remote refs even if we won't fetch such refs.

During fetch, everything_local() tries to mark common part by walking the refs the other side advertised upon the first contact, so it is correct that the number of checks is not reduced in a fetch that does not fetch many refs, but the number of remote-tracking refs you have has no effect, so I doubt such a rephrasing would make the description more understandable. "When fetching from a repository with large number of refs" is probably what you want to say, no?

Quoted 2 lines

Yes. But I think the default limit for the number of loose objects, 7000, gives us small overhead when we do enumeration of all objects.

Hmph, I didn't see the code that does the estimation of loose object count before starting to enumerate, though.

Quoted 9 lines
Quoted 7 lines

Hmm, OBJECT_INFO_QUICK optimization was added in dfdd4afc ("sha1_file: teach sha1_object_info_extended more flags", 2017-06-21), but since 8b4c0103 ("sha1_file: support lazily fetching missing objects", 2017-12-08) it appears that passing OBJECT_INFO_QUICK down the codepath does not do anything interesting. Jonathan (cc'ed), are all remaining hits from "git grep OBJECT_INFO_QUICK" all dead no-ops these days?

Yes the flag is no-op now, but let me untouched the flag in this patch.

Yeah, I do not want you to be touching that in this change. It is an independent/orthogonal clean-up.

Thanks.

#18 lore raw
Quoted 5 lines · Junio C Hamano
Quoted 2 lines

Yes. But I think the default limit for the number of loose objects, 7000, gives us small overhead when we do enumeration of all objects.

Hmph, I didn't see the code that does the estimation of loose object count before starting to enumerate, though.

Another thing the code could do to avoid negative consequences on projects that look quite different from yours (e.g. the other side does not have insane number of refs, but there are locally quite a few loose objects) is to count how many entries are on *refs list before we decide to enumerate all loose objects. When the refs list is relatively shorter than the estimated number of loose objects (you can actually do the estimation based on sampling, or just rely on your assumed 7k), it may be a win _not_ to trigger the new code you are adding to this codepath with this patch. I would imagine that the simplest implementaion may just count

	for (ref = *refs, count = 0; ref && count++ < LIMIT; ref = ref->next)
		;
	use_oidset_optim = (LIMIT <= count);

assuming your "up to 7k loose objects" and then experimenting to determine the LIMIT which is a rough number of refs that makes the oidset optimization worthwhile.

We need a bit better/descriptive name for the LIMIT, if we go that route, though.

Thanks.

#19 lore raw

PATCH 1/1 sha1_file: restore OBJECT_INFO_QUICK functionality

In reply to Junio C Hamano

Support for the OBJECT_INFO_QUICK flag in sha1_object_info_extended() was added in commit dfdd4afcf9 ("sha1_file: teach sha1_object_info_extended more flags", 2017-06-26) in order to support commit e83e71c5e1 ("sha1_file: refactor has_sha1_file_with_flags", 2017-06-26), but it was inadvertently removed in commit 8b4c0103a9 ("sha1_file: support lazily fetching missing objects", 2017-12-08).

Restore this functionality.

---

Quoted 7 lines

Hmm, OBJECT_INFO_QUICK optimization was added in dfdd4afc ("sha1_file: teach sha1_object_info_extended more flags", 2017-06-21), but since 8b4c0103 ("sha1_file: support lazily fetching missing objects", 2017-12-08) it appears that passing OBJECT_INFO_QUICK down the codepath does not do anything interesting. Jonathan (cc'ed), are all remaining hits from "git grep OBJECT_INFO_QUICK" all dead no-ops these days?

They are, but they are not supposed to be. Here is a patch correcting that.

---
 sha1_file.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
sha1_file.c +5 -3
index 1b94f39c4..cc0f43ea8 100644
@@ -1267,9 +1267,11 @@ int sha1_object_info_extended(const unsigned char *sha1, struct object_info *oi,
 			return 0;
 
 		/* Not a loose object; someone else may have just packed it. */
-		reprepare_packed_git();
-		if (find_pack_entry(real, &e))
-			break;
+		if (!(flags & OBJECT_INFO_QUICK)) {
+			reprepare_packed_git();
+			if (find_pack_entry(real, &e))
+				break;
+		}
 
 		/* Check if it is a missing object */
 		if (fetch_if_missing && repository_format_partial_clone &&
Signature · 1 line
2.16.2.660.g709887971b-goog