[PATCH 4/4] odb/source-files: only reload alternates when the file has changed
- From
Patrick Steinhardt <ps@pks.im>
- Date
- Oct 20, 2026, 08:54 UTC
- Message-ID
- <20261020-pks-odb-files-reload-alternates-v1-4-d9e156323836@pks.im>
- In-Reply-To
- <20261020-pks-odb-files-reload-alternates-v1-0-d9e156323836@pks.im>
In the 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~)
Time (mean ± σ): 280.7 ms ± 2.1 ms [User: 95.7 ms, System: 181.0 ms]
Range (min … max): 277.9 ms … 285.0 ms 10 runs Benchmark 2: ./git -C /tmp/repo cat-file --batch-check (rev = 5946a564cd)
Time (mean ± σ): 179.5 ms ± 1.7 ms [User: 60.5 ms, System: 115.6 ms]
Range (min … max): 177.6 ms … 183.2 ms 16 runs Benchmark 3: ./git -C /tmp/repo cat-file --batch-check (rev = HEAD~)
Time (mean ± σ): 257.2 ms ± 3.5 ms [User: 94.8 ms, System: 158.3 ms]
Range (min … max): 252.6 ms … 262.1 ms 11 runs Benchmark 4: ./git -C /tmp/repo cat-file --batch-check (rev = HEAD)
Time (mean ± σ): 197.4 ms ± 3.8 ms [User: 66.9 ms, System: 126.7 ms]
Range (min … max): 192.3 ms … 203.3 ms 14 runs Summary
./git -C /tmp/repo cat-file --batch-check (rev = 5946a564cd) ran
1.10 ± 0.02 times faster than ./git -C /tmp/repo cat-file --batch-check (rev = HEAD)
1.43 ± 0.02 times faster than ./git -C /tmp/repo cat-file --batch-check (rev = HEAD~)
1.56 ± 0.02 times faster than ./git -C /tmp/repo cat-file --batch-check (rev = 5946a564cd~)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 <ps@pks.im> --- odb/source-files.c | 31 ++++++++++++++++++++++--------- odb/source-files.h | 4 ++++ 2 files changed, 26 insertions(+), 9 deletions(-)
diff --git a/odb/source-files.c b/odb/source-files.c index 917f5e9d25..8ac4f7f380 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; } hashmap_clear(&files->dirs_by_path); + stat_validity_clear(&files->alternates_validity); odb_source_release(&files->base); free(files); @@ -329,22 +330,33 @@ 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)) { + int fd = open(path, O_RDONLY); + if (fd < 0) { + warn_on_fopen_errors(path); + if (validity) + stat_validity_clear(validity); + } else { + if (validity) + stat_validity_update(validity, fd); + 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); + 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, @@ -370,7 +382,7 @@ static void add_alternate_recursively(struct odb_source_files *files, hashmap_add(&files->dirs_by_path, &alternate->by_path_entry); /* 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); @@ -389,7 +401,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->dirs->abspath, &alternates); + read_alternates(files->dirs->abspath, &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 837e890304..b0795cd384 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" struct odb_source_loose; struct odb_source_packed; @@ -66,6 +67,9 @@ struct odb_source_files { * determined by "core.ignoreCase". */ int dirs_paths_icase; + + /* Stat data of the "info/alternates" file. */ + struct stat_validity alternates_validity; }; /* Allocate and initialize a new object source. */
-- 2.56.0.269.g68944adf51.dirty