Skip to content

Commit 47c6100

Browse files
pks-tgitster
authored andcommitted
fetch: avoid unpacking headers in object existence check
When updating local refs after the fetch has transferred all objects, we do an object existence test as a safety guard to avoid updating a ref to an object which we don't have. We do so via `oid_object_info()`: if it returns an error, then we know the object does not exist. One side effect of `oid_object_info()` is that it parses the object's type, and to do so it must unpack the object header. This is completely pointless: we don't care for the type, but only want to assert that the object exists. Refactor the code to use `repo_has_object_file()`, which both makes the code's intent clearer and is also faster because it does not unpack object headers. In a real-world repo with 2.3M refs, this results in a small speedup when doing a mirror-fetch: Benchmark #1: HEAD~: git-fetch Time (mean ± σ): 33.686 s ± 0.176 s [User: 30.119 s, System: 5.262 s] Range (min … max): 33.512 s … 33.944 s 5 runs Benchmark #2: HEAD: git-fetch Time (mean ± σ): 31.247 s ± 0.195 s [User: 28.135 s, System: 5.066 s] Range (min … max): 30.948 s … 31.472 s 5 runs Summary 'HEAD: git-fetch' ran 1.08 ± 0.01 times faster than 'HEAD~: git-fetch' Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fe7df03 commit 47c6100

File tree

1 file changed

+1
-3
lines changed

1 file changed

+1
-3
lines changed

builtin/fetch.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -846,13 +846,11 @@ static int update_local_ref(struct ref *ref,
846846
int summary_width)
847847
{
848848
struct commit *current = NULL, *updated;
849-
enum object_type type;
850849
struct branch *current_branch = branch_get(NULL);
851850
const char *pretty_ref = prettify_refname(ref->name);
852851
int fast_forward = 0;
853852

854-
type = oid_object_info(the_repository, &ref->new_oid, NULL);
855-
if (type < 0)
853+
if (!repo_has_object_file(the_repository, &ref->new_oid))
856854
die(_("object %s not found"), oid_to_hex(&ref->new_oid));
857855

858856
if (oideq(&ref->old_oid, &ref->new_oid)) {

0 commit comments

Comments
 (0)