Skip to content

Commit 024aa46

Browse files
Takuto Ikutagitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d0db9ed commit 024aa46

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

cache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,6 +1773,8 @@ struct object_info {
17731773
#define OBJECT_INFO_SKIP_CACHED 4
17741774
/* Do not retry packed storage after checking packed and loose storage */
17751775
#define OBJECT_INFO_QUICK 8
1776+
/* Do not check loose object */
1777+
#define OBJECT_INFO_IGNORE_LOOSE 16
17761778
extern int sha1_object_info_extended(const unsigned char *, struct object_info *, unsigned flags);
17771779

17781780
/*

fetch-pack.c

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,28 @@ static void mark_alternate_complete(struct object *obj)
711711
mark_complete(&obj->oid);
712712
}
713713

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+
714736
static int everything_local(struct fetch_pack_args *args,
715737
struct ref **refs,
716738
struct ref **sought, int nr_sought)
@@ -719,16 +741,31 @@ static int everything_local(struct fetch_pack_args *args,
719741
int retval;
720742
int old_save_commit_buffer = save_commit_buffer;
721743
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);
722751

723752
save_commit_buffer = 0;
724753

725754
for (ref = *refs; ref; ref = ref->next) {
726755
struct object *o;
756+
unsigned int flags = OBJECT_INFO_QUICK;
727757

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+
}
731766

767+
if (!has_object_file_with_flags(&ref->old_oid, flags))
768+
continue;
732769
o = parse_object(&ref->old_oid);
733770
if (!o)
734771
continue;
@@ -744,6 +781,8 @@ static int everything_local(struct fetch_pack_args *args,
744781
}
745782
}
746783

784+
oidset_clear(&loose_oid_set);
785+
747786
if (!args->no_dependents) {
748787
if (!args->deepen) {
749788
for_each_ref(mark_complete_oid, NULL);

sha1_file.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,6 +1262,9 @@ int sha1_object_info_extended(const unsigned char *sha1, struct object_info *oi,
12621262
if (find_pack_entry(real, &e))
12631263
break;
12641264

1265+
if (flags & OBJECT_INFO_IGNORE_LOOSE)
1266+
return -1;
1267+
12651268
/* Most likely it's a loose object. */
12661269
if (!sha1_loose_object_info(real, oi, flags))
12671270
return 0;

0 commit comments

Comments
 (0)