Skip to content

Commit 17e6275

Browse files
szedergitster
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. Helped-by: René Scharfe <[email protected]> Signed-off-by: SZEDER Gábor <[email protected]> Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9bab081 commit 17e6275

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

commit-graph.c

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,9 +1559,13 @@ static int write_graph_chunk_base(struct hashfile *f,
15591559
return 0;
15601560
}
15611561

1562+
typedef int (*chunk_write_fn)(struct hashfile *f,
1563+
struct write_commit_graph_context *ctx);
1564+
15621565
struct chunk_info {
15631566
uint32_t id;
15641567
uint64_t size;
1568+
chunk_write_fn write_fn;
15651569
};
15661570

15671571
static int write_commit_graph_file(struct write_commit_graph_context *ctx)
@@ -1624,27 +1628,34 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
16241628

16251629
chunks[0].id = GRAPH_CHUNKID_OIDFANOUT;
16261630
chunks[0].size = GRAPH_FANOUT_SIZE;
1631+
chunks[0].write_fn = write_graph_chunk_fanout;
16271632
chunks[1].id = GRAPH_CHUNKID_OIDLOOKUP;
16281633
chunks[1].size = hashsz * ctx->commits.nr;
1634+
chunks[1].write_fn = write_graph_chunk_oids;
16291635
chunks[2].id = GRAPH_CHUNKID_DATA;
16301636
chunks[2].size = (hashsz + 16) * ctx->commits.nr;
1637+
chunks[2].write_fn = write_graph_chunk_data;
16311638
if (ctx->num_extra_edges) {
16321639
chunks[num_chunks].id = GRAPH_CHUNKID_EXTRAEDGES;
16331640
chunks[num_chunks].size = 4 * ctx->num_extra_edges;
1641+
chunks[num_chunks].write_fn = write_graph_chunk_extra_edges;
16341642
num_chunks++;
16351643
}
16361644
if (ctx->changed_paths) {
16371645
chunks[num_chunks].id = GRAPH_CHUNKID_BLOOMINDEXES;
16381646
chunks[num_chunks].size = sizeof(uint32_t) * ctx->commits.nr;
1647+
chunks[num_chunks].write_fn = write_graph_chunk_bloom_indexes;
16391648
num_chunks++;
16401649
chunks[num_chunks].id = GRAPH_CHUNKID_BLOOMDATA;
16411650
chunks[num_chunks].size = sizeof(uint32_t) * 3
16421651
+ ctx->total_bloom_filter_data_size;
1652+
chunks[num_chunks].write_fn = write_graph_chunk_bloom_data;
16431653
num_chunks++;
16441654
}
16451655
if (ctx->num_commit_graphs_after > 1) {
16461656
chunks[num_chunks].id = GRAPH_CHUNKID_BASE;
16471657
chunks[num_chunks].size = hashsz * (ctx->num_commit_graphs_after - 1);
1658+
chunks[num_chunks].write_fn = write_graph_chunk_base;
16481659
num_chunks++;
16491660
}
16501661

@@ -1680,19 +1691,12 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
16801691
progress_title.buf,
16811692
num_chunks * ctx->commits.nr);
16821693
}
1683-
write_graph_chunk_fanout(f, ctx);
1684-
write_graph_chunk_oids(f, ctx);
1685-
write_graph_chunk_data(f, ctx);
1686-
if (ctx->num_extra_edges)
1687-
write_graph_chunk_extra_edges(f, ctx);
1688-
if (ctx->changed_paths) {
1689-
write_graph_chunk_bloom_indexes(f, ctx);
1690-
write_graph_chunk_bloom_data(f, ctx);
1691-
}
1692-
if (ctx->num_commit_graphs_after > 1 &&
1693-
write_graph_chunk_base(f, ctx)) {
1694-
return -1;
1694+
1695+
for (i = 0; i < num_chunks; i++) {
1696+
if (chunks[i].write_fn(f, ctx))
1697+
return -1;
16951698
}
1699+
16961700
stop_progress(&ctx->progress);
16971701
strbuf_release(&progress_title);
16981702

0 commit comments

Comments
 (0)