midx.c: extract `midx_fanout_add_pack_fanout()`

Extract a routine to add all objects whose object ID's first byte is `cur_fanout` from a given pack (identified by its index into the `struct pack_info` array maintained by the MIDX writing routine). Unlike the previous extraction (for `midx_fanout_add_midx_fanout()`), this function will be called twice, once for all new packs, and again for the preferred pack (if it appears in an existing MIDX). The latter change is to resolve the bug described a few patches ago, and will be made in the subsequent commit. Similar to the previous refactoring, this function also enhances the readability of its caller in `get_sorted_entries()`. Its functionality is unchanged in this commit. Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Taylor Blau committed Aug 22, 2022 at 15:50 UTC 1d6f4c64084f31bd1be558ae72f25e7600c39f4d
1 file changed +28 -15
midx.c
+28 -15
@@ -618,6 +618,31 @@ static void midx_fanout_add_midx_fanout(struct midx_fanout *fanout,
618 }
619 }
620
621 +static void midx_fanout_add_pack_fanout(struct midx_fanout *fanout,
622 + struct pack_info *info,
623 + uint32_t cur_pack,
624 + int preferred,
625 + uint32_t cur_fanout)
626 +{
627 + struct packed_git *pack = info[cur_pack].p;
628 + uint32_t start = 0, end;
629 + uint32_t cur_object;
630 +
631 + if (cur_fanout)
632 + start = get_pack_fanout(pack, cur_fanout - 1);
633 + end = get_pack_fanout(pack, cur_fanout);
634 +
635 + for (cur_object = start; cur_object < end; cur_object++) {
636 + midx_fanout_grow(fanout, fanout->nr + 1);
637 + fill_pack_entry(cur_pack,
638 + info[cur_pack].p,
639 + cur_object,
640 + &fanout->entries[fanout->nr],
641 + preferred);
642 + fanout->nr++;
643 + }
644 +}
645 +
646 /*
647 * It is possible to artificially get into a state where there are many
648 * duplicate copies of objects. That can create high memory pressure if
@@ -663,22 +688,10 @@ static struct pack_midx_entry *get_sorted_entries(struct multi_pack_index *m,
688 cur_fanout);
689
690 for (cur_pack = start_pack; cur_pack < nr_packs; cur_pack++) {
666 - uint32_t start = 0, end;
691 int preferred = cur_pack == preferred_pack;
668 -
669 - if (cur_fanout)
670 - start = get_pack_fanout(info[cur_pack].p, cur_fanout - 1);
671 - end = get_pack_fanout(info[cur_pack].p, cur_fanout);
672 -
673 - for (cur_object = start; cur_object < end; cur_object++) {
674 - midx_fanout_grow(&fanout, fanout.nr + 1);
675 - fill_pack_entry(cur_pack,
676 - info[cur_pack].p,
677 - cur_object,
678 - &fanout.entries[fanout.nr],
679 - preferred);
680 - fanout.nr++;
681 - }
692 + midx_fanout_add_pack_fanout(&fanout,
693 + info, cur_pack,
694 + preferred, cur_fanout);
695 }
696
697 midx_fanout_sort(&fanout);