midx-write.c: reduce argument count for `get_sorted_entries()`

The function `midx-write.c::get_sorted_entries()` is responsible for constructing the array of OIDs from a given list of packs which will comprise the MIDX being written. The singular call-site for this function looks something like: ctx.entries = get_sorted_entries(ctx.m, ctx.info, ctx.nr, &ctx.entries_nr, ctx.preferred_pack_idx); This function has five formal arguments, all of which are members of the shared `struct write_midx_context` used to track various pieces of information about the MIDX being written. The function `get_sorted_entries()` dates back to fe1ed56f5e4 (midx: sort and deduplicate objects from packfiles, 2018-07-12), which came shortly after 396f257018a (multi-pack-index: read packfile list, 2018-07-12). The latter patch introduced the `pack_list` structure, which was a precursor to the structure we now know as `write_midx_context` (c.f. 577dc49696a (midx: rename pack_info to write_midx_context, 2021-02-18)). At the time, `get_sorted_entries()` likely could have used the pack_list structure introduced earlier in 396f257018a, but understandably did not since the structure only contained three fields (only two of which were relevant to `get_sorted_entries()`) at the time. Simplify the declaration of this function by taking a single pointer to the whole `struct write_midx_context` instead of various members within it. Since this function is now computing the entire result (populating both `ctx->entries`, and `ctx->entries_nr`), rename it to something that doesn't start with "get_" to make clear that this function has a side-effect. Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Taylor Blau committed May 29, 2024 at 18:55 UTC 3eac5e1ff1974b63862b9f7eaf8fc8ec22d1fc7e
1 file changed +19 -26
midx-write.c
+19 -26
@@ -299,21 +299,16 @@ static void midx_fanout_add_pack_fanout(struct midx_fanout *fanout,
299 * Copy only the de-duplicated entries (selected by most-recent modified time
300 * of a packfile containing the object).
301 */
302 -static struct pack_midx_entry *get_sorted_entries(struct multi_pack_index *m,
303 - struct pack_info *info,
304 - uint32_t nr_packs,
305 - size_t *nr_objects,
306 - int preferred_pack)
302 +static void compute_sorted_entries(struct write_midx_context *ctx)
303 {
304 uint32_t cur_fanout, cur_pack, cur_object;
305 size_t alloc_objects, total_objects = 0;
306 struct midx_fanout fanout = { 0 };
311 - struct pack_midx_entry *deduplicated_entries = NULL;
312 - uint32_t start_pack = m ? m->num_packs : 0;
307 + uint32_t start_pack = ctx->m ? ctx->m->num_packs : 0;
308
314 - for (cur_pack = start_pack; cur_pack < nr_packs; cur_pack++)
309 + for (cur_pack = start_pack; cur_pack < ctx->nr; cur_pack++)
310 total_objects = st_add(total_objects,
316 - info[cur_pack].p->num_objects);
311 + ctx->info[cur_pack].p->num_objects);
312
313 /*
314 * As we de-duplicate by fanout value, we expect the fanout
@@ -323,26 +318,26 @@ static struct pack_midx_entry *get_sorted_entries(struct multi_pack_index *m,
318 alloc_objects = fanout.alloc = total_objects > 3200 ? total_objects / 200 : 16;
319
320 ALLOC_ARRAY(fanout.entries, fanout.alloc);
326 - ALLOC_ARRAY(deduplicated_entries, alloc_objects);
327 - *nr_objects = 0;
321 + ALLOC_ARRAY(ctx->entries, alloc_objects);
322 + ctx->entries_nr = 0;
323
324 for (cur_fanout = 0; cur_fanout < 256; cur_fanout++) {
325 fanout.nr = 0;
326
332 - if (m)
333 - midx_fanout_add_midx_fanout(&fanout, m, cur_fanout,
334 - preferred_pack);
327 + if (ctx->m)
328 + midx_fanout_add_midx_fanout(&fanout, ctx->m, cur_fanout,
329 + ctx->preferred_pack_idx);
330
336 - for (cur_pack = start_pack; cur_pack < nr_packs; cur_pack++) {
337 - int preferred = cur_pack == preferred_pack;
331 + for (cur_pack = start_pack; cur_pack < ctx->nr; cur_pack++) {
332 + int preferred = cur_pack == ctx->preferred_pack_idx;
333 midx_fanout_add_pack_fanout(&fanout,
339 - info, cur_pack,
334 + ctx->info, cur_pack,
335 preferred, cur_fanout);
336 }
337
343 - if (-1 < preferred_pack && preferred_pack < start_pack)
344 - midx_fanout_add_pack_fanout(&fanout, info,
345 - preferred_pack, 1,
338 + if (-1 < ctx->preferred_pack_idx && ctx->preferred_pack_idx < start_pack)
339 + midx_fanout_add_pack_fanout(&fanout, ctx->info,
340 + ctx->preferred_pack_idx, 1,
341 cur_fanout);
342
343 midx_fanout_sort(&fanout);
@@ -356,17 +351,16 @@ static struct pack_midx_entry *get_sorted_entries(struct multi_pack_index *m,
351 &fanout.entries[cur_object].oid))
352 continue;
353
359 - ALLOC_GROW(deduplicated_entries, st_add(*nr_objects, 1),
354 + ALLOC_GROW(ctx->entries, st_add(ctx->entries_nr, 1),
355 alloc_objects);
361 - memcpy(&deduplicated_entries[*nr_objects],
356 + memcpy(&ctx->entries[ctx->entries_nr],
357 &fanout.entries[cur_object],
358 sizeof(struct pack_midx_entry));
364 - (*nr_objects)++;
359 + ctx->entries_nr++;
360 }
361 }
362
363 free(fanout.entries);
369 - return deduplicated_entries;
364 }
365
366 static int write_midx_pack_names(struct hashfile *f, void *data)
@@ -1054,8 +1048,7 @@ static int write_midx_internal(const char *object_dir,
1048 }
1049 }
1050
1057 - ctx.entries = get_sorted_entries(ctx.m, ctx.info, ctx.nr, &ctx.entries_nr,
1058 - ctx.preferred_pack_idx);
1051 + compute_sorted_entries(&ctx);
1052
1053 ctx.large_offsets_needed = 0;
1054 for (i = 0; i < ctx.entries_nr; i++) {