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 <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
main
Patrick Steinhardt 2025-10-30 11:38:42 +01:00 committed by Junio C Hamano
parent 02a7f6ffab
commit 0d0e4b5954
1 changed files with 14 additions and 14 deletions

View File

@ -4388,27 +4388,27 @@ static void add_unreachable_loose_objects(struct rev_info *revs)

static int has_sha1_pack_kept_or_nonlocal(const struct object_id *oid)
{
struct packfile_store *packs = the_repository->objects->packfiles;
static struct packed_git *last_found = (void *)1;
static struct packed_git *last_found = NULL;
struct packed_git *p;

p = (last_found != (void *)1) ? last_found :
packfile_store_get_packs(packs);
if (last_found && find_pack_entry_one(oid, last_found))
return 1;

while (p) {
if ((!p->pack_local || p->pack_keep ||
p->pack_keep_in_core) &&
find_pack_entry_one(oid, p)) {
repo_for_each_pack(the_repository, p) {
/*
* We have already checked `last_found`, so there is no need to
* re-check here.
*/
if (p == last_found)
continue;

if ((!p->pack_local || p->pack_keep || p->pack_keep_in_core) &&
find_pack_entry_one(oid, p)) {
last_found = p;
return 1;
}
if (p == last_found)
p = packfile_store_get_packs(packs);
else
p = p->next;
if (p == last_found)
p = p->next;
}

return 0;
}