Skip to content

Commit 3d24b98

Browse files
szederderrickstolee
authored andcommitted
commit-graph: simplify chunk writes into loop
In write_commit_graph_file() we now have one block of code filling the array of 'struct chunk_info' with the IDs and sizes of chunks to be written, and an other block of code calling the functions responsible for writing individual chunks. In case of optional chunks like Extra Edge List an Base Graphs List there is also a condition checking whether that chunk is necessary/desired, and that same condition is repeated in both blocks of code. Other, newer chunks have similar optional conditions. Eliminate these repeated conditions by storing the function pointers responsible for writing individual chunks in the 'struct chunk_info' array as well, and calling them in a loop to write the commit-graph file. This will open up the possibility for a bit of foolproofing in the following patch. Signed-off-by: SZEDER Gábor <[email protected]> Signed-off-by: Derrick Stolee <[email protected]>
1 parent 65eb152 commit 3d24b98

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

commit-graph.c

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,9 +1532,13 @@ static int write_graph_chunk_base(struct hashfile *f,
15321532
return 0;
15331533
}
15341534

1535+
typedef int (*chunk_write_fn)(struct hashfile *f,
1536+
struct write_commit_graph_context *ctx);
1537+
15351538
struct chunk_info {
15361539
uint32_t id;
15371540
uint64_t size;
1541+
chunk_write_fn write_fn;
15381542
};
15391543

15401544
static int write_commit_graph_file(struct write_commit_graph_context *ctx)
@@ -1591,27 +1595,34 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
15911595

15921596
chunks[0].id = GRAPH_CHUNKID_OIDFANOUT;
15931597
chunks[0].size = GRAPH_FANOUT_SIZE;
1598+
chunks[0].write_fn = write_graph_chunk_fanout;
15941599
chunks[1].id = GRAPH_CHUNKID_OIDLOOKUP;
15951600
chunks[1].size = hashsz * ctx->commits.nr;
1601+
chunks[1].write_fn = write_graph_chunk_oids;
15961602
chunks[2].id = GRAPH_CHUNKID_DATA;
15971603
chunks[2].size = (hashsz + 16) * ctx->commits.nr;
1604+
chunks[2].write_fn = write_graph_chunk_data;
15981605
if (ctx->num_extra_edges) {
15991606
chunks[num_chunks].id = GRAPH_CHUNKID_EXTRAEDGES;
16001607
chunks[num_chunks].size = 4 * ctx->num_extra_edges;
1608+
chunks[num_chunks].write_fn = write_graph_chunk_extra_edges;
16011609
num_chunks++;
16021610
}
16031611
if (ctx->changed_paths) {
16041612
chunks[num_chunks].id = GRAPH_CHUNKID_BLOOMINDEXES;
16051613
chunks[num_chunks].size = sizeof(uint32_t) * ctx->commits.nr;
1614+
chunks[num_chunks].write_fn = write_graph_chunk_bloom_indexes;
16061615
num_chunks++;
16071616
chunks[num_chunks].id = GRAPH_CHUNKID_BLOOMDATA;
16081617
chunks[num_chunks].size = sizeof(uint32_t) * 3
16091618
+ ctx->total_bloom_filter_data_size;
1619+
chunks[num_chunks].write_fn = write_graph_chunk_bloom_data;
16101620
num_chunks++;
16111621
}
16121622
if (ctx->num_commit_graphs_after > 1) {
16131623
chunks[num_chunks].id = GRAPH_CHUNKID_BASE;
16141624
chunks[num_chunks].size = hashsz * (ctx->num_commit_graphs_after - 1);
1625+
chunks[num_chunks].write_fn = write_graph_chunk_base;
16151626
num_chunks++;
16161627
}
16171628

@@ -1647,19 +1658,15 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
16471658
progress_title.buf,
16481659
num_chunks * ctx->commits.nr);
16491660
}
1650-
write_graph_chunk_fanout(f, ctx);
1651-
write_graph_chunk_oids(f, ctx);
1652-
write_graph_chunk_data(f, ctx);
1653-
if (ctx->num_extra_edges)
1654-
write_graph_chunk_extra_edges(f, ctx);
1655-
if (ctx->changed_paths) {
1656-
write_graph_chunk_bloom_indexes(f, ctx);
1657-
write_graph_chunk_bloom_data(f, ctx);
1658-
}
1659-
if (ctx->num_commit_graphs_after > 1 &&
1660-
write_graph_chunk_base(f, ctx)) {
1661-
return -1;
1661+
1662+
for (i = 0; i < num_chunks; i++) {
1663+
if (chunks[i].write_fn(f, ctx)) {
1664+
error(_("failed writing chunk with id %"PRIx32""),
1665+
chunks[i].id);
1666+
return -1;
1667+
}
16621668
}
1669+
16631670
stop_progress(&ctx->progress);
16641671
strbuf_release(&progress_title);
16651672

0 commit comments

Comments
 (0)