midx-write.c: factor fanout layering from `compute_sorted_entries()`

When computing the set of objects to appear in a MIDX, we use compute_sorted_entries(), which handles objects from various existing sources one fanout layer at a time. The process for computing this set is slightly different during MIDX compaction, so factor out the existing functionality into its own routine to prevent `compute_sorted_entries()` from becoming too difficult to read. Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Taylor Blau committed Feb 24, 2026 at 14:00 UTC 9aea84c4e78b462fe55d142e67997b90d8ee3055
1 file changed +25 -17
midx-write.c
+25 -17
@@ -328,6 +328,30 @@ static void midx_fanout_add_pack_fanout(struct midx_fanout *fanout,
328 }
329 }
330
331 +static void midx_fanout_add(struct midx_fanout *fanout,
332 + struct write_midx_context *ctx,
333 + uint32_t start_pack,
334 + uint32_t cur_fanout)
335 +{
336 + uint32_t cur_pack;
337 +
338 + if (ctx->m && !ctx->incremental)
339 + midx_fanout_add_midx_fanout(fanout, ctx->m, cur_fanout,
340 + ctx->preferred_pack_idx);
341 +
342 + for (cur_pack = start_pack; cur_pack < ctx->nr; cur_pack++) {
343 + int preferred = cur_pack == ctx->preferred_pack_idx;
344 + midx_fanout_add_pack_fanout(fanout, ctx->info, cur_pack,
345 + preferred, cur_fanout);
346 + }
347 +
348 + if (ctx->preferred_pack_idx != NO_PREFERRED_PACK &&
349 + ctx->preferred_pack_idx < start_pack)
350 + midx_fanout_add_pack_fanout(fanout, ctx->info,
351 + ctx->preferred_pack_idx, 1,
352 + cur_fanout);
353 +}
354 +
355 /*
356 * It is possible to artificially get into a state where there are many
357 * duplicate copies of objects. That can create high memory pressure if
@@ -364,23 +388,7 @@ static void compute_sorted_entries(struct write_midx_context *ctx,
388 for (cur_fanout = 0; cur_fanout < 256; cur_fanout++) {
389 fanout.nr = 0;
390
367 - if (ctx->m && !ctx->incremental)
368 - midx_fanout_add_midx_fanout(&fanout, ctx->m, cur_fanout,
369 - ctx->preferred_pack_idx);
370 -
371 - for (cur_pack = start_pack; cur_pack < ctx->nr; cur_pack++) {
372 - int preferred = cur_pack == ctx->preferred_pack_idx;
373 - midx_fanout_add_pack_fanout(&fanout,
374 - ctx->info, cur_pack,
375 - preferred, cur_fanout);
376 - }
377 -
378 - if (ctx->preferred_pack_idx != NO_PREFERRED_PACK &&
379 - ctx->preferred_pack_idx < start_pack)
380 - midx_fanout_add_pack_fanout(&fanout, ctx->info,
381 - ctx->preferred_pack_idx, 1,
382 - cur_fanout);
383 -
391 + midx_fanout_add(&fanout, ctx, start_pack, cur_fanout);
392 midx_fanout_sort(&fanout);
393
394 /*