Skip to content

Commit 28ed983

Browse files
mhaggergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 531cc4a commit 28ed983

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

refs/files-backend.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -342,13 +342,18 @@ static void files_ref_path(struct files_ref_store *refs,
342342

343343
/*
344344
* 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()`.
346350
*/
347351
static struct packed_ref_cache *get_packed_ref_cache(struct files_ref_store *refs)
348352
{
349353
const char *packed_refs_file = files_packed_refs_path(refs);
350354

351355
if (refs->packed &&
356+
!is_lock_file_locked(&refs->packed_refs_lock) &&
352357
!stat_validity_check(&refs->packed->validity, packed_refs_file))
353358
clear_packed_ref_cache(refs);
354359

@@ -1288,10 +1293,11 @@ static int lock_packed_refs(struct files_ref_store *refs, int flags)
12881293
flags, timeout_value) < 0)
12891294
return -1;
12901295
/*
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.
12951301
*/
12961302
packed_ref_cache = get_packed_ref_cache(refs);
12971303
/* Increment the reference count to prevent it from being freed: */

0 commit comments

Comments
 (0)