midx-write.c: check count of packs to repack after grouping

In both fill_included_packs_all() and fill_included_packs_batch(), we accumulate a list of packs whose contents we want to repack together, and then use that information to feed a list of objects as input to pack-objects. In both cases, the `fill_included_packs_` functions keep track of how many packs they want to repack together, and only execute pack-objects if there are at least two packs that need repacking. Having both of these functions keep track of this information themselves is not strictly necessary, since they also log which packs to repack via the `include_pack` array, so we can simply count the non-zero entries in that array after either function is done executing, reducing the overall amount of code necessary. Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Taylor Blau committed Apr 1, 2024 at 17:16 UTC 440e470edb183d020d2982d90d27113f05189727
1 file changed +20 -24
midx-write.c
+20 -24
@@ -1367,11 +1367,11 @@ static int want_included_pack(struct repository *r,
1367 return 1;
1368 }
1369
1370 -static int fill_included_packs_all(struct repository *r,
1371 - struct multi_pack_index *m,
1372 - unsigned char *include_pack)
1370 +static void fill_included_packs_all(struct repository *r,
1371 + struct multi_pack_index *m,
1372 + unsigned char *include_pack)
1373 {
1374 - uint32_t i, count = 0;
1374 + uint32_t i;
1375 int pack_kept_objects = 0;
1376
1377 repo_config_get_bool(r, "repack.packkeptobjects", &pack_kept_objects);
@@ -1381,18 +1381,15 @@ static int fill_included_packs_all(struct repository *r,
1381 continue;
1382
1383 include_pack[i] = 1;
1384 - count++;
1384 }
1386 -
1387 - return count < 2;
1385 }
1386
1390 -static int fill_included_packs_batch(struct repository *r,
1391 - struct multi_pack_index *m,
1392 - unsigned char *include_pack,
1393 - size_t batch_size)
1387 +static void fill_included_packs_batch(struct repository *r,
1388 + struct multi_pack_index *m,
1389 + unsigned char *include_pack,
1390 + size_t batch_size)
1391 {
1395 - uint32_t i, packs_to_repack;
1392 + uint32_t i;
1393 size_t total_size;
1394 struct repack_info *pack_info;
1395 int pack_kept_objects = 0;
@@ -1418,7 +1415,6 @@ static int fill_included_packs_batch(struct repository *r,
1415 QSORT(pack_info, m->num_packs, compare_by_mtime);
1416
1417 total_size = 0;
1421 - packs_to_repack = 0;
1418 for (i = 0; total_size < batch_size && i < m->num_packs; i++) {
1419 int pack_int_id = pack_info[i].pack_int_id;
1420 struct packed_git *p = m->packs[pack_int_id];
@@ -1434,23 +1430,17 @@ static int fill_included_packs_batch(struct repository *r,
1430 if (expected_size >= batch_size)
1431 continue;
1432
1437 - packs_to_repack++;
1433 total_size += expected_size;
1434 include_pack[pack_int_id] = 1;
1435 }
1436
1437 free(pack_info);
1443 -
1444 - if (packs_to_repack < 2)
1445 - return 1;
1446 -
1447 - return 0;
1438 }
1439
1440 int midx_repack(struct repository *r, const char *object_dir, size_t batch_size, unsigned flags)
1441 {
1442 int result = 0;
1453 - uint32_t i;
1443 + uint32_t i, packs_to_repack = 0;
1444 unsigned char *include_pack;
1445 struct child_process cmd = CHILD_PROCESS_INIT;
1446 FILE *cmd_in;
@@ -1469,10 +1459,16 @@ int midx_repack(struct repository *r, const char *object_dir, size_t batch_size,
1459
1460 CALLOC_ARRAY(include_pack, m->num_packs);
1461
1472 - if (batch_size) {
1473 - if (fill_included_packs_batch(r, m, include_pack, batch_size))
1474 - goto cleanup;
1475 - } else if (fill_included_packs_all(r, m, include_pack))
1462 + if (batch_size)
1463 + fill_included_packs_batch(r, m, include_pack, batch_size);
1464 + else
1465 + fill_included_packs_all(r, m, include_pack);
1466 +
1467 + for (i = 0; i < m->num_packs; i++) {
1468 + if (include_pack[i])
1469 + packs_to_repack++;
1470 + }
1471 + if (packs_to_repack <= 1)
1472 goto cleanup;
1473
1474 repo_config_get_bool(r, "repack.usedeltabaseoffset", &delta_base_offset);