Skip to content

commit-graph: avoid object reads for refs already in the graph - #2322

Open
spkrka wants to merge 3 commits into
gitgitgadget:masterfrom
spkrka:commit-graph-has-oid
Open

spkrka wants to merge 3 commits into
gitgitgadget:masterfrom
spkrka:commit-graph-has-oid

Conversation

@spkrka

@spkrka spkrka commented Nov 20, 2026

Copy link
Copy Markdown

I ran into this while profiling "git fetch" on a repository with about
200k refs, where rewriting the commit-graph took around 20 seconds,
nearly all of it in add_ref_to_set() doing a cold object lookup for
every ref. For fetch itself that was addressed by
kk/fetch-write-commit-graph-incremental, which seeds the write from
the updated refs only, but the same per-ref cost remains for every
other caller of "git commit-graph write --reachable", and in the
condition "git maintenance run --auto" uses to decide whether to write
a commit-graph at all.

Both cases usually find that nearly every ref tip is already in the
existing graph, and a commit-graph only stores commits, so finding a
ref there is enough to know that it is a commit, without peeling it or
reading anything from the object database. The goal is for
maintenance to be cheap when there is little or nothing to do, which
is the common case in an active repository.

  • Patch 1 adds commit_graph_contains(), a plain membership test that
    neither parses the commit nor touches the object database.

  • Patch 2 uses it when collecting the refs for --reachable, for the
    ref itself and for what an annotated tag points to. The set of
    starting points does not change.

  • Patch 3 does the same in the auto condition of the commit-graph
    maintenance task, which runs after fetch, merge, commit and so on,
    and usually walks all refs only to decide there is nothing to do.

With the page cache dropped, on linux.git with 200k refs added and on
a private repository with 3.1M commits and 75k refs, "git commit-graph
write --reachable --split" goes from 5.62s to 1.12s and from 15.56s to
1.38s, and the maintenance auto condition from 6.05s to 0.52s and from
16.59s to 0.64s. The details are in the commit messages.

There is a possible follow-up that leaves known commits out of the
starting points of split writes altogether, saving the remaining
lookups and parsing of known ref tips, but since that changes what is
fed to the writer rather than how refs are classified, I left it out
of this series.

I am not sure how this interacts with Patrick's work on making commit
graphs pluggable [1], or with what is planned after it. The series
applies on master and merges cleanly with ps/odb-pluggable-commit-graphs
and with seen, but if it would get in the way, I am happy to hold it
until that has settled.

[1] https://lore.kernel.org/git/20261116-pks-odb-pluggable-commit-graphs-v1-0-a83fc1ff0808@pks.im/

cc: Patrick Steinhardt ps@pks.im
cc: Derrick Stolee stolee@gmail.com
cc: Taylor Blau me@ttaylorr.com

lookup_commit_in_graph() answers whether an object ID is in the
commit-graph, but it also allocates a commit object and parses it from
the graph, and, with GIT_COMMIT_GRAPH_PARANOIA, checks that the object
database still has it.

Some callers only want to know whether the graph has an entry for an
object ID.  Since a commit-graph only stores commits, that also tells
them that the object is a commit, without reading it.  In the
following commits this will be used to avoid object reads for refs
whose commits are already in the graph.

Add commit_graph_contains(), which only searches the graph.

Signed-off-by: Kristofer Karlsson <krka@spotify.com>
"git commit-graph write --reachable" collects its starting points by
peeling every ref and asking the object database for the type of the
peeled object, skipping anything that is not a commit.

With many refs this is slow.  Unless the ref backend stored a peeled
value, which it only does for annotated tags, peeling reads the object
header, and the type check reads it again.  Each ref costs pack index
lookups and reads at random offsets in the packs, which with a cold
cache means a page fault or more per ref.

In practice, most refs point directly to commits that are already in
the existing commit-graph.  A commit-graph only stores commits, so
finding the ref's object ID there is enough to know that it is a
commit that needs no peeling.

Teach add_ref_to_set() to search the graph for the ref's object ID
with commit_graph_contains() first, and to add it without reading any
object when it is found.
Otherwise peel the ref as before, and search the graph again for what
it points to, so that an annotated tag of a known commit only costs
reading the tag.  Only objects found in neither place are looked up in
the object database.  The set of starting points does not change.

The search does not parse the commit from the graph, so a full rewrite
still reads every commit from the object database.  If a ref points to
a commit that the graph lists but the object database lacks, a full
write now fails to parse it instead of silently leaving it out.  Add
tests for that, for a branch and for a tag.

This matters most for "git maintenance run --auto", which fetch, merge,
commit and other commands run afterwards: its default strategy
includes the commit-graph task, which runs "git commit-graph write
--split --reachable".

Measured on two repositories: linux.git (1.46M commits) with 200k refs
added, one for every 7th commit in its history, on top of its 955 refs,
952 of them annotated tags; and a private repository with 3.1M commits
and 75k refs.  With the page cache dropped before each run, median of
three:

  git commit-graph write --reachable --split
                         graph up to date        150 new commits
                         before     after        before     after
  linux.git               5.62s     1.12s         5.69s     1.38s
  private                15.56s     1.38s        17.01s     7.16s

Writes without --split re-read every commit, which takes minutes on
these repositories either way.

Signed-off-by: Kristofer Karlsson <krka@spotify.com>
"git maintenance run --auto" decides whether to run the commit-graph
task by counting commits that are reachable from refs but missing from
the commit-graph, stopping once maintenance.commit-graph.auto (100 by
default) is reached.  It runs after fetch, merge, commit and other
commands, and in a repository whose graph is up to date it usually
walks every ref and decides that there is nothing to do.

For each ref, dfs_on_ref() peels it and asks the object database for
its type before it parses the commit and finds that it is in the graph.
As in the previous commit, those are reads at random offsets in the
packs, and they make up nearly all of the time spent.

A commit that is in the graph has all its ancestors there too, so it
adds nothing to the count.  Use commit_graph_contains() to return
early for a ref whose object ID is in the graph, and after peeling, for
a tag of a commit in the graph.  The
count does not change: such refs were already ignored, just after
reading objects instead of before.

Measured on linux.git with 200k refs added, one for every 7th commit,
and on a private repository with 3.1M commits and 75k refs.  With the
graph up to date and the page cache dropped before each run, median of
three:

  git maintenance run --auto --task=commit-graph
                         before     after
  linux.git               6.05s     0.52s
  private                16.59s     0.64s

Signed-off-by: Kristofer Karlsson <krka@spotify.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant