From: Patrick Steinhardt Date: Mon, 08 Jun 2026 06:59:44 GMT Subject: Re: [PATCH] describe: limit default ref iteration to tags Message-ID: In-Reply-To: <20260607-describe-tag-ref-scope-v1-1-653d232b86b5@gmail.com> On Sun, Jun 07, 2026 at 04:51:53PM -0400, Tamir Duberstein wrote: > Unless --all is given, get_name() rejects every ref outside refs/tags/. > The rejection happens only after the ref backend has enumerated the ref, > so repositories with many other refs spend most of a simple describe > invocation visiting refs which cannot affect its result. Right. The relevant block is this one: if (skip_prefix(ref->name, "refs/tags/", &path_to_match)) { is_tag = 1; } else if (all) { if ((exclude_patterns.nr || patterns.nr) && !skip_prefix(ref->name, "refs/heads/", &path_to_match) && !skip_prefix(ref->name, "refs/remotes/", &path_to_match)) { /* Only accept reference of known type if there are match/exclude patterns */ return 0; } } else { /* Reject anything outside refs/tags/ unless --all */ return 0; } So we really only use tags unless "--all" is given. > Commit 8a5a1884e9 (Avoid accessing non-tag refs in git-describe unless > --all is requested, 2008-02-24) moved this rejection before object > lookup, but left iteration unscoped. Pass the existing refs/tags/ > restriction to the iterator unless --all is given so the backend can > avoid unrelated refs. > > On a checkout with 124,357 refs, of which 330 were tags, I ran the > following command with the parent and patched binaries: > > hyperfine --warmup 3 --runs 15 \ > 'git describe --always --long --abbrev=40 HEAD' > > The results were: > > parent this commit > elapsed 196.2 ms 63.3 ms > user 69.5 ms 48.0 ms > system 123.0 ms 12.0 ms It's a bit curious that you don't post the hyperfine(1) results as-is here. > The wall-time standard deviations were 13.2 ms and 2.6 ms, respectively, > for a 3.10x speedup. Makes sense that this would result in a sizeable speedup, depending of course on the shape of the existing refs in the repository. > diff --git a/builtin/describe.c b/builtin/describe.c > index 1c47d7c0b7..3532c8ff22 100644 > --- a/builtin/describe.c > +++ b/builtin/describe.c > @@ -740,6 +740,9 @@ int cmd_describe(int argc, > return ret; > } > > + if (!all) > + for_each_ref_opts.prefix = "refs/tags/"; > + > hashmap_init(&names, commit_name_neq, NULL, 0); > refs_for_each_ref_ext(get_main_ref_store(the_repository), > get_name, NULL, &for_each_ref_opts); Another performance optimization that we could do here is to wire up the exclude patterns via `for_each_ref_opts.exclude_patterns`. But that's outside the scope of this patch series, and also much less likely to help many use cases out there. > diff --git a/t/perf/p6100-describe.sh b/t/perf/p6100-describe.sh > index 069f91ce49..dfcaf59e90 100755 > --- a/t/perf/p6100-describe.sh > +++ b/t/perf/p6100-describe.sh > @@ -5,6 +5,12 @@ test_description='performance of git-describe' > > test_perf_default_repo > > +test_lazy_prereq PERF_REFFILES ' > + test "$(git rev-parse --show-ref-format)" = files > +' > + > +ref_count=10000 Let's not declare this variable outside of tests. > @@ -27,4 +33,18 @@ test_perf 'describe HEAD with one tag' ' > git describe --match=new HEAD > ' > > +test_expect_success PERF_REFFILES 'set up many unrelated refs' ' > + git tag -m tip tip HEAD && > + for i in $(test_seq $ref_count) > + do > + printf "create refs/heads/describe-perf/%05d HEAD\n" $i || > + return 1 > + done >instructions && > + git update-ref --stdin +' Why is this limited to the "files" backend, only? The logic should work for both backends as-is. Thanks! Patrick