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. Signed-off-by: Takuto Ikuta <tikuta@chromium.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Takuto Ikuta committed Mar 14, 2018 at 15:32 UTC 024aa4696c788eb1b07be53331f770605696ffba
3 files changed +47 -3
cache.h
+2
@@ -1773,6 +1773,8 @@ struct object_info {
1773 #define OBJECT_INFO_SKIP_CACHED 4
1774 /* Do not retry packed storage after checking packed and loose storage */
1775 #define OBJECT_INFO_QUICK 8
1776 +/* Do not check loose object */
1777 +#define OBJECT_INFO_IGNORE_LOOSE 16
1778 extern int sha1_object_info_extended(const unsigned char *, struct object_info *, unsigned flags);
1779
1780 /*
fetch-pack.c
+42 -3
@@ -711,6 +711,28 @@ static void mark_alternate_complete(struct object *obj)
711 mark_complete(&obj->oid);
712 }
713
714 +struct loose_object_iter {
715 + struct oidset *loose_object_set;
716 + struct ref *refs;
717 +};
718 +
719 +/*
720 + * If the number of refs is not larger than the number of loose objects,
721 + * this function stops inserting.
722 + */
723 +static int add_loose_objects_to_set(const struct object_id *oid,
724 + const char *path,
725 + void *data)
726 +{
727 + struct loose_object_iter *iter = data;
728 + oidset_insert(iter->loose_object_set, oid);
729 + if (iter->refs == NULL)
730 + return 1;
731 +
732 + iter->refs = iter->refs->next;
733 + return 0;
734 +}
735 +
736 static int everything_local(struct fetch_pack_args *args,
737 struct ref **refs,
738 struct ref **sought, int nr_sought)
@@ -719,16 +741,31 @@ static int everything_local(struct fetch_pack_args *args,
741 int retval;
742 int old_save_commit_buffer = save_commit_buffer;
743 timestamp_t cutoff = 0;
744 + struct oidset loose_oid_set = OIDSET_INIT;
745 + int use_oidset = 0;
746 + struct loose_object_iter iter = {&loose_oid_set, *refs};
747 +
748 + /* Enumerate all loose objects or know refs are not so many. */
749 + use_oidset = !for_each_loose_object(add_loose_objects_to_set,
750 + &iter, 0);
751
752 save_commit_buffer = 0;
753
754 for (ref = *refs; ref; ref = ref->next) {
755 struct object *o;
756 + unsigned int flags = OBJECT_INFO_QUICK;
757
728 - if (!has_object_file_with_flags(&ref->old_oid,
729 - OBJECT_INFO_QUICK))
730 - continue;
758 + if (use_oidset &&
759 + !oidset_contains(&loose_oid_set, &ref->old_oid)) {
760 + /*
761 + * I know this does not exist in the loose form,
762 + * so check if it exists in a non-loose form.
763 + */
764 + flags |= OBJECT_INFO_IGNORE_LOOSE;
765 + }
766
767 + if (!has_object_file_with_flags(&ref->old_oid, flags))
768 + continue;
769 o = parse_object(&ref->old_oid);
770 if (!o)
771 continue;
@@ -744,6 +781,8 @@ static int everything_local(struct fetch_pack_args *args,
781 }
782 }
783
784 + oidset_clear(&loose_oid_set);
785 +
786 if (!args->no_dependents) {
787 if (!args->deepen) {
788 for_each_ref(mark_complete_oid, NULL);
sha1_file.c
+3
@@ -1262,6 +1262,9 @@ int sha1_object_info_extended(const unsigned char *sha1, struct object_info *oi,
1262 if (find_pack_entry(real, &e))
1263 break;
1264
1265 + if (flags & OBJECT_INFO_IGNORE_LOOSE)
1266 + return -1;
1267 +
1268 /* Most likely it's a loose object. */
1269 if (!sha1_loose_object_info(real, oi, flags))
1270 return 0;