From: Patrick Steinhardt Date: Tue, 20 Oct 2026 08:54:54 GMT Subject: [PATCH 3/4] odb/source-files: avoid rechecking already-known alternates Message-ID: <20261020-pks-odb-files-reload-alternates-v1-3-d9e156323836@pks.im> In-Reply-To: <20261020-pks-odb-files-reload-alternates-v1-0-d9e156323836@pks.im> When adding alternates we first check whether the alternate is usable via `odb_files_dir_is_usable()`. This function performs three checks: - It verifies that the alternate is not the same as the primary object directory. - It verifies that the alternate exists on disk. - It verifies that the alternate isn't already known to us by looking it up in `dirs_by_path`. The first two checks hit the filesystem, whereas the last one is a mere hashmap lookup. But the way the code is currently arranged we perform the hashmap lookup last, which may cause us to do a couple of syscalls only to then notice that we already know the alternate anyway, which is of course quite inefficient. Furthermore, the first check has gotten more expensive over time. While we traditionally stored the relative path to the primary object directory, we nowadays store an absolute path. This change is overall a sensible one as it brings it in line with alternate object directories, for which we always used to store absolute paths. But it also results in more calls to lstat(3p) when calling `strbuf_realpath()` to resolve the path of the primary object directory, as that syscall is made once per path component. Reorder the logic so that we perform the hash lookup first for an easy win. Using the same benchmark as in the preceding commit: Benchmark 1: ./git -C /tmp/repo cat-file --batch-check (rev = 5946a564cd~) Time (mean ± σ): 282.5 ms ± 2.9 ms [User: 100.0 ms, System: 178.4 ms] Range (min … max): 279.8 ms … 289.1 ms 10 runs Benchmark 2: ./git -C /tmp/repo cat-file --batch-check (rev = 5946a564cd) Time (mean ± σ): 181.1 ms ± 1.7 ms [User: 53.9 ms, System: 123.4 ms] Range (min … max): 178.2 ms … 183.6 ms 16 runs Benchmark 3: ./git -C /tmp/repo cat-file --batch-check (rev = HEAD~) Time (mean ± σ): 309.7 ms ± 2.7 ms [User: 108.0 ms, System: 197.4 ms] Range (min … max): 305.4 ms … 315.1 ms 10 runs Benchmark 4: ./git -C /tmp/repo cat-file --batch-check (rev = HEAD) Time (mean ± σ): 263.4 ms ± 3.6 ms [User: 92.2 ms, System: 167.1 ms] Range (min … max): 254.8 ms … 268.2 ms 11 runs Summary ./git -C /tmp/repo cat-file --batch-check (rev = 5946a564cd) ran 1.45 ± 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~) 1.71 ± 0.02 times faster than ./git -C /tmp/repo cat-file --batch-check (rev = HEAD~) So we're faster than the original, non-broken state now, but still significantly slower compared to the regressed commit where we didn't reload alternates at all. Signed-off-by: Patrick Steinhardt --- odb/source-files.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/odb/source-files.c b/odb/source-files.c index 86f3fd1136..917f5e9d25 100644 --- a/odb/source-files.c +++ b/odb/source-files.c @@ -228,16 +228,6 @@ static bool odb_files_dir_is_usable(struct odb_source_files *files, struct hashmap_entry key; bool usable = false; - strbuf_realpath(&normalized_objdir, files->dirs->abspath, 1); - - /* 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. @@ -249,12 +239,21 @@ static bool odb_files_dir_is_usable(struct odb_source_files *files, hashmap_add(&files->dirs_by_path, &files->dirs->by_path_entry); } + hashmap_entry_init(&key, strihash(path)); + if (hashmap_get(&files->dirs_by_path, &key, path)) + goto out; + + strbuf_realpath(&normalized_objdir, files->dirs->abspath, 1); if (!odb_files_dir_paths_cmp(files, path, normalized_objdir.buf)) goto out; - hashmap_entry_init(&key, strihash(path)); - if (hashmap_get(&files->dirs_by_path, &key, path)) + /* 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; -- 2.56.0.269.g68944adf51.dirty