index-pack: avoid excessive re-reading of pack directory

Since 45e8a74 (has_sha1_file: re-check pack directory before giving up, 2013-08-30), we spend extra effort for has_sha1_file to give the right answer when somebody else is repacking. Usually this effort does not matter, because after finding that the object does not exist, the next step is usually to die(). However, some code paths make a large number of has_sha1_file checks which are _not_ expected to return 1. The collision test in index-pack.c is such a case. On a local system, this can cause a performance slowdown of around 5%. But on a system with high-latency system calls (like NFS), it can be much worse. This patch introduces a "quick" flag to has_sha1_file which callers can use when they would prefer high performance at the cost of false negatives during repacks. There may be other code paths that can use this, but the index-pack one is the most obviously critical, so we'll start with switching that one. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jun 9, 2015 at 13:24 UTC 0eeb077be7d526635995e17808927c94c70be665
3 files changed +14 -3
builtin/index-pack.c
+1 -1
@@ -721,7 +721,7 @@ static void sha1_object(const void *data, struct object_entry *obj_entry,
721 assert(data || obj_entry);
722
723 read_lock();
724 - collision_test_needed = has_sha1_file(sha1);
724 + collision_test_needed = has_sha1_file_with_flags(sha1, HAS_SHA1_QUICK);
725 read_unlock();
726
727 if (collision_test_needed && !data) {
cache.h
+10 -1
@@ -873,8 +873,17 @@ extern int has_sha1_pack(const unsigned char *sha1);
873 * Return true iff we have an object named sha1, whether local or in
874 * an alternate object database, and whether packed or loose. This
875 * function does not respect replace references.
876 + *
877 + * If the QUICK flag is set, do not re-check the pack directory
878 + * when we cannot find the object (this means we may give a false
879 + * negative answer if another process is simultaneously repacking).
880 */
877 -extern int has_sha1_file(const unsigned char *sha1);
881 +#define HAS_SHA1_QUICK 0x1
882 +extern int has_sha1_file_with_flags(const unsigned char *sha1, int flags);
883 +static inline int has_sha1_file(const unsigned char *sha1)
884 +{
885 + return has_sha1_file_with_flags(sha1, 0);
886 +}
887
888 /*
889 * Return true iff an alternate object database has a loose object
sha1_file.c
+3 -1
@@ -3017,7 +3017,7 @@ int has_sha1_pack(const unsigned char *sha1)
3017 return find_pack_entry(sha1, &e);
3018 }
3019
3020 -int has_sha1_file(const unsigned char *sha1)
3020 +int has_sha1_file_with_flags(const unsigned char *sha1, int flags)
3021 {
3022 struct pack_entry e;
3023
@@ -3025,6 +3025,8 @@ int has_sha1_file(const unsigned char *sha1)
3025 return 1;
3026 if (has_loose_object(sha1))
3027 return 1;
3028 + if (flags & HAS_SHA1_QUICK)
3029 + return 0;
3030 reprepare_packed_git();
3031 return find_pack_entry(sha1, &e);
3032 }