git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Derrick Stolee <derrickstolee@github.com>
Date
Jun 4, 2022, 02:33 UTC
Message-ID
<af32aefc-8184-94ff-fd80-76221dc313b4@github.com>
In-Reply-To
<3fa6932641f18d78156bbf60b1571383f2cb5046.1654293264.git.me@ttaylorr.com>
On 6/3/2022 5:55 PM, Taylor Blau wrote:
Show 9 quoted lines
> 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.
...
Show 15 quoted lines
> 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.
Show 6 quoted lines
> -	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

Previous: Taylor BlauNext: Junio C Hamano
Message 2 of 3 in “builtin/show-ref.c: avoid over-iterating with --heads, --tags”
  1. builtin/show-ref.c: avoid over-iterating with --heads, --tagsTaylor Blau, Jun 3, 2022
  2. Derrick StoleeJun 4, 2022
  3. Junio C HamanoJun 6, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.