[PATCH v3 5/6] odb/source-files: avoid stat'ing already-known alternates
- From
Patrick Steinhardt <ps@pks.im>
- Date
- Oct 28, 2026, 07:11 UTC
- Message-ID
- <20261028-pks-odb-files-reload-alternates-v3-5-011ddb64b626@pks.im>
- In-Reply-To
- <20261028-pks-odb-files-reload-alternates-v3-0-011ddb64b626@pks.im>
When adding alternates we first check whether the alternate exists before checking whether we already know about it. This results in a useless stat(3p) syscall in the case where we already track the alternate.
Reorder the logic so that we first check whether we know the alternate already before checking whether its directory exists to avoid the syscall. Using the same benchmark as in the preceding commit:
Benchmark 1: ./git -C /tmp/repo cat-file --batch-check (rev = 5946a564cd~) (50 runs)
mean ± σ min … max
Wall Time 289.5 ms ± 3.4 ms 285.0 ms … 300.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.5 ms 181.2 ms … 200.8 ms -36.0% (1.6x faster)
Memory 5.7 MiB ± 0.0 MiB 5.5 MiB … 5.7 MiB +0.2% Benchmark 3: ./git -C /tmp/repo cat-file --batch-check (rev = HEAD~) (50 runs)
mean ± σ min … max
Wall Time 269.2 ms ± 4.5 ms 264.9 ms … 290.0 ms -7.0%
Memory 5.9 MiB ± 0.1 MiB 5.6 MiB … 6.1 MiB +4.9% Benchmark 4: ./git -C /tmp/repo cat-file --batch-check (rev = HEAD) (50 runs)
mean ± σ min … max
Wall Time 260.7 ms ± 3.4 ms 256.8 ms … 271.1 ms -10.0%
Memory 5.9 MiB ± 0.1 MiB 5.8 MiB … 6.1 MiB +4.7%Note that this causes a user-visible change in behaviour: when an alternate was already linked into the "files" backend, its directory disappears afterwards and we later try to reload alternates, then we now stop printing the error message that the object directory does not exist. That feels sufficiently niche to not really matter in practice though, and other than that the user-observable behaviour remains the exact same.
Signed-off-by: Patrick Steinhardt <ps@pks.im> --- odb/source-files.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
diff --git a/odb/source-files.c b/odb/source-files.c index 8b63d0eff2b..4a2b9039784 100644 --- a/odb/source-files.c +++ b/odb/source-files.c @@ -185,14 +185,6 @@ static bool odb_files_dir_is_usable(struct odb_source_files *files, struct strbuf buf = STRBUF_INIT; bool usable = false; - /* Detect cases where alternate disappeared */ - if (!is_directory(path)) { - error(_("object directory %s does not exist; " - "check .git/objects/info/alternates"), - path); - goto out; - } - /* * Prevent the common mistake of listing the same thing twice, * or object directory itself. @@ -241,6 +233,14 @@ static bool odb_files_dir_is_usable(struct odb_source_files *files, if (strset_contains(&files->dir_paths, buf.buf)) goto out; + /* Detect cases where alternate disappeared */ + if (!is_directory(path)) { + error(_("object directory %s does not exist; " + "check .git/objects/info/alternates"), + path); + goto out; + } + usable = true; out:
-- 2.56.0.341.g393f52fd96.dirty