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

Re: limiting git branch --contains

From
Jeff King <peff@peff.net>
Date
Mar 24, 2023, 19:10 UTC
Message-ID
<20230324191009.GA536967@coredump.intra.peff.net>
In-Reply-To
<594a358e-7bd4-e7a1-ad0f-7e41ca1fe767@github.com>
On Fri, Mar 24, 2023 at 01:23:32PM -0400, Derrick Stolee wrote:
Show 8 quoted lines
> Could you make sure to run 'git commit-graph write --reachable' before
> testing again?
> 
> When the commit-graph exists on disk, the algorithm does do a single
> reachability walk from all the initial points. If it does not exist,
> then each starting point triggers its own reachability walk, which
> is significantly slower. See repo_is_descendant_of() in commit-reach.c
> for more information on this split.

I'm a bit confused by that reference. We do switch behavior based on the presence of generation numbers in repo_is_descendant_of(). But ref-filter calls that function from commit_contains(), which is only fed one ref at a time. So we'll still do several walks, one per ref.

In commit_contains() we'll use the "tag algo" instead of calling repo_is_descendant_of(). It still sees the refs individually, but it keeps a cache to avoid walking over the same parts of history. We didn't traditionally use that algorithm for branches because it has a tendency to walk down to the roots (which is OK for tags, where you have old ones that require walking down that far anyway, but not for branches, where you can usually stop at a recent merge base). But now that we have reliable generation numbers, we can stop that traversal early.

But it doesn't look like we actually trigger the tag algo for anything but git-tag. I.e., I wonder if we should be doing something like this:

diff --git a/commit-reach.c b/commit-reach.c
index 7c0c39fd286..16c1a341bf5 100644
--- a/commit-reach.c
+++ b/commit-reach.c
@@ -712,7 +712,8 @@ static enum contains_result contains_tag_algo(struct commit *candidate,
 int commit_contains(struct ref_filter *filter, struct commit *commit,
 		    struct commit_list *list, struct contains_cache *cache)
 {
-	if (filter->with_commit_tag_algo)
+	if (filter->with_commit_tag_algo ||
+	    generation_numbers_enabled(the_repository))
 		return contains_tag_algo(commit, list, cache) == CONTAINS_YES;
 	return repo_is_descendant_of(the_repository, commit, list);
 }

The speedup is pretty minor compared to using commit-graphs at all.
Doing "git for-each-ref --format='%(refname)' --contains HEAD" on a
clone of linux.git gets me:

  - with no commit graph: 1m40s
  - after "commit-graph write --reachable": 30ms
  - plus the patch above; 23ms

So most of the help comes from not parsing the commit objects (courtesy
of the commit graph) and perhaps some early cutoffs (due to the use of
generation numbers in repo_is_descendant_of()). Using the cached walk
helps a little, but it may be more so for certain patterns of data.

I also scratched my head a little that we are still using
commit_contains() at all. I thought we now had functions to do a single
walk that would give us an answer for each ref, and that we could
trigger that in filter_refs(). And we do have reach_filter() there, but
I think it only handles --merged/--no-merged.

I admit I haven't kept up with the state of things here, so I'm not sure
what tools we have available.

-Peff
Previous: Oswald BuddenhagenNext: Kristofer Karlsson
Message 14 of 17 in “limiting git branch --contains”
  1. Oswald BuddenhagenMar 23, 2023
  2. Junio C HamanoMar 23, 2023
  3. Oswald BuddenhagenMar 23, 2023
  4. Derrick StoleeMar 24, 2023
  5. Oswald BuddenhagenMar 24, 2023
  6. Derrick StoleeMar 24, 2023
  7. Oswald BuddenhagenMar 24, 2023
  8. Jeff KingMar 24, 2023
  9. Oswald BuddenhagenMar 24, 2023
  10. Jeff KingMar 24, 2023
  11. Oswald BuddenhagenMar 24, 2023
  12. Jeff KingMar 25, 2023
  13. Oswald BuddenhagenMar 25, 2023
  14. Jeff KingMar 24, 2023
  15. Kristofer KarlssonMay 27, 2026
  16. Jeff KingJun 8, 2026
  17. Felipe ContrerasMar 23, 2023

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.