Skip to content

Commit 9a43523

Browse files
committed
Merge branch 'ps/midx-negative-packfile-cache'
When a stale .midx file refers to .pack files that no longer exist, we ended up checking for these non-existent files repeatedly, which has been optimized by memoizing the non-existence. * ps/midx-negative-packfile-cache: midx: stop repeatedly looking up nonexistent packfiles packfile: explain ordering of how we look up auxiliary pack files
2 parents 1a140c8 + 1f34bf3 commit 9a43523

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

midx.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include "pack-bitmap.h"
1414
#include "pack-revindex.h"
1515

16+
#define MIDX_PACK_ERROR ((void *)(intptr_t)-1)
17+
1618
int midx_checksum_valid(struct multi_pack_index *m);
1719
void clear_midx_files_ext(const char *object_dir, const char *ext,
1820
const char *keep_hash);
@@ -405,7 +407,7 @@ void close_midx(struct multi_pack_index *m)
405407
munmap((unsigned char *)m->data, m->data_len);
406408

407409
for (i = 0; i < m->num_packs; i++) {
408-
if (m->packs[i])
410+
if (m->packs[i] && m->packs[i] != MIDX_PACK_ERROR)
409411
m->packs[i]->multi_pack_index = 0;
410412
}
411413
FREE_AND_NULL(m->packs);
@@ -458,6 +460,8 @@ int prepare_midx_pack(struct repository *r, struct multi_pack_index *m,
458460

459461
pack_int_id = midx_for_pack(&m, pack_int_id);
460462

463+
if (m->packs[pack_int_id] == MIDX_PACK_ERROR)
464+
return 1;
461465
if (m->packs[pack_int_id])
462466
return 0;
463467

@@ -482,8 +486,10 @@ int prepare_midx_pack(struct repository *r, struct multi_pack_index *m,
482486
strbuf_release(&pack_name);
483487
strbuf_release(&key);
484488

485-
if (!p)
489+
if (!p) {
490+
m->packs[pack_int_id] = MIDX_PACK_ERROR;
486491
return 1;
492+
}
487493

488494
p->multi_pack_index = 1;
489495
m->packs[pack_int_id] = p;
@@ -495,6 +501,8 @@ struct packed_git *nth_midxed_pack(struct multi_pack_index *m,
495501
uint32_t pack_int_id)
496502
{
497503
uint32_t local_pack_int_id = midx_for_pack(&m, pack_int_id);
504+
if (m->packs[local_pack_int_id] == MIDX_PACK_ERROR)
505+
return NULL;
498506
return m->packs[local_pack_int_id];
499507
}
500508

packfile.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,17 @@ struct packed_git *add_packed_git(struct repository *r, const char *path,
737737
p = alloc_packed_git(r, alloc);
738738
memcpy(p->pack_name, path, path_len);
739739

740+
/*
741+
* Note that we have to check auxiliary data structures before we check
742+
* for the ".pack" file to exist to avoid races with a packfile that is
743+
* in the process of being deleted. The ".pack" file is unlinked before
744+
* its auxiliary data structures, so we know that we either get a
745+
* consistent snapshot of all data structures or that we'll fail to
746+
* stat(3p) the packfile itself and thus return `NULL`.
747+
*
748+
* As such, we cannot bail out before the access(3p) calls in case the
749+
* packfile doesn't exist without doing two stat(3p) calls for it.
750+
*/
740751
xsnprintf(p->pack_name + path_len, alloc - path_len, ".keep");
741752
if (!access(p->pack_name, F_OK))
742753
p->pack_keep = 1;

0 commit comments

Comments
 (0)