Subject: Re: [PATCH] builtin/show-ref.c: avoid over-iterating with --heads, --tags

From: Derrick Stolee <[email-masked]>
Date: Fri, 3 Jun 2022 22:33:47 -0400

On 6/3/2022 5:55 PM, Taylor Blau wrote:
> When `show-ref` is combined with the `--heads` or `--tags` options, it
> can avoid iterating parts of a repository's references that it doesn't
> care about.
> 
> But it doesn't take advantage of this potential optimization. When this
> command was introduced back in 358ddb62cf (Add "git show-ref" builtin
> command, 2006-09-15), `for_each_ref_in()` did exist. But since most
> repositories don't have many (any?) references that aren't branches or
> tags already, this makes little difference in practice.
...
> Nonetheless, in our synthetic example above, this provides a significant
> speed-up ("git" is roughly v2.36, "git.compile" is this patch):
> 
>     $ hyperfine -N 'git show-ref --heads' 'git.compile show-ref --heads'
>     Benchmark 1: git show-ref --heads
>       Time (mean ± σ):      49.9 ms ±   6.2 ms    [User: 45.6 ms, System: 4.1 ms]
>       Range (min … max):    46.1 ms …  73.6 ms    43 runs
> 
>     Benchmark 2: git.compile show-ref --heads
>       Time (mean ± σ):       2.8 ms ±   0.4 ms    [User: 1.4 ms, System: 1.2 ms]
>       Range (min … max):     1.3 ms …   5.6 ms    957 runs
> 
>     Summary
>       'git.compile show-ref --heads' ran
>        18.03 ± 3.38 times faster than 'git show-ref --heads'

Excellent speedup.

> -	for_each_ref(show_ref, NULL);
> +	if (heads_only || tags_only) {
> +		if (heads_only)
> +			for_each_fullref_in("refs/heads/", show_ref, NULL);
> +		if (tags_only)
> +			for_each_fullref_in("refs/tags/", show_ref, NULL);

This looks a little silly if these were truly "only" (they
could not both be true), but indeed they could both be true
and the names are just slightly misleading.

Indeed, t1403-show-ref.sh tests all combinations of these
options.

> +	} else {
> +		for_each_ref(show_ref, NULL);
> +	}

Thanks for finding this!
-Stolee