refs/files-backend: optimize reading of symbolic refs

When reading references via `files_read_raw_ref()` we always consult both the loose reference, and if that wasn't found, we also consult the packed-refs file. While this makes sense to read a generic reference, it is wasteful in the case where we only care about symbolic references: the packed-refs backend does not support them, and thus it cannot ever return one for us. Special-case reading of symbolic references for the files backend such that we always skip asking the packed-refs backend. We use `refs_read_symbolic_ref()` extensively to determine whether we need to skip updating local symbolic references during a fetch, which is why the change results in a significant speedup when doing fetches in repositories with huge numbers of references. The following benchmark executes a mirror-fetch in a repository with about 2 million references via `git fetch --prune --no-write-fetch-head +refs/*:refs/*`: Benchmark 1: HEAD~ Time (mean ± σ): 68.372 s ± 2.344 s [User: 65.629 s, System: 8.786 s] Range (min … max): 65.745 s … 70.246 s 3 runs Benchmark 2: HEAD Time (mean ± σ): 60.259 s ± 0.343 s [User: 61.019 s, System: 7.245 s] Range (min … max): 60.003 s … 60.649 s 3 runs Summary 'HEAD' ran 1.13 ± 0.04 times faster than 'HEAD~' Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Mar 1, 2022 at 10:33 UTC 0a7b38707d5d5c8a7baeb88a85d6259cee4f4e4d
1 file changed +28 -6
refs/files-backend.c
+28 -6
@@ -338,9 +338,9 @@ static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs)
338 return refs->loose;
339 }
340
341 -static int files_read_raw_ref(struct ref_store *ref_store, const char *refname,
342 - struct object_id *oid, struct strbuf *referent,
343 - unsigned int *type, int *failure_errno)
341 +static int read_ref_internal(struct ref_store *ref_store, const char *refname,
342 + struct object_id *oid, struct strbuf *referent,
343 + unsigned int *type, int *failure_errno, int skip_packed_refs)
344 {
345 struct files_ref_store *refs =
346 files_downcast(ref_store, REF_STORE_READ, "read_raw_ref");
@@ -381,7 +381,7 @@ stat_ref:
381 if (lstat(path, &st) < 0) {
382 int ignore_errno;
383 myerr = errno;
384 - if (myerr != ENOENT)
384 + if (myerr != ENOENT || skip_packed_refs)
385 goto out;
386 if (refs_read_raw_ref(refs->packed_ref_store, refname, oid,
387 referent, type, &ignore_errno)) {
@@ -425,7 +425,8 @@ stat_ref:
425 * ref is supposed to be, there could still be a
426 * packed ref:
427 */
428 - if (refs_read_raw_ref(refs->packed_ref_store, refname, oid,
428 + if (skip_packed_refs ||
429 + refs_read_raw_ref(refs->packed_ref_store, refname, oid,
430 referent, type, &ignore_errno)) {
431 myerr = EISDIR;
432 goto out;
@@ -470,6 +471,27 @@ out:
471 return ret;
472 }
473
474 +static int files_read_raw_ref(struct ref_store *ref_store, const char *refname,
475 + struct object_id *oid, struct strbuf *referent,
476 + unsigned int *type, int *failure_errno)
477 +{
478 + return read_ref_internal(ref_store, refname, oid, referent, type, failure_errno, 0);
479 +}
480 +
481 +static int files_read_symbolic_ref(struct ref_store *ref_store, const char *refname,
482 + struct strbuf *referent)
483 +{
484 + struct object_id oid;
485 + int failure_errno, ret;
486 + unsigned int type;
487 +
488 + ret = read_ref_internal(ref_store, refname, &oid, referent, &type, &failure_errno, 1);
489 + if (ret)
490 + return ret;
491 +
492 + return !(type & REF_ISSYMREF);
493 +}
494 +
495 int parse_loose_ref_contents(const char *buf, struct object_id *oid,
496 struct strbuf *referent, unsigned int *type,
497 int *failure_errno)
@@ -3286,7 +3308,7 @@ struct ref_storage_be refs_be_files = {
3308
3309 files_ref_iterator_begin,
3310 files_read_raw_ref,
3289 - NULL,
3311 + files_read_symbolic_ref,
3312
3313 files_reflog_iterator_begin,
3314 files_for_each_reflog_ent,