From: Patrick Steinhardt Date: Fri, 23 Oct 2026 12:48:05 GMT Subject: [PATCH v2 6/6] odb/source-files: only reload alternates when the file has changed Message-ID: <20261023-pks-odb-files-reload-alternates-v2-6-30559d7ff43b@pks.im> In-Reply-To: <20261023-pks-odb-files-reload-alternates-v2-0-30559d7ff43b@pks.im> In a preceding commit we have fixed a regression where we didn't re-read the alternates file anymore when we hit a missing object. But while correct, this change significantly hampers performance, even though we're roughly at the same level compared to before the regression. But the checks are unnecessarily expensive, as we always read and parse the alternates file even in the case where it has not changed. This of course translates into a lot of busywork for the common case, where the "objects/info/alternates" is still the exact same. Improve this by using a `struct stat_validity`. Like this we can easily short-circuit reading the alternates file in case we can determine via a stat(3p) call that the file hasn't changed. This significantly improves performance: Benchmark 1: ./git -C /tmp/repo cat-file --batch-check (rev = 5946a564cd~) (50 runs) mean ± σ min … max Wall Time 288.5 ms ± 2.3 ms 284.9 ms … 293.7 ms Memory 5.6 MiB ± 0.1 MiB 5.5 MiB … 5.7 MiB Benchmark 2: ./git -C /tmp/repo cat-file --batch-check (rev = 5946a564cd) (50 runs) mean ± σ min … max Wall Time 185.3 ms ± 3.4 ms 181.2 ms … 200.1 ms -35.8% (1.6x faster) Memory 5.6 MiB ± 0.1 MiB 5.5 MiB … 5.7 MiB -0.1% Benchmark 3: ./git -C /tmp/repo cat-file --batch-check (rev = HEAD~) (50 runs) mean ± σ min … max Wall Time 262.7 ms ± 2.7 ms 257.1 ms … 268.0 ms -9.0% Memory 5.9 MiB ± 0.1 MiB 5.6 MiB … 6.1 MiB +4.5% Benchmark 4: ./git -C /tmp/repo cat-file --batch-check (rev = HEAD) (50 runs) mean ± σ min … max Wall Time 199.5 ms ± 2.5 ms 195.4 ms … 207.8 ms -30.9% (1.4x faster) Memory 5.9 MiB ± 0.1 MiB 5.6 MiB … 6.0 MiB +4.3% We're significantly faster compared to the original state now and are almost as fast as the broken state where we don't reload alternates at all. This is probably good enough for now. Signed-off-by: Patrick Steinhardt --- odb/source-files.c | 39 ++++++++++++++++++++++++++++++--------- odb/source-files.h | 4 ++++ 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/odb/source-files.c b/odb/source-files.c index f7eb4a9df7..6b4ac3e6be 100644 --- a/odb/source-files.c +++ b/odb/source-files.c @@ -73,6 +73,7 @@ static void odb_source_files_free(struct odb_source *source) files->dirs = next; } strset_clear(&files->dir_paths); + stat_validity_clear(&files->alternates_validity); odb_source_release(&files->base); free(files); @@ -298,22 +299,41 @@ static void parse_alternates(const char *string, strbuf_release(&buf); } -static int read_alternates(const char *object_dir, struct strvec *out) +static void read_alternates(const char *object_dir, struct strvec *out, + struct stat_validity *validity) { struct strbuf buf = STRBUF_INIT; char *path; path = xstrfmt("%s/info/alternates", object_dir); - if (strbuf_read_file(&buf, path, 1024) < 0) { - warn_on_fopen_errors(path); - free(path); - return 0; + if (!validity || !stat_validity_check(validity, path)) { + bool successful = false; + int fd; + + fd = open(path, O_RDONLY); + if (fd < 0) { + warn_on_fopen_errors(path); + } else if (strbuf_read(&buf, fd, 1024) < 0) { + warning_errno(_("unable to read alternates file '%s'"), + path); + } else { + parse_alternates(buf.buf, '\n', object_dir, out); + successful = true; + } + + if (validity) { + if (successful) + stat_validity_update(validity, fd); + else + stat_validity_clear(validity); + } + + if (fd >= 0) + close(fd); } - parse_alternates(buf.buf, '\n', object_dir, out); strbuf_release(&buf); free(path); - return 0; } static void add_alternate_recursively(struct odb_source_files *files, @@ -345,7 +365,7 @@ static void add_alternate_recursively(struct odb_source_files *files, } /* recursively add alternates */ - read_alternates(alternate->abspath, &alternates); + read_alternates(alternate->abspath, &alternates, NULL); if (alternates.nr && depth + 1 > 5) { error(_("%s: ignoring alternate object stores, nesting too deep"), path); @@ -364,7 +384,8 @@ static void prepare_alternates(struct odb_source_files *files, struct strvec alternates = STRVEC_INIT; parse_alternates(alternate_db, PATH_SEP, NULL, &alternates); - read_alternates(files->base.path, &alternates); + read_alternates(files->base.path, &alternates, + &files->alternates_validity); for (size_t i = 0; i < alternates.nr; i++) add_alternate_recursively(files, alternates.v[i], 0); diff --git a/odb/source-files.h b/odb/source-files.h index 8695b6ee91..de058edaed 100644 --- a/odb/source-files.h +++ b/odb/source-files.h @@ -2,6 +2,7 @@ #define ODB_SOURCE_FILES_H #include "odb/source.h" +#include "statinfo.h" #include "strmap.h" struct odb_source_loose; @@ -61,6 +62,9 @@ struct odb_source_files { * determined by "core.ignoreCase". */ int dir_paths_icase; + + /* Stat data of the "info/alternates" file. */ + struct stat_validity alternates_validity; }; /* Allocate and initialize a new object source. */ -- 2.56.0.307.g5fe7bd680c.dirty