Skip to content

Commit 0d0e4b5

Browse files
pks-tgitster
authored andcommitted
builtin/pack-objects: simplify logic to find kept or nonlocal objects
The function `has_sha1_pack_kept_or_nonlocal()` takes an object ID and then searches through packed objects to figure out whether the object exists in a kept or non-local pack. As a performance optimization we remember the packfile that contains a given object ID so that the next call to the function first checks that same packfile again. The way this is written is rather hard to follow though, as the caching mechanism is intertwined with the loop that iterates through the packs. Consequently, we need to do some gymnastics to re-start the iteration if the cached pack does not contain the objects. Refactor this so that we check the cached packfile at the beginning. We don't have to re-verify whether the packfile meets the properties as we have already verified those when storing the pack in `last_found` in the first place. So all we need to do is to use `find_pack_entry_one()` to check whether the pack contains the object ID, and to skip the cached pack in the loop so that we don't search it twice. Furthermore, stop using the `(void *)1` sentinel value and instead use a simple `NULL` pointer to indicate that we don't have a last-found pack yet. This refactoring significantly simplifies the logic and makes it much easier to follow. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 02a7f6f commit 0d0e4b5

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

builtin/pack-objects.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4388,27 +4388,27 @@ static void add_unreachable_loose_objects(struct rev_info *revs)
43884388

43894389
static int has_sha1_pack_kept_or_nonlocal(const struct object_id *oid)
43904390
{
4391-
struct packfile_store *packs = the_repository->objects->packfiles;
4392-
static struct packed_git *last_found = (void *)1;
4391+
static struct packed_git *last_found = NULL;
43934392
struct packed_git *p;
43944393

4395-
p = (last_found != (void *)1) ? last_found :
4396-
packfile_store_get_packs(packs);
4394+
if (last_found && find_pack_entry_one(oid, last_found))
4395+
return 1;
43974396

4398-
while (p) {
4399-
if ((!p->pack_local || p->pack_keep ||
4400-
p->pack_keep_in_core) &&
4401-
find_pack_entry_one(oid, p)) {
4397+
repo_for_each_pack(the_repository, p) {
4398+
/*
4399+
* We have already checked `last_found`, so there is no need to
4400+
* re-check here.
4401+
*/
4402+
if (p == last_found)
4403+
continue;
4404+
4405+
if ((!p->pack_local || p->pack_keep || p->pack_keep_in_core) &&
4406+
find_pack_entry_one(oid, p)) {
44024407
last_found = p;
44034408
return 1;
44044409
}
4405-
if (p == last_found)
4406-
p = packfile_store_get_packs(packs);
4407-
else
4408-
p = p->next;
4409-
if (p == last_found)
4410-
p = p->next;
44114410
}
4411+
44124412
return 0;
44134413
}
44144414

0 commit comments

Comments
 (0)