Skip to content

Commit 3eac5e1

Browse files
ttaylorrgitster
authored andcommitted
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 fe1ed56 (midx: sort and deduplicate objects from packfiles, 2018-07-12), which came shortly after 396f257 (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. 577dc49 (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 396f257, 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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 23532be commit 3eac5e1

File tree

1 file changed

+19
-26
lines changed

1 file changed

+19
-26
lines changed

midx-write.c

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -299,21 +299,16 @@ static void midx_fanout_add_pack_fanout(struct midx_fanout *fanout,
299299
* Copy only the de-duplicated entries (selected by most-recent modified time
300300
* of a packfile containing the object).
301301
*/
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)
307303
{
308304
uint32_t cur_fanout, cur_pack, cur_object;
309305
size_t alloc_objects, total_objects = 0;
310306
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;
313308

314-
for (cur_pack = start_pack; cur_pack < nr_packs; cur_pack++)
309+
for (cur_pack = start_pack; cur_pack < ctx->nr; cur_pack++)
315310
total_objects = st_add(total_objects,
316-
info[cur_pack].p->num_objects);
311+
ctx->info[cur_pack].p->num_objects);
317312

318313
/*
319314
* 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,
323318
alloc_objects = fanout.alloc = total_objects > 3200 ? total_objects / 200 : 16;
324319

325320
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;
328323

329324
for (cur_fanout = 0; cur_fanout < 256; cur_fanout++) {
330325
fanout.nr = 0;
331326

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);
335330

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;
338333
midx_fanout_add_pack_fanout(&fanout,
339-
info, cur_pack,
334+
ctx->info, cur_pack,
340335
preferred, cur_fanout);
341336
}
342337

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,
346341
cur_fanout);
347342

348343
midx_fanout_sort(&fanout);
@@ -356,17 +351,16 @@ static struct pack_midx_entry *get_sorted_entries(struct multi_pack_index *m,
356351
&fanout.entries[cur_object].oid))
357352
continue;
358353

359-
ALLOC_GROW(deduplicated_entries, st_add(*nr_objects, 1),
354+
ALLOC_GROW(ctx->entries, st_add(ctx->entries_nr, 1),
360355
alloc_objects);
361-
memcpy(&deduplicated_entries[*nr_objects],
356+
memcpy(&ctx->entries[ctx->entries_nr],
362357
&fanout.entries[cur_object],
363358
sizeof(struct pack_midx_entry));
364-
(*nr_objects)++;
359+
ctx->entries_nr++;
365360
}
366361
}
367362

368363
free(fanout.entries);
369-
return deduplicated_entries;
370364
}
371365

372366
static int write_midx_pack_names(struct hashfile *f, void *data)
@@ -1054,8 +1048,7 @@ static int write_midx_internal(const char *object_dir,
10541048
}
10551049
}
10561050

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);
10591052

10601053
ctx.large_offsets_needed = 0;
10611054
for (i = 0; i < ctx.entries_nr; i++) {

0 commit comments

Comments
 (0)