@matheusbsbs / matheus-bagliano / commits / 17e6275fc9
Fork 0

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 <l.s.r@web.de> Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com> Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

SZEDER Gábor committed Jul 1, 2020 at 13:27 UTC 17e6275fc9f5763fb89a1219aceaf14adf340a2b
1 file changed +16 -12
commit-graph.c
+16 -12
@@ -1559,9 +1559,13 @@ static int write_graph_chunk_base(struct hashfile *f,
1559 return 0;
1560 }
1561
1562 +typedef int (*chunk_write_fn)(struct hashfile *f,
1563 + struct write_commit_graph_context *ctx);
1564 +
1565 struct chunk_info {
1566 uint32_t id;
1567 uint64_t size;
1568 + chunk_write_fn write_fn;
1569 };
1570
1571 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)
1628
1629 chunks[0].id = GRAPH_CHUNKID_OIDFANOUT;
1630 chunks[0].size = GRAPH_FANOUT_SIZE;
1631 + chunks[0].write_fn = write_graph_chunk_fanout;
1632 chunks[1].id = GRAPH_CHUNKID_OIDLOOKUP;
1633 chunks[1].size = hashsz * ctx->commits.nr;
1634 + chunks[1].write_fn = write_graph_chunk_oids;
1635 chunks[2].id = GRAPH_CHUNKID_DATA;
1636 chunks[2].size = (hashsz + 16) * ctx->commits.nr;
1637 + chunks[2].write_fn = write_graph_chunk_data;
1638 if (ctx->num_extra_edges) {
1639 chunks[num_chunks].id = GRAPH_CHUNKID_EXTRAEDGES;
1640 chunks[num_chunks].size = 4 * ctx->num_extra_edges;
1641 + chunks[num_chunks].write_fn = write_graph_chunk_extra_edges;
1642 num_chunks++;
1643 }
1644 if (ctx->changed_paths) {
1645 chunks[num_chunks].id = GRAPH_CHUNKID_BLOOMINDEXES;
1646 chunks[num_chunks].size = sizeof(uint32_t) * ctx->commits.nr;
1647 + chunks[num_chunks].write_fn = write_graph_chunk_bloom_indexes;
1648 num_chunks++;
1649 chunks[num_chunks].id = GRAPH_CHUNKID_BLOOMDATA;
1650 chunks[num_chunks].size = sizeof(uint32_t) * 3
1651 + ctx->total_bloom_filter_data_size;
1652 + chunks[num_chunks].write_fn = write_graph_chunk_bloom_data;
1653 num_chunks++;
1654 }
1655 if (ctx->num_commit_graphs_after > 1) {
1656 chunks[num_chunks].id = GRAPH_CHUNKID_BASE;
1657 chunks[num_chunks].size = hashsz * (ctx->num_commit_graphs_after - 1);
1658 + chunks[num_chunks].write_fn = write_graph_chunk_base;
1659 num_chunks++;
1660 }
1661
@@ -1680,19 +1691,12 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
1691 progress_title.buf,
1692 num_chunks * ctx->commits.nr);
1693 }
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;
1698 }
1699 +
1700 stop_progress(&ctx->progress);
1701 strbuf_release(&progress_title);
1702