Skip to content

Commit 5dff093

Browse files
ttaylorrgitster
authored andcommitted
midx-write.c: extract inner loop from fill_packs_from_midx()
The function fill_packs_from_midx() does relatively little, but ends up in a doubly-nested loop because we're enumerating each pack within each layer of the incremental MIDX chain. Let's de-dent the inner loop of fill_packs_from_midx() by extracting its contents into a separate function, and calling that. Signed-off-by: Taylor Blau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 76ec9c2 commit 5dff093

File tree

1 file changed

+41
-31
lines changed

1 file changed

+41
-31
lines changed

midx-write.c

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -938,44 +938,54 @@ static struct multi_pack_index *lookup_multi_pack_index(struct repository *r,
938938
return result;
939939
}
940940

941+
static int fill_packs_from_midx_1(struct write_midx_context *ctx,
942+
struct multi_pack_index *m,
943+
int prepare_packs)
944+
{
945+
for (uint32_t i = 0; i < m->num_packs; i++) {
946+
/*
947+
* If generating a reverse index, need to have
948+
* packed_git's loaded to compare their
949+
* mtimes and object count.
950+
*/
951+
if (prepare_packs) {
952+
if (prepare_midx_pack(ctx->repo, m,
953+
m->num_packs_in_base + i)) {
954+
error(_("could not load pack"));
955+
return 1;
956+
}
957+
958+
if (open_pack_index(m->packs[i]))
959+
die(_("could not open index for %s"),
960+
m->packs[i]->pack_name);
961+
}
962+
963+
fill_pack_info(&ctx->info[ctx->nr++], m->packs[i],
964+
m->pack_names[i],
965+
m->num_packs_in_base + i);
966+
}
967+
968+
return 0;
969+
}
970+
941971
static int fill_packs_from_midx(struct write_midx_context *ctx,
942972
const char *preferred_pack_name, uint32_t flags)
943973
{
944974
struct multi_pack_index *m;
975+
int prepare_packs;
945976

946-
for (m = ctx->m; m; m = m->base_midx) {
947-
uint32_t i;
948-
949-
for (i = 0; i < m->num_packs; i++) {
950-
ALLOC_GROW(ctx->info, ctx->nr + 1, ctx->alloc);
951-
952-
/*
953-
* If generating a reverse index, need to have
954-
* packed_git's loaded to compare their
955-
* mtimes and object count.
956-
*
957-
* If a preferred pack is specified, need to
958-
* have packed_git's loaded to ensure the chosen
959-
* preferred pack has a non-zero object count.
960-
*/
961-
if (flags & MIDX_WRITE_REV_INDEX ||
962-
preferred_pack_name) {
963-
if (prepare_midx_pack(ctx->repo, m,
964-
m->num_packs_in_base + i)) {
965-
error(_("could not load pack"));
966-
return 1;
967-
}
968-
969-
if (open_pack_index(m->packs[i]))
970-
die(_("could not open index for %s"),
971-
m->packs[i]->pack_name);
972-
}
977+
/*
978+
* If generating a reverse index, need to have packed_git's
979+
* loaded to compare their mtimes and object count.
980+
*/
981+
prepare_packs = !!(flags & MIDX_WRITE_REV_INDEX || preferred_pack_name);
973982

974-
fill_pack_info(&ctx->info[ctx->nr++], m->packs[i],
975-
m->pack_names[i],
976-
m->num_packs_in_base + i);
977-
}
983+
for (m = ctx->m; m; m = m->base_midx) {
984+
int ret = fill_packs_from_midx_1(ctx, m, prepare_packs);
985+
if (ret)
986+
return ret;
978987
}
988+
979989
return 0;
980990
}
981991

0 commit comments

Comments
 (0)