sha1_file: do not access pack if unneeded

Currently, regardless of the contents of the "struct object_info" passed to sha1_object_info_extended(), that function always accesses the packfile whenever it returns information about a packed object, since it needs to populate "u.packed". Add the ability to pass NULL, and use NULL-ness of the argument to activate an optimization in which sha1_object_info_extended() does not needlessly access the packfile. A subsequent patch will make use of this optimization. A similar optimization is not made for the cached and loose cases as it would not cause a significant performance improvement. Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Tan committed Jun 21, 2017 at 17:40 UTC cd585e2a33841d725b821adbb9b48654fc7d0b61
1 file changed +11
sha1_file.c
+11
@@ -2977,12 +2977,16 @@ static int sha1_loose_object_info(const unsigned char *sha1,
2977
2978 int sha1_object_info_extended(const unsigned char *sha1, struct object_info *oi, unsigned flags)
2979 {
2980 + static struct object_info blank_oi = OBJECT_INFO_INIT;
2981 struct pack_entry e;
2982 int rtype;
2983 const unsigned char *real = (flags & OBJECT_INFO_LOOKUP_REPLACE) ?
2984 lookup_replace_object(sha1) :
2985 sha1;
2986
2987 + if (!oi)
2988 + oi = &blank_oi;
2989 +
2990 if (!(flags & OBJECT_INFO_SKIP_CACHED)) {
2991 struct cached_object *co = find_cached_object(real);
2992 if (co) {
@@ -3020,6 +3024,13 @@ int sha1_object_info_extended(const unsigned char *sha1, struct object_info *oi,
3024 }
3025 }
3026
3027 + if (oi == &blank_oi)
3028 + /*
3029 + * We know that the caller doesn't actually need the
3030 + * information below, so return early.
3031 + */
3032 + return 0;
3033 +
3034 rtype = packed_object_info(e.p, e.offset, oi);
3035 if (rtype < 0) {
3036 mark_bad_packed_object(e.p, real);