[PATCH] git: improve changelog and summary performance for large repos
Mads Kiilerich
mads at kiilerich.com
Wed Dec 2 22:27:54 UTC 2026
Hi
The root cause seems to be that the vcs library assumes that a linear
topologically sorted list of enumerated commits in the DAG is readily
available for Git as it is for Mercurial. In hindsight, that
linearization seems like a clever implementation detail that Mercurial
shouldn't have exposed in the UI. Now it also shows in the Kallithea UI.
The "best" solution would probably be to remove the linearization from
the UI and from the vcs lib git support. That will probably imply
significant API changes and require major rework, so that is not feasible.
Instead, we must minimize the overhead of repeatedly (and unnecessarily)
making that linearization. It is generally append only ... or can be.
Anybody and everybody can trivially and consistently rebuild the list.
It is thus a simple caching problem, with minimal need for locking.
Cache invalidation can either happen explicitly when changing the repo,
or by always checking timestamp of some .git file before using the cache.
Storing it in memory for each server process does not seem like a good
solution. The efficiency would depend a lot on the lifetime of server
processes, and the amount of server processes vs number of different
repositories.
The "easy" solution in the existing Kallithea architecture would be to
add a beaker cache, probably keyed by the repo id, caching the repo
commit list for each repo in a separate file.
For some purposes, it could make sense to also store or cache commit
meta info in the database. But that would also be a major change.
The more performant solution would be a binary fileformat with records
(python structs or just the commit hash) with fixed size. A simple fstat
will tell the number of commits in the repo, and the hash of the commit
with a given index can be found with fseek without reading the whole
file. Appending also doesn't require reading/writing the whole file.
That is quite similar to what Mercurial does.
A quick search hints that https://git-scm.com/docs/commit-graph kind of
is and does the same thing. I don't know if that could/should be used,
and the caveats would be.
I suggest trying a beaker cache. I don't know if Mercurial would benefit
from it too, so perhaps keep it in the vcs git support. Conceptually, it
will be like an extremely lazy and cheating LazyProperty.
/Mads
On 12/2/26 04:35, 橋本 憲一 wrote:
> Hi Mads-san, Tomáš-san,
>
> Now that 0.7.1 is out, I would like to offer a change for very big git
> repositories. It goes further than the fixes in this thread, and we have
> used it in our in-house Kallithea since mid-November.
>
> Our case is a mirror of NetBSD src, now with about 1,080,000 revisions
> and 600 branches. The changelog often took more than a minute, and some
> requests failed with MemoryError (process data size limit).
>
> What remains in 0.7.1:
>
> 1. Each repository instance runs "git rev-list --all" again, so every
> request lists all revisions.
> 2. revisions is a list of str, about 100 bytes per revision, and each
> changeset finds its number with list.index.
> 3. get_changesets without filters walks the history again with
> "git log".
>
> The change, git backend only:
>
> - The rev-list output is cached per process, with the refs it was listed
> from, and reused while the refs, the shallow file and info/grafts are
> unchanged. git gets the commits of exactly those refs on stdin, in the
> order --all uses, so the cached list always belongs to the stored refs,
> also when a push happens while git runs. The refs are read with a new
> dulwich Repo, because dulwich never rereads packed-refs. The cache holds
> at most 64M characters per process (least recently used entries are
> dropped) and is guarded by a lock.
> - Only the default git_rev_filter --all is cached, and only when the
> repository has no other worktrees. Everything else is listed on each
> use as before.
> - revisions is a read-only sequence over the output string. Slices and
> reverse only narrow a range of line numbers. index() uses str.find
> with a memo shared by all views of the same output.
> - get_changesets without date, branch or max_revisions uses revisions
> instead of running "git log".
> - The heads are read once per repository instance. Everything derived
> from the refs is dropped when the instance changes them (fetch, pull,
> tag, remove_tag, in-memory commit).
> - Tests in kallithea/tests/vcs/test_git_revisions.py.
>
> Limits: replace refs, shallow and grafts are still read by git while it
> runs. The first request in each process still runs rev-list once.
>
> Measured with a bare copy of https://github.com/NetBSD/src (1,078,757
> revisions, 615 refs), calling the vcs layer like the first changelog
> page, median of 5 runs, each in a new process. git 2.43.0, Python 3.11,
> dulwich 0.24.10:
>
> first rev-list changelog 2nd changelog peak RSS
> 0.7.1 8.0 s 18.2 s 18.0 s 412 MB
> with the change 7.9 s 0.15 s 0.017 s 129 MB
>
> with commit-graph (the two versions run alternately)
> 0.7.1 4.9 s 10.1 s 10.1 s 412 MB
> with the change 4.3 s 0.15 s 0.016 s 129 MB
>
> The cache does not change the first rev-list in a process. A
> commit-graph ("git commit-graph write --reachable") roughly halves it.
> I can send the benchmark script.
>
> Two questions before I rebase it:
>
> - Mads-san, you suggested that folder browsing should use
> changeset_cache from the database. This change is in the vcs layer and
> also helps the changelog, so I think both can be done. Would you accept
> a per-process cache in the vcs layer, or do you prefer another
> direction?
> - Is a module-level cache shared by the threads of one process
> acceptable for the ways Kallithea is deployed?
>
> If you are interested, I will rebase it on 0.7.1, run the full test
> suite, and push it to kallithea-incoming as a separate branch.
>
> Best regards,
> Hashimoto Kenichi
>
>
> ________________________________________
> 差出人: kallithea-general <kallithea-general-bounces at sfconservancy.org> が Tomáš Musil <tomas.musil at atrak.cz> の代理で送信
> 送信日時: 2026年11月27日 20:07
> 宛先: Mads Kiilerich; kallithea-general at sfconservancy.org
> 件名: Re: [PATCH] git: improve changelog and summary performance for large repos
>
> Your changes work for me and from a quick test they seem to be even faster than my solution. Repository summary page is <1s and changelog page is ~1.5s
>
> Great find.
>
> Tomas
>
>
> Dne 26. 11. 26 v 23:19 Mads Kiilerich napsal(a):
> Thanks for the thorough analysis.
>
> I think I found the root cause for the issues I saw - it might be fixed in the attached patches. Does that work for you too?
>
> But also, folder browsing should probably just use changeset_cache from the database, without touching the actual repository ...
>
> /Mads
>
>
> On 26/11/2026 19:22, Tomáš Musil wrote:
>
> Hello,
>
> happy to help.
>
> Apologies in advance for the longer email.
>
> I was measuring the line-by-line performance of summary page. Measurements provided below contain only main culprit lines above 10 ms.
>
> Summary page:
>
> Total time: 6.7141 s
> File: Kallithea/kallithea/controllers/summary.py
> Function: SummaryController.index at line 116
>
> Line # Hits Time Per Hit % Time Line Contents
> ==============================================================
> ...
> 123 1 2422.3 2422.3 36.1 collection = c.db_repo_scm_instance.get_changesets(reverse=True)
> ...
> 127 1 1167.4 1167.4 17.4 c.cs_pagination = Page(collection, page=p, items_per_page=size)
> ...
> 167 1 3101.2 3101.2 46.2 return base.render('summary/summary.html')
>
> from that I drilled down to get_changesets:
>
> Total time: 2.38587 s
> File: Kallithea/kallithea/lib/vcs/backends/git/repository.py
> Function: GitRepository.get_changesets at line 554
>
> Line # Hits Time Per Hit % Time Line Contents
> ==============================================================
> ...
> 583 1 1175.0 1175.0 49.2 if self._empty:
> ...
> 600 1 1210.6 1210.6 50.7 revs = self.run_git_command(cmd).splitlines()
> ...
>
> through _empty:
>
> Total time: 1.17289 s
> File: Kallithea/kallithea/lib/vcs/backends/git/repository.py
> Function: GitRepository._empty at line 104
>
> Line # Hits Time Per Hit % Time Line Contents
> ==============================================================
> ...
> 111 1 1172.9 1172.9 100.0 self.revisions[0]
> ...
>
> and through _get_all_revisions:
>
> Total time: 1.17006 s
> File: Kallithea/kallithea/lib/vcs/backends/git/repository.py
> Function: GitRepository._get_all_revisions at line 310
>
> Line # Hits Time Per Hit % Time Line Contents
> ==============================================================
> ...
> 323 1 1168.3 1168.3 99.8 so = self.run_git_command(cmd)
> ...
>
> into _run_git_command:
>
> Total time: 1.16255 s
> File: Kallithea/kallithea/lib/vcs/backends/git/repository.py
> Function: GitRepository._run_git_command at line 125
>
> Line # Hits Time Per Hit % Time Line Contents
> ==============================================================
> ...
> 143 1 1141.9 1141.9 98.2 p = subprocessio.SubprocessIOChunker(cmd, cwd=cwd, env=gitenv, shell=False)
> ...
>
> From bottom to top:
>
> 1. mutliplying the default buffer_size and chunk_size in SubprocessIOChunker reduced this particular execution from 1141ms to 169ms. (Note again: I don't really understand the implications of this change so this is not my proposed solution).
> 2. as you've mentioned, the self.revisions is cached (for a single summary page display the _empty method is called 21 times but _get_all_revisions only once) but only for the duration of a single request, so reloading the page retriggers the calculation.
> 3. Even after the change in point 1 the first call of _empty still takes around 181ms, replacing the check for self.revisions with checking self.head lowers it to 0.2ms, though this call is not cached.
> 4. These changes lower the execution of get_changesets to 200ms from initial 2422ms, where the main culprit remains the run_git_command at line 600.
> 5. The overall SummaryController.index then takes about 2 seconds, where the most time remains in pagination on line 127 which I didn't manage to optimize. The rendering of summary page also seemingly went down to about 400ms, probably due to subsequent running of other git commands outside of the get_changesets.
>
> My logic behind the _empty change was that it's unnecessary to load all revisions just to check if there is any, so checking the head seemed fine to me.
> I've looked at the tests and I think the issue is that self.head checks for refs[b'HEAD'] but the GitInMemoryChangeset used for test repo preparation never sets that ref.
> With that said, I don't have enough knowledge to claim that the issue is in the tests rather than in my assumption that every non-empty repo has HEAD set, so I leave that up to you.
>
> Equivalent analysis was applied to the changelog page, where the issue was simply that calling _heads for each of the 100 changesets on the page iterated over all refs in the repo (including the 4k tags in my case) unnecessarily.
>
> Best regards.
>
> Tomas
>
> Dne 26. 11. 26 v 1:52 Mads Kiilerich napsal(a):
> Thanks.
>
> I finally got around to look at this. I pushed part of the changes to the stable branch.
>
> I do however see problems with the _empty fixes: They cause test failures. I have not investigated why. Also, self.revisions is cached and should not be recomputed that often. Can you clarify the exact code path and problem you see?
>
> /Mads
>
>
> On 24/04/2026 13:44, Tomáš Musil wrote:
> Hello,
>
> we have recently converted a large repo (22k revisions, 4k tags) from Mercurial to Git and noticed a major slowdown when loading summary (8 seconds) and changelog (24 seconds) pages, when for Mercurial repo the pages show basically instantly.
>
> I have tracked down the issues to:
>
> 1. _empty() loads all revisions but just checking if repo has "head" seems to do the trick
> 2. _heads() loads and iterates over all refs (including tags) but later only checks /refs/heads and /refs/remotes/origin
> 3. SubprocessIOChunker's default buffer_size and chunk_size seem to be pretty low in _run_git_command when running "git log" to get all revisions
>
> I attempted to address the first two points in the attached patch, in the third point I just mutiplied the defaults by 100 which seems to be enough for our repo but definitely is not a properly calculated solution.
>
> Overall these changes dropped summary loading time to <2sec and changelog to <3sec for our large repo.
>
> Thank you for keeping the Kallithea project alive.
>
> Best regards,
>
> Tomas
>
> _______________________________________________
> kallithea-general mailing list
> kallithea-general at sfconservancy.org<mailto:kallithea-general at sfconservancy.org>
> https://lists.sfconservancy.org/mailman/listinfo/kallithea-general
More information about the kallithea-general
mailing list