midx: use `strset` for retained MIDX files

Both `clear_midx_files_ext()` and `clear_incremental_midx_files_ext()` build a list of filenames to keep while pruning stale MIDX files. Today they hand-roll an array instead of using a `strset`, thus requiring us to pass an additional length parameter, and makes lookups linear. Replace the bare array with a `strset` which can be passed around as a single parameter. Though it improves lookup performance, the difference is likely immeasurable given how small the keep_hashes array typically is. Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Taylor Blau committed May 19, 2026 at 11:57 UTC ddaa7a6fb79038a30b59341ed3f0f2097014ccbf
1 file changed +27 -30
midx.c
+27 -30
@@ -758,8 +758,7 @@ int midx_checksum_valid(struct multi_pack_index *m)
758 }
759
760 struct clear_midx_data {
761 - char **keep;
762 - uint32_t keep_nr;
761 + struct strset keep;
762 const char *ext;
763 };
764
@@ -767,15 +766,12 @@ static void clear_midx_file_ext(const char *full_path, size_t full_path_len UNUS
766 const char *file_name, void *_data)
767 {
768 struct clear_midx_data *data = _data;
770 - uint32_t i;
769
770 if (!(starts_with(file_name, "multi-pack-index-") &&
771 ends_with(file_name, data->ext)))
772 return;
775 - for (i = 0; i < data->keep_nr; i++) {
776 - if (!strcmp(data->keep[i], file_name))
777 - return;
778 - }
773 + if (strset_contains(&data->keep, file_name))
774 + return;
775 if (unlink(full_path))
776 die_errno(_("failed to remove %s"), full_path);
777 }
@@ -783,48 +779,49 @@ static void clear_midx_file_ext(const char *full_path, size_t full_path_len UNUS
779 void clear_midx_files_ext(struct odb_source *source, const char *ext,
780 const char *keep_hash)
781 {
786 - struct clear_midx_data data;
787 - memset(&data, 0, sizeof(struct clear_midx_data));
782 + struct clear_midx_data data = {
783 + .keep = STRSET_INIT,
784 + .ext = ext,
785 + };
786
787 if (keep_hash) {
790 - ALLOC_ARRAY(data.keep, 1);
788 + struct strbuf buf = STRBUF_INIT;
789 + strbuf_addf(&buf, "multi-pack-index-%s.%s", keep_hash, ext);
790 +
791 + strset_add(&data.keep, buf.buf);
792
792 - data.keep[0] = xstrfmt("multi-pack-index-%s.%s", keep_hash, ext);
793 - data.keep_nr = 1;
793 + strbuf_release(&buf);
794 }
795 - data.ext = ext;
795
797 - for_each_file_in_pack_dir(source->path,
798 - clear_midx_file_ext,
799 - &data);
796 + for_each_file_in_pack_dir(source->path, clear_midx_file_ext, &data);
797
801 - if (keep_hash)
802 - free(data.keep[0]);
803 - free(data.keep);
798 + strset_clear(&data.keep);
799 }
800
801 void clear_incremental_midx_files_ext(struct odb_source *source, const char *ext,
802 char **keep_hashes,
803 uint32_t hashes_nr)
804 {
810 - struct clear_midx_data data;
805 + struct clear_midx_data data = {
806 + .keep = STRSET_INIT,
807 + .ext = ext,
808 + };
809 + struct strbuf buf = STRBUF_INIT;
810 uint32_t i;
811
813 - memset(&data, 0, sizeof(struct clear_midx_data));
812 + for (i = 0; i < hashes_nr; i++) {
813 + strbuf_reset(&buf);
814 + strbuf_addf(&buf, "multi-pack-index-%s.%s", keep_hashes[i],
815 + ext);
816
815 - ALLOC_ARRAY(data.keep, hashes_nr);
816 - for (i = 0; i < hashes_nr; i++)
817 - data.keep[i] = xstrfmt("multi-pack-index-%s.%s", keep_hashes[i],
818 - ext);
819 - data.keep_nr = hashes_nr;
820 - data.ext = ext;
817 + strset_add(&data.keep, buf.buf);
818 + }
819
820 for_each_file_in_pack_subdir(source->path, "multi-pack-index.d",
821 clear_midx_file_ext, &data);
822
825 - for (i = 0; i < hashes_nr; i++)
826 - free(data.keep[i]);
827 - free(data.keep);
823 + strbuf_release(&buf);
824 + strset_clear(&data.keep);
825 }
826
827 void clear_midx_file(struct repository *r)