get_packed_ref_cache(): assume "packed-refs" won't change while locked

If we've got the "packed-refs" file locked, then it can't change; there's no need to keep calling `stat_validity_check()` on it. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed May 22, 2017 at 16:17 UTC 28ed9830b1dc65df37bdb3c9b8ef743f1f266262
1 file changed +11 -5
refs/files-backend.c
+11 -5
@@ -342,13 +342,18 @@ static void files_ref_path(struct files_ref_store *refs,
342
343 /*
344 * Get the packed_ref_cache for the specified files_ref_store,
345 - * creating it if necessary.
345 + * creating and populating it if it hasn't been read before or if the
346 + * file has been changed (according to its `validity` field) since it
347 + * was last read. On the other hand, if we hold the lock, then assume
348 + * that the file hasn't been changed out from under us, so skip the
349 + * extra `stat()` call in `stat_validity_check()`.
350 */
351 static struct packed_ref_cache *get_packed_ref_cache(struct files_ref_store *refs)
352 {
353 const char *packed_refs_file = files_packed_refs_path(refs);
354
355 if (refs->packed &&
356 + !is_lock_file_locked(&refs->packed_refs_lock) &&
357 !stat_validity_check(&refs->packed->validity, packed_refs_file))
358 clear_packed_ref_cache(refs);
359
@@ -1288,10 +1293,11 @@ static int lock_packed_refs(struct files_ref_store *refs, int flags)
1293 flags, timeout_value) < 0)
1294 return -1;
1295 /*
1291 - * Get the current packed-refs while holding the lock. If the
1292 - * packed-refs file has been modified since we last read it,
1293 - * this will automatically invalidate the cache and re-read
1294 - * the packed-refs file.
1296 + * Get the current packed-refs while holding the lock. It is
1297 + * important that we call `get_packed_ref_cache()` before
1298 + * setting `packed_ref_cache->lock`, because otherwise the
1299 + * former will see that the file is locked and assume that the
1300 + * cache can't be stale.
1301 */
1302 packed_ref_cache = get_packed_ref_cache(refs);
1303 /* Increment the reference count to prevent it from being freed: */